Example usage for java.awt Rectangle equals

List of usage examples for java.awt Rectangle equals

Introduction

In this page you can find the example usage for java.awt Rectangle equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Checks whether two rectangles are equal.

Usage

From source file:Main.java

/**
 * Returns an appropriate location for a component's tool tip that <i>always</i>
 * lies within the specified frame./*from  ww  w  . ja v  a2  s  .  c  o m*/
 * <p>
 * Intended be used in custom implementations of {@link JComponent#getToolTipLocation(MouseEvent)}.
 *
 * @param e
 *          the event that caused the display of the tool tip
 * @param c
 *          the parent component of the tool tip
 * @param frame
 *          a component in which the tool tip has to fit (usually the surrounding window of "c")
 * @return
 */
public static Point getAdjustedToolTipLocation(MouseEvent e, JComponent c, Component frame) {
    JToolTip tip = new JToolTip();
    tip.setTipText(c.getToolTipText(e));
    Dimension tipSize = tip.getPreferredSize();
    // Tool tip will be positioned within the bounds of the specified component (+ 5px inset)
    Rectangle frameR = frame.getBounds();
    if (frame instanceof Container) {
        Container container = (Container) frame;
        Insets insets = container.getInsets();
        frameR.x += insets.left;
        frameR.y += insets.top;
        frameR.width -= (insets.left + insets.right);
        frameR.height -= (insets.top + insets.bottom);
    }
    frameR.x += 5;
    frameR.y += 5;
    frameR.width -= 10;
    frameR.height -= 10;
    // Initial try for the tool tip's position
    Rectangle r = new Rectangle(e.getXOnScreen(), c.getLocationOnScreen().y + c.getSize().height + 1,
            tipSize.width, tipSize.height);
    // Check if it fits within the frame
    Rectangle intersection = frameR.intersection(r);
    if (r.equals(intersection)) {
        // Tool tip is fully visible within the frame --> use default behaviour
        //
        // Note: The implementation of ToolTipManager.showTipWindow() is not always
        // correct in dual screen mode. The tool tip is _always_ put on that screen,
        // where the most part of the frame lies upon, even if we return coordinates
        // that clearly belong to the other screen. Unfortunately we cannot change
        // that behavior... (bsh 2010-11-24)
        return null;
    }
    // Otherwise, move the tool tip
    int correction = 0;
    if (r.height == intersection.height) {
        // Height is okay, just move left. To make it look better, position the
        // tip 5px below the component.
        r = new Rectangle(r.x, c.getLocationOnScreen().y + c.getSize().height + 5, tipSize.width,
                tipSize.height);
        correction = -5; // needed to make the ToolTipManager use a lightweight pop-up
    } else {
        // The height does not fit. Position the tool tip above the component.
        r = new Rectangle(c.getLocationOnScreen().x + 10, c.getLocationOnScreen().y - tipSize.height - 1,
                tipSize.width, tipSize.height);
    }
    // Adjust to frame bounds
    intersection = frameR.intersection(r);
    intersection.x -= (r.width - intersection.width);
    intersection.y -= (r.height - intersection.height);
    // Return value is expected to be relative to the component's position
    return new Point((-c.getLocationOnScreen().x) + intersection.x + correction,
            (-c.getLocationOnScreen().y) + intersection.y);
}

From source file:Main.java

/**
 * @param bounds//ww  w . ja  va2s.  c o m
 * @return
 */
public static GraphicsDevice getScreenByBounds(Rectangle bounds) {
    final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    final GraphicsDevice[] screens = ge.getScreenDevices();
    Rectangle biggestIntersection = null;
    GraphicsDevice bestScreen = null;
    for (final GraphicsDevice screen : screens) {
        final Rectangle sb = screen.getDefaultConfiguration().getBounds();
        Rectangle intersection = sb.intersection(bounds);
        if (intersection != null) {
            if (biggestIntersection == null || intersection.width
                    * intersection.height > biggestIntersection.width * biggestIntersection.height) {
                biggestIntersection = intersection;
                bestScreen = screen;
                if (intersection.equals(bounds)) {
                    // it will not get better
                    break;
                }
            }
        }

    }
    return (biggestIntersection == null || biggestIntersection.width * biggestIntersection.height == 0) ? null
            : bestScreen;
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    Rectangle r = new Rectangle(10, 10, 50, 40);

    System.out.println(r.equals(new Rectangle(20, 20, 60, 60)));

    g2.fill(r);/*from   w w  w  .  jav  a  2s .  c o  m*/
}

From source file:JXTransformer.java

public void paint(Graphics g) {
    //repaint the whole transformer in case the view component was repainted
    Rectangle clipBounds = g.getClipBounds();        
    if (clipBounds != null && !clipBounds.equals(visibleRect)) {
        repaint();//from ww  w  .  j a v a 2s  .  c om
    }
    //clear the background
    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());

    if (view != null && at.getDeterminant() != 0) {
        Graphics2D g2 = (Graphics2D) g.create();
        Insets insets = getInsets();
        Rectangle bounds = getBounds();
            
        //don't forget about insets
        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= insets.left + insets.right;
        bounds.height -= insets.top + insets.bottom;
        double centerX1 = bounds.getCenterX();
        double centerY1 = bounds.getCenterY();

        Rectangle tb = getTransformedSize();
        double centerX2 = tb.getCenterX();
        double centerY2 = tb.getCenterY();

        //set antialiasing by default
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if (renderingHints != null) {
            g2.addRenderingHints(renderingHints);
        }
        //translate it to the center of the view component again
        double tx = centerX1 - centerX2 - getX();
        double ty = centerY1 - centerY2 - getY();
        g2.translate((int) tx, (int) ty);
        g2.transform(at);
        view.paint(g2);
        g2.dispose();
    }
    //paint the border
    paintBorder(g);
}

From source file:net.pms.newgui.components.WindowProperties.java

private boolean updateScreenBounds(@Nonnull Window eventWindow) {
    Rectangle bounds = eventWindow.getGraphicsConfiguration().getBounds();
    if (bounds == null) {
        if (screenBounds == null) {
            return false;
        }/*from w  w w .j a  va2  s .  com*/
    } else if (bounds.equals(screenBounds)) {
        return false;
    }
    screenBounds = bounds;
    return true;
}

From source file:net.pms.newgui.components.WindowProperties.java

private boolean updateEffectiveScreenBounds() {
    if (screenBounds == null) {
        return false;
    }/*w  ww . ja v  a  2s.  co  m*/
    Insets tmpInsets = screenInsets == null ? new Insets(0, 0, 0, 0) : screenInsets;
    Rectangle newEffectiveScreenBounds = new Rectangle(screenBounds.x, screenBounds.y,
            screenBounds.width - tmpInsets.left - tmpInsets.right,
            screenBounds.height - tmpInsets.top - tmpInsets.bottom);
    if (!newEffectiveScreenBounds.equals(effectiveScreenBounds)) {
        effectiveScreenBounds = newEffectiveScreenBounds;
        return true;
    }
    return false;
}

From source file:net.pms.newgui.components.WindowProperties.java

private boolean updateWindowBounds(@Nullable Window eventWindow) {
    if (eventWindow != window) {
        return false;
    }//from w  ww  .j a v  a2s . c o m
    int state = eventWindow instanceof Frame ? ((Frame) eventWindow).getExtendedState() : 0;
    Rectangle bounds;
    if (state == 0) {
        bounds = eventWindow.getBounds();
    } else if ((state & Frame.MAXIMIZED_BOTH) != Frame.MAXIMIZED_BOTH) {
        bounds = eventWindow.getBounds();
        // Don't store maximized dimensions
        if ((state & Frame.MAXIMIZED_HORIZ) != 0) {
            bounds.x = windowBounds.x;
            bounds.width = windowBounds.width;
        } else if ((state & Frame.MAXIMIZED_VERT) != 0) {
            bounds.y = windowBounds.y;
            bounds.height = windowBounds.height;
        }
    } else {
        bounds = windowBounds;
    }
    boolean changed = !bounds.equals(windowBounds);
    if (changed) {
        windowBounds = bounds;
    }
    if (windowState != (byte) state) {
        windowState = (byte) state;
        changed = true;
    }
    return changed;
}

From source file:org.geotools.gce.imagemosaic.RasterLayerResponse.java

/**
 * Once we reach this method it means that we have loaded all the images
 * which were intersecting the requested envelope. Next step is to create
 * the final mosaic image and cropping it to the exact requested envelope.
 * @param visitor //from ww w.j  a  v  a  2 s .  co  m
 * 
 * @return A {@link RenderedImage}}.
 */
private RenderedImage buildMosaic(final MosaicBuilder visitor) throws IOException {

    // build final layout and use it for cropping purposes
    final ImageLayout layout = new ImageLayout(rasterBounds.x, rasterBounds.y, rasterBounds.width,
            rasterBounds.height);

    //prepare hints
    final Dimension tileDimensions = request.getTileDimensions();
    if (tileDimensions != null) {
        layout.setTileHeight(tileDimensions.width).setTileWidth(tileDimensions.height);
    }
    final RenderingHints localHints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
    if (hints != null && !hints.isEmpty()) {
        if (hints.containsKey(JAI.KEY_TILE_CACHE)) {
            final Object tc = hints.get(JAI.KEY_TILE_CACHE);
            if (tc != null && tc instanceof TileCache)
                localHints.add(new RenderingHints(JAI.KEY_TILE_CACHE, (TileCache) tc));
        }
        boolean addBorderExtender = true;
        if (hints != null && hints.containsKey(JAI.KEY_BORDER_EXTENDER)) {
            final Object extender = hints.get(JAI.KEY_BORDER_EXTENDER);
            if (extender != null && extender instanceof BorderExtender) {
                localHints.add(new RenderingHints(JAI.KEY_BORDER_EXTENDER, (BorderExtender) extender));
                addBorderExtender = false;
            }
        }
        if (addBorderExtender) {
            localHints.add(ImageUtilities.BORDER_EXTENDER_HINTS);
        }
        if (hints.containsKey(JAI.KEY_TILE_SCHEDULER)) {
            final Object ts = hints.get(JAI.KEY_TILE_SCHEDULER);
            if (ts != null && ts instanceof TileScheduler)
                localHints.add(new RenderingHints(JAI.KEY_TILE_SCHEDULER, (TileScheduler) ts));
        }
    }

    //
    // SPECIAL CASE
    // 1 single tile, we try not do a mosaic.
    final ROI[] sourceRoi = visitor.sourceRoi;
    if (visitor.granulesNumber == 1 && Utils.OPTIMIZE_CROP) {
        // the roi is exactly equal to the 
        final ROI roi = visitor.rois.get(0);
        Rectangle bounds = Utils.toRectangle(roi.getAsShape());
        if (bounds != null) {
            RenderedImage image = visitor.getSourcesAsArray()[0];
            Rectangle imageBounds = PlanarImage.wrapRenderedImage(image).getBounds();
            if (imageBounds.equals(bounds)) {

                // do we need to crop? (image is bigger than requested?)
                if (!rasterBounds.contains(imageBounds)) {
                    // we have to crop
                    XRectangle2D.intersect(imageBounds, rasterBounds, imageBounds);

                    if (imageBounds.isEmpty()) {
                        // return back a constant image
                        return null;
                    }
                    // crop
                    ImageWorker iw = new ImageWorker(image);
                    iw.setRenderingHints(localHints);
                    iw.crop(imageBounds.x, imageBounds.y, imageBounds.width, imageBounds.height);

                    image = iw.getRenderedImage();
                    imageBounds = PlanarImage.wrapRenderedImage(image).getBounds();
                }

                // and, do we need to add a border around the image?
                if (!imageBounds.contains(rasterBounds)) {
                    image = MosaicDescriptor.create(new RenderedImage[] { image },
                            request.isBlend() ? MosaicDescriptor.MOSAIC_TYPE_BLEND
                                    : MosaicDescriptor.MOSAIC_TYPE_OVERLAY,
                            (alphaIn || visitor.doInputTransparency) ? visitor.alphaChannels : null, sourceRoi,
                            visitor.sourceThreshold, backgroundValues, localHints);
                }

                return image;
            }
        }
    }

    // 
    // Final Merge
    // 
    // I can even do a stacking merge or a flat merge
    final RenderedImage mosaic = request.getMergeBehavior().process(visitor.getSourcesAsArray(),
            backgroundValues, visitor.sourceThreshold,
            (alphaIn || visitor.doInputTransparency) ? visitor.alphaChannels : null, sourceRoi,
            request.isBlend() ? MosaicDescriptor.MOSAIC_TYPE_BLEND : MosaicDescriptor.MOSAIC_TYPE_OVERLAY,
            localHints);

    if (setRoiProperty) {

        //Adding globalRoi to the output
        RenderedOp rop = (RenderedOp) mosaic;
        ROI globalRoi = null;
        ROI[] rois = sourceRoi;
        for (int i = 0; i < rois.length; i++) {
            if (globalRoi == null) {
                globalRoi = new ROIGeometry(((ROIGeometry) rois[i]).getAsGeometry());
            } else {
                globalRoi = globalRoi.add(rois[i]);
            }
        }
        rop.setProperty("ROI", globalRoi);
    }

    if (LOGGER.isLoggable(Level.FINE))
        LOGGER.fine("Mosaic created ");

    // create the coverage
    return mosaic;

}

From source file:org.pentaho.reporting.libraries.designtime.swing.LibSwingUtil.java

public static boolean safeRestoreWindow(final Window frame, final Rectangle bounds) {
    final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    final GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
    for (int i = 0; i < devices.length; i++) {
        final GraphicsDevice device = devices[i];
        final Rectangle rectangle = device.getDefaultConfiguration().getBounds();
        if (rectangle.contains(bounds) || rectangle.equals(bounds)) {
            logger.info("Found a usable screen-configuration: Restoring frame to " + bounds);
            frame.setBounds(bounds);/*from  www .j  a v  a  2 s  .c o  m*/
            return true;
        }
    }
    return false;
}

From source file:org.squidy.designer.util.DrawableString.java

/**
 * @param bounds//from w w w .  j a  va 2  s  .c  o m
 */
public void setBounds(Rectangle bounds) {
    if (bounds != null && !bounds.equals(this.bounds)) {
        this.bounds = bounds;
        dirty = true;
    }
}