Example usage for java.awt Rectangle intersection

List of usage examples for java.awt Rectangle intersection

Introduction

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

Prototype

public Rectangle intersection(Rectangle r) 

Source Link

Document

Computes the intersection of this Rectangle with the specified Rectangle .

Usage

From source file:Main.java

/**
 * @param bounds//from ww w  .  j a  v a2  s .  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

/**
 * Positions the specified dialog at a position relative to its parent.
 *
 * @param dialog            the dialog to be positioned.
 * @param horizontalPercent the relative location.
 * @param verticalPercent   the relative location.
 *///from   ww w .  j a  va 2 s . c  om
public static void positionDialogRelativeToParent(final Dialog dialog, final double horizontalPercent,
        final double verticalPercent) {
    final Container parent = dialog.getParent();
    if (parent == null || (parent.isVisible() == false)) {
        positionFrameOnScreen(dialog, horizontalPercent, verticalPercent);
        return;
    }

    final Dimension d = dialog.getSize();
    final Dimension p = parent.getSize();

    final int baseX = parent.getX();
    final int baseY = parent.getY();

    final int parentPointX = baseX + (int) (horizontalPercent * p.width);
    final int parentPointY = baseY + (int) (verticalPercent * p.height);

    final int dialogPointX = Math.max(0, parentPointX - (int) (horizontalPercent * d.width));
    final int dialogPointY = Math.max(0, parentPointY - (int) (verticalPercent * d.height));

    // make sure the dialog fits completely on the screen...
    final Rectangle s = parent.getGraphicsConfiguration().getBounds();
    final Rectangle r = new Rectangle(dialogPointX, dialogPointY, d.width, d.height);
    final Rectangle intersectedDialogBounds = r.intersection(s);
    if (intersectedDialogBounds.width < d.width) {
        r.x = s.width - d.width;
        r.width = d.width;
    }
    if (intersectedDialogBounds.height < d.height) {
        r.y = s.height - d.height;
        r.height = d.height;
    }
    final Rectangle finalIntersection = r.intersection(s);
    dialog.setBounds(finalIntersection);
}

From source file:Main.java

/**
 * @param r/*from  www  .ja va 2 s.  c o m*/
 *          the original rectangle
 * @param includeReservedInsets
 *          if taskbar and other windowing insets should be included in the
 *          returned area
 * @return iff there are multiple monitors the other monitor than the
 *         effective view of the monitor that the rectangle mostly coveres, or
 *         null if there is just one screen
 */
public static Rectangle getOppositeFullScreenBoundsFor(Rectangle r, boolean includeReservedInsets) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    TreeMap<Integer, Rectangle> prioMap = new TreeMap<Integer, Rectangle>();
    for (GraphicsDevice dev : ge.getScreenDevices()) {
        Rectangle bounds;
        if ((!includeReservedInsets) && dev == ge.getDefaultScreenDevice()) {
            bounds = ge.getMaximumWindowBounds();
        } else {
            bounds = dev.getDefaultConfiguration().getBounds();
        }
        Rectangle intersection = bounds.intersection(r);
        prioMap.put(intersection.width * intersection.height, bounds);
    }
    if (prioMap.size() <= 1) {
        return null;
    } else {
        return prioMap.get(prioMap.firstKey());
    }
}

From source file:Java2DUtils.java

@SuppressWarnings("unchecked")
public static void setClip(Graphics g, Rectangle clipBounds) {
    Rectangle currentClipBounds;// w  w  w . j a  va2s . c om

    clipBounds = new Rectangle(clipBounds);
    clipBounds.width += 1;
    clipBounds.height += 1;

    currentClipBounds = g.getClipBounds();
    if (currentClipBounds != null) {
        clipBounds = clipBounds.intersection(g.getClipBounds());
    }

    clipBoundsStack.push(currentClipBounds);
    g.setClip(clipBounds);
}

From source file:Main.java

/**
 * Positions the specified frame at a relative position in the screen, where 50% is considered to be the center of the
 * screen.//  w w w .j  a  va2  s  .com
 *
 * @param frame             the frame.
 * @param horizontalPercent the relative horizontal position of the frame (0.0 to 1.0, where 0.5 is the center of the
 *                          screen).
 * @param verticalPercent   the relative vertical position of the frame (0.0 to 1.0, where 0.5 is the center of the
 *                          screen).
 */
public static void positionFrameOnScreen(final Window frame, final double horizontalPercent,
        final double verticalPercent) {

    final Rectangle s = frame.getGraphicsConfiguration().getBounds();
    final Dimension f = frame.getSize();

    final int spaceOnX = Math.max(s.width - f.width, 0);
    final int spaceOnY = Math.max(s.height - f.height, 0);
    final int x = (int) (horizontalPercent * spaceOnX) + s.x;
    final int y = (int) (verticalPercent * spaceOnY) + s.y;
    frame.setBounds(x, y, f.width, f.height);
    frame.setBounds(s.intersection(frame.getBounds()));
}

From source file:GraphicsUtil.java

public static void copyBand(Raster src, int srcBand, WritableRaster dst, int dstBand) {

    Rectangle sR = src.getBounds();
    Rectangle dR = dst.getBounds();
    Rectangle cpR = sR.intersection(dR);

    copyBand(src, cpR, srcBand, dst, cpR, dstBand);
}

From source file:GraphicsUtil.java

public static void copyBand(Raster src, Rectangle sR, int sBand, WritableRaster dst, Rectangle dR, int dBand) {
    int dy = dR.y - sR.y;
    int dx = dR.x - sR.x;
    sR = sR.intersection(src.getBounds());
    dR = dR.intersection(dst.getBounds());
    int width, height;
    if (dR.width < sR.width)
        width = dR.width;/*from w  ww  . j av a 2 s .c o  m*/
    else
        width = sR.width;
    if (dR.height < sR.height)
        height = dR.height;
    else
        height = sR.height;

    int x = sR.x + dx;
    int[] samples = null;
    for (int y = sR.y; y < sR.y + height; y++) {
        samples = src.getSamples(sR.x, y, width, 1, sBand, samples);
        dst.setSamples(x, y + dy, width, 1, dBand, samples);
    }
}

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 w  ww  .  java2s  .  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

public void paint(Graphics g) {
    Rectangle r = new Rectangle(50, 50, 100, 100);
    Rectangle r1 = new Rectangle(100, 100, 75, 75);
    g.drawRect(r.x, r.y, r.width, r.height);
    g.drawRect(r1.x, r1.y, r1.width, r1.height);
    Rectangle r2 = r.intersection(r1);
    System.out.println(r2);/*from w ww.  ja  va 2s  .c om*/
    g.fillRect(r2.x, r2.y, r2.width, r2.height);
}

From source file:OAT.ui.BarChartFrame.java

@Override
protected void init() {
    //        setJMenuBar(new MainMenuBar(this));
    //        setContentPane(chartPanel);
    //        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

    //set getItemCount and location
    //Rectangle mainBound = Main.frame.getBounds();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    screenSize.height = screenSize.height - MENU_BAR_HEIGHT;
    //Dimension chartFrameSize = getChartPanelSize();
    //chartFrameSize.height += MENU_BAR_HEIGHT;

    int gridRows = Main.chartFramesGrid.length;
    int gridColumns = Main.chartFramesGrid[0].length;

    boolean isOccupied = false;
    for (int c = 0; c < gridColumns; c++) {
        for (int r = 0; r < gridRows; r++) {
            Rectangle bound = Main.chartFramesGrid[r][c];

            for (BarChartFrame existingFrame : Main.chartFrames) {
                if (bound.contains(existingFrame.getLocation())
                        || existingFrame.getBounds().contains(bound.intersection(new Rectangle(screenSize)))) {
                    isOccupied = true;// w w w.j a  va2  s . co  m
                    break;
                }
            }

            if (!isOccupied) {
                setBounds(bound);
                break;
            }
        }

        if (!isOccupied) {
            break;
        }
    }

    if (isOccupied) {
        setBounds(Main.chartFramesGrid[Main.lastChartFrameGridId
                % gridRows][(Main.lastChartFrameGridId / gridRows) % gridColumns]);
    }

    Main.lastChartFrameGridId++;

    Main.chartFrames.add(this);

    addComponentListener(new java.awt.event.ComponentAdapter() {

        @Override
        public void componentHidden(java.awt.event.ComponentEvent evt) {
            formComponentHidden(evt);
        }
    });

    //pack and display
    pack();
}