Java Code Examples for java.awt.Rectangle.clone()

The following are Jave code examples for showing how to use clone() of the java.awt.Rectangle class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: PopupManager.java   View Source Code Vote up 5 votes
/** Variation of the method for computing the bounds
 * for the concrete view component. As the component can possibly
 * be placed in a scroll pane it's first necessary
 * to translate the cursor bounds and also translate
 * back the resulting popup bounds.
 * @param popup  popup panel to be displayed
 * @param view component over which the popup is displayed.
 * @param cursorBounds the bounds of the caret or mouse cursor
 *    relative to the upper-left corner of the visible view.
 * @param placement where to place the popup panel according to
 *    the cursor position.
 * @return bounds of popup panel relative to the upper-left corner
 *    of the underlying view component.
 *    <CODE>null</CODE> if there is no place to display popup.
 */
protected static Rectangle computeBounds(JComponent popup,
JComponent view, Rectangle cursorBounds, Placement placement, HorizontalBounds horizontalBounds) {
    
    if (horizontalBounds == null) horizontalBounds = ViewPortBounds;
    
    Rectangle ret;
    Component viewParent = view.getParent();
    
    if (viewParent instanceof JLayeredPane) {
        viewParent = viewParent.getParent();
    }
    
    if (viewParent instanceof JViewport) {
        Rectangle viewBounds = ((JViewport)viewParent).getViewRect();

        Rectangle translatedCursorBounds = (Rectangle)cursorBounds.clone();
        if (placement != FixedPoint) {
            translatedCursorBounds.translate(-viewBounds.x, -viewBounds.y);
        }

        ret = computeBounds(popup, viewBounds.width, viewBounds.height,
            translatedCursorBounds, placement, horizontalBounds);
        
        if (ret != null) { // valid bounds
            ret.translate(viewBounds.x, viewBounds.y);
        }
        
    } else { // not in scroll pane
        ret = computeBounds(popup, view.getWidth(), view.getHeight(),
            cursorBounds, placement);
    }
    
    return ret;
}
 
Example 2
Project: jdk8u-jdk   File: SourceClippingBlitTest.java   View Source Code Vote up 4 votes
public void test(Rectangle srcRect, Rectangle dstRect) {
    int w = getWidth();
    int h = getHeight();
    Toolkit.getDefaultToolkit().sync();
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {}
    Point p = getLocationOnScreen();
    grabbedBI = robot.createScreenCapture(new Rectangle(p.x, p.y, w, h));

    // calculate the destination rectangle
    Rectangle srcBounds = srcRect.intersection(IMAGE_BOUNDS);
    int trX = dstRect.x - srcRect.x;
    int trY = dstRect.y - srcRect.y;
    Rectangle newDstRect = (Rectangle)dstRect.clone();
    newDstRect.translate(-trX, -trY);
    Rectangle.intersect(newDstRect, srcBounds, newDstRect);
    newDstRect.translate(trX, trY);
    Rectangle.intersect(newDstRect, new Rectangle(0, 0, w, h), newDstRect);

    System.out.println("calculated dest rect:" + newDstRect);

    // we do implicit clipping of the destination surface
    // by only checking pixels within its bounds
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int rgb = 0;
            if (newDstRect.contains(x, y)) {
                rgb = Color.red.getRGB();
            } else {
                rgb = Color.green.getRGB();
            }
            if (grabbedBI.getRGB(x, y) != rgb) {
                String msg1 = "Test failed at x="+x+" y="+y;
                System.out.println(msg1);
                System.out.println(" expected: "+Integer.toHexString(rgb)+
                        " got:"+Integer.toHexString(grabbedBI.getRGB(x, y)));
                throw new RuntimeException(msg1);
            }
        }
    }
    System.out.println("subtest passed");
}
 
Example 3
Project: openjdk-jdk10   File: SourceClippingBlitTest.java   View Source Code Vote up 4 votes
public void test(Rectangle srcRect, Rectangle dstRect) {
    int w = getWidth();
    int h = getHeight();
    Toolkit.getDefaultToolkit().sync();
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {}
    Point p = getLocationOnScreen();
    grabbedBI = robot.createScreenCapture(new Rectangle(p.x, p.y, w, h));

    // calculate the destination rectangle
    Rectangle srcBounds = srcRect.intersection(IMAGE_BOUNDS);
    int trX = dstRect.x - srcRect.x;
    int trY = dstRect.y - srcRect.y;
    Rectangle newDstRect = (Rectangle)dstRect.clone();
    newDstRect.translate(-trX, -trY);
    Rectangle.intersect(newDstRect, srcBounds, newDstRect);
    newDstRect.translate(trX, trY);
    Rectangle.intersect(newDstRect, new Rectangle(0, 0, w, h), newDstRect);

    System.out.println("calculated dest rect:" + newDstRect);

    // we do implicit clipping of the destination surface
    // by only checking pixels within its bounds
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int rgb = 0;
            if (newDstRect.contains(x, y)) {
                rgb = Color.red.getRGB();
            } else {
                rgb = Color.green.getRGB();
            }
            if (grabbedBI.getRGB(x, y) != rgb) {
                String msg1 = "Test failed at x="+x+" y="+y;
                System.out.println(msg1);
                System.out.println(" expected: "+Integer.toHexString(rgb)+
                        " got:"+Integer.toHexString(grabbedBI.getRGB(x, y)));
                throw new RuntimeException(msg1);
            }
        }
    }
    System.out.println("subtest passed");
}
 
Example 4
Project: OpenJSharp   File: IIOParam.java   View Source Code Vote up 3 votes
/**
 * Sets the source region of interest.  The region of interest is
 * described as a rectangle, with the upper-left corner of the
 * source image as pixel (0, 0) and increasing values down and to
 * the right.  The actual number of pixels used will depend on
 * the subsampling factors set by <code>setSourceSubsampling</code>.
 * If subsampling has been set such that this number is zero,
 * an <code>IllegalStateException</code> will be thrown.
 *
 * <p> The source region of interest specified by this method will
 * be clipped as needed to fit within the source bounds, as well
 * as the destination offsets, width, and height at the time of
 * actual I/O.
 *
 * <p> A value of <code>null</code> for <code>sourceRegion</code>
 * will remove any region specification, causing the entire image
 * to be used.
 *
 * @param sourceRegion a <code>Rectangle</code> specifying the
 * source region of interest, or <code>null</code>.
 *
 * @exception IllegalArgumentException if
 * <code>sourceRegion</code> is non-<code>null</code> and either
 * <code>sourceRegion.x</code> or <code>sourceRegion.y</code> is
 * negative.
 * @exception IllegalArgumentException if
 * <code>sourceRegion</code> is non-<code>null</code> and either
 * <code>sourceRegion.width</code> or
 * <code>sourceRegion.height</code> is negative or 0.
 * @exception IllegalStateException if subsampling is such that
 * this region will have a subsampled width or height of zero.
 *
 * @see #getSourceRegion
 * @see #setSourceSubsampling
 * @see ImageReadParam#setDestinationOffset
 * @see ImageReadParam#getDestinationOffset
 */
public void setSourceRegion(Rectangle sourceRegion) {
    if (sourceRegion == null) {
        this.sourceRegion = null;
        return;
    }

    if (sourceRegion.x < 0) {
        throw new IllegalArgumentException("sourceRegion.x < 0!");
    }
    if (sourceRegion.y < 0){
        throw new IllegalArgumentException("sourceRegion.y < 0!");
    }
    if (sourceRegion.width <= 0) {
        throw new IllegalArgumentException("sourceRegion.width <= 0!");
    }
    if (sourceRegion.height <= 0) {
        throw new IllegalArgumentException("sourceRegion.height <= 0!");
    }

    // Throw an IllegalStateException if region falls between subsamples
    if (sourceRegion.width <= subsamplingXOffset) {
        throw new IllegalStateException
            ("sourceRegion.width <= subsamplingXOffset!");
    }
    if (sourceRegion.height <= subsamplingYOffset) {
        throw new IllegalStateException
            ("sourceRegion.height <= subsamplingYOffset!");
    }

    this.sourceRegion = (Rectangle)sourceRegion.clone();
}
 
Example 5
Project: jdk8u-jdk   File: IIOParam.java   View Source Code Vote up 3 votes
/**
 * Sets the source region of interest.  The region of interest is
 * described as a rectangle, with the upper-left corner of the
 * source image as pixel (0, 0) and increasing values down and to
 * the right.  The actual number of pixels used will depend on
 * the subsampling factors set by <code>setSourceSubsampling</code>.
 * If subsampling has been set such that this number is zero,
 * an <code>IllegalStateException</code> will be thrown.
 *
 * <p> The source region of interest specified by this method will
 * be clipped as needed to fit within the source bounds, as well
 * as the destination offsets, width, and height at the time of
 * actual I/O.
 *
 * <p> A value of <code>null</code> for <code>sourceRegion</code>
 * will remove any region specification, causing the entire image
 * to be used.
 *
 * @param sourceRegion a <code>Rectangle</code> specifying the
 * source region of interest, or <code>null</code>.
 *
 * @exception IllegalArgumentException if
 * <code>sourceRegion</code> is non-<code>null</code> and either
 * <code>sourceRegion.x</code> or <code>sourceRegion.y</code> is
 * negative.
 * @exception IllegalArgumentException if
 * <code>sourceRegion</code> is non-<code>null</code> and either
 * <code>sourceRegion.width</code> or
 * <code>sourceRegion.height</code> is negative or 0.
 * @exception IllegalStateException if subsampling is such that
 * this region will have a subsampled width or height of zero.
 *
 * @see #getSourceRegion
 * @see #setSourceSubsampling
 * @see ImageReadParam#setDestinationOffset
 * @see ImageReadParam#getDestinationOffset
 */
public void setSourceRegion(Rectangle sourceRegion) {
    if (sourceRegion == null) {
        this.sourceRegion = null;
        return;
    }

    if (sourceRegion.x < 0) {
        throw new IllegalArgumentException("sourceRegion.x < 0!");
    }
    if (sourceRegion.y < 0){
        throw new IllegalArgumentException("sourceRegion.y < 0!");
    }
    if (sourceRegion.width <= 0) {
        throw new IllegalArgumentException("sourceRegion.width <= 0!");
    }
    if (sourceRegion.height <= 0) {
        throw new IllegalArgumentException("sourceRegion.height <= 0!");
    }

    // Throw an IllegalStateException if region falls between subsamples
    if (sourceRegion.width <= subsamplingXOffset) {
        throw new IllegalStateException
            ("sourceRegion.width <= subsamplingXOffset!");
    }
    if (sourceRegion.height <= subsamplingYOffset) {
        throw new IllegalStateException
            ("sourceRegion.height <= subsamplingYOffset!");
    }

    this.sourceRegion = (Rectangle)sourceRegion.clone();
}
 
Example 6
Project: openjdk-jdk10   File: IIOParam.java   View Source Code Vote up 3 votes
/**
 * Sets the source region of interest.  The region of interest is
 * described as a rectangle, with the upper-left corner of the
 * source image as pixel (0, 0) and increasing values down and to
 * the right.  The actual number of pixels used will depend on
 * the subsampling factors set by {@code setSourceSubsampling}.
 * If subsampling has been set such that this number is zero,
 * an {@code IllegalStateException} will be thrown.
 *
 * <p> The source region of interest specified by this method will
 * be clipped as needed to fit within the source bounds, as well
 * as the destination offsets, width, and height at the time of
 * actual I/O.
 *
 * <p> A value of {@code null} for {@code sourceRegion}
 * will remove any region specification, causing the entire image
 * to be used.
 *
 * @param sourceRegion a {@code Rectangle} specifying the
 * source region of interest, or {@code null}.
 *
 * @exception IllegalArgumentException if
 * {@code sourceRegion} is non-{@code null} and either
 * {@code sourceRegion.x} or {@code sourceRegion.y} is
 * negative.
 * @exception IllegalArgumentException if
 * {@code sourceRegion} is non-{@code null} and either
 * {@code sourceRegion.width} or
 * {@code sourceRegion.height} is negative or 0.
 * @exception IllegalStateException if subsampling is such that
 * this region will have a subsampled width or height of zero.
 *
 * @see #getSourceRegion
 * @see #setSourceSubsampling
 * @see ImageReadParam#setDestinationOffset
 * @see ImageReadParam#getDestinationOffset
 */
public void setSourceRegion(Rectangle sourceRegion) {
    if (sourceRegion == null) {
        this.sourceRegion = null;
        return;
    }

    if (sourceRegion.x < 0) {
        throw new IllegalArgumentException("sourceRegion.x < 0!");
    }
    if (sourceRegion.y < 0){
        throw new IllegalArgumentException("sourceRegion.y < 0!");
    }
    if (sourceRegion.width <= 0) {
        throw new IllegalArgumentException("sourceRegion.width <= 0!");
    }
    if (sourceRegion.height <= 0) {
        throw new IllegalArgumentException("sourceRegion.height <= 0!");
    }

    // Throw an IllegalStateException if region falls between subsamples
    if (sourceRegion.width <= subsamplingXOffset) {
        throw new IllegalStateException
            ("sourceRegion.width <= subsamplingXOffset!");
    }
    if (sourceRegion.height <= subsamplingYOffset) {
        throw new IllegalStateException
            ("sourceRegion.height <= subsamplingYOffset!");
    }

    this.sourceRegion = (Rectangle)sourceRegion.clone();
}