Example usage for java.awt Rectangle contains

List of usage examples for java.awt Rectangle contains

Introduction

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

Prototype

public boolean contains(int x, int y) 

Source Link

Document

Checks whether or not this Rectangle contains the point at the specified location (x,y) .

Usage

From source file:Main.java

private static Rectangle getScreenBounds(Window aWindow) {

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] devices = ge.getScreenDevices();

    if (aWindow == null)
        return devices[0].getDefaultConfiguration().getBounds();

    Rectangle bounds = aWindow.getBounds();
    int centerX = (int) bounds.getCenterX();
    int centerY = (int) bounds.getCenterY();

    for (GraphicsDevice device : devices) {
        GraphicsConfiguration gc = device.getDefaultConfiguration();
        Rectangle rect = gc.getBounds();
        if (rect.contains(centerX, centerY))
            return rect;
    }/*from w w w.j a  va  2s .co  m*/

    return null;
}

From source file:SwingUtil.java

/**
 * Verifies if the given point is visible on the screen.
 * /* w  w  w .  j  a v  a  2s .  co m*/
 * @param    location       The given location on the screen.
 * @return                True if the location is on the screen, false otherwise.
 */
public static boolean isLocationInScreenBounds(Point location) {

    // Check if the location is in the bounds of one of the graphics devices.
    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
    Rectangle graphicsConfigurationBounds = new Rectangle();

    // Iterate over the graphics devices.
    for (int j = 0; j < graphicsDevices.length; j++) {

        // Get the bounds of the device.
        GraphicsDevice graphicsDevice = graphicsDevices[j];
        graphicsConfigurationBounds.setRect(graphicsDevice.getDefaultConfiguration().getBounds());

        // Is the location in this bounds?
        graphicsConfigurationBounds.setRect(graphicsConfigurationBounds.x, graphicsConfigurationBounds.y,
                graphicsConfigurationBounds.width, graphicsConfigurationBounds.height);
        if (graphicsConfigurationBounds.contains(location.x, location.y)) {

            // The location is in this screengraphics.
            return true;

        }

    }

    // We could not find a device that contains the given point.
    return false;

}

From source file:Main.java

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

    Rectangle r = new Rectangle(20, 20, 200, 200);

    g2.fill(r);//  w  ww .  ja  va2 s  .c o m
    System.out.println(r.contains(40, 40));

}

From source file:Main.java

public void mouseClicked(MouseEvent evt) {
    JTable table = ((JTableHeader) evt.getSource()).getTable();
    TableColumnModel colModel = table.getColumnModel();

    int index = colModel.getColumnIndexAtX(evt.getX());
    if (index == -1) {
        return;/* ww w  .  j  a  va2 s.  c om*/
    }
    Rectangle headerRect = table.getTableHeader().getHeaderRect(index);
    if (index == 0) {
        headerRect.width -= 10;
    } else {
        headerRect.grow(-10, 0);
    }
    if (!headerRect.contains(evt.getX(), evt.getY())) {
        int vLeftColIndex = index;
        if (evt.getX() < headerRect.x) {
            vLeftColIndex--;
        }
    }
}

From source file:Main.java

@Override
public boolean isCellEditable(EventObject e) {
    if (e instanceof MouseEvent && e.getSource() instanceof JTree) {
        MouseEvent me = (MouseEvent) e;
        JTree tree = (JTree) e.getSource();
        TreePath path = tree.getPathForLocation(me.getX(), me.getY());
        Rectangle r = tree.getPathBounds(path);
        if (r == null) {
            return false;
        }//from   w  w w  .jav a 2s .c  o m
        Dimension d = check.getPreferredSize();
        r.setSize(new Dimension(d.width, r.height));
        if (r.contains(me.getX(), me.getY())) {
            check.setBounds(new Rectangle(0, 0, d.width, r.height));
            return true;
        }
    }
    return false;
}

From source file:Main.java

@Override
public boolean isCellEditable(final EventObject event) {
    Object source = event.getSource();
    if (!(source instanceof JTree) || !(event instanceof MouseEvent)) {
        return false;
    }//from  w  ww . j  a  v  a2  s  . c  o  m
    JTree tree = (JTree) source;
    MouseEvent mouseEvent = (MouseEvent) event;
    TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
    if (path == null) {
        return false;
    }
    Object node = path.getLastPathComponent();
    if (node == null || !(node instanceof DefaultMutableTreeNode)) {
        return false;
    }

    Rectangle r = tree.getPathBounds(path);
    if (r == null) {
        return false;
    }
    Dimension d = panel.getPreferredSize();
    r.setSize(new Dimension(d.width, r.height));
    if (r.contains(mouseEvent.getX(), mouseEvent.getY())) {
        Point pt = SwingUtilities.convertPoint(tree, mouseEvent.getPoint(), panel);
        Object o = SwingUtilities.getDeepestComponentAt(panel, pt.x, pt.y);
        if (o instanceof JComboBox) {
            comboBox.showPopup();
        } else if (o instanceof Component) {
            Object oo = SwingUtilities.getAncestorOfClass(JComboBox.class, (Component) o);
            if (oo instanceof JComboBox) {
                comboBox.showPopup();
            }
        }
        return true;
    }
    return delegate.isCellEditable(event);
}

From source file:org.squidy.nodes.MouseIO.java

protected void setMouse(DataPosition2D dataPosition2D) {

    if (!manual) {
        double xPos = (width * dataPosition2D.getX()) + minX;
        double yPos = (height * dataPosition2D.getY()) + minY;
        boolean allowPoint = false;
        // Fasts up the contain procedure -> most cases it won't require an
        // iteration for all bounds.
        if (lastBounds != null && lastBounds.contains((int) xPos, (int) yPos)) {
            allowPoint = true;//from  w w  w . ja v a  2  s  . c  o  m
        }
        if (!allowPoint) {
            for (Rectangle bounds : screenBounds) {
                if (bounds.contains((int) xPos, (int) yPos)) {
                    lastBounds = bounds;
                    allowPoint = true;
                    break;
                }
            }
        }
        // Only move mouse if point is within a bound of a graphics device.
        if (allowPoint) {
            robot.mouseMove((int) xPos, (int) yPos);
        }
    } else {
        double xPos = (manualWidth * dataPosition2D.getX()) + originOffsetX;
        double yPos = (manualHeight * dataPosition2D.getY()) + originOffsetY;

        robot.mouseMove((int) xPos, (int) yPos);
    }
}

From source file:knop.psfj.BeadFrameList.java

/**
 * Gets the from roi./*from  w  w  w .j  a  va  2  s .  c o  m*/
 *
 * @param roi the roi
 * @return the from roi
 */
public BeadFrameList getFromROI(Rectangle roi) {

    if (roi == null)
        return this;
    BeadFrameList list = new BeadFrameList();

    for (BeadFrame frame : this) {
        if (roi.contains(frame.getFrameCenterX(), frame.getFrameCenterY()))
            list.add(frame);
    }

    return list;

}

From source file:org.domainmath.gui.FileTreePanel.java

/**
 * Returns selected file(last)./*w w w  .  j  a  va2s .com*/
 * @return lastSelectedFile
 */
public File getLastSelectedFile() {
    fileTree.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            TreePath path = fileTree.getPathForLocation(e.getX(), e.getY());
            Rectangle pathBounds = fileTree.getUI().getPathBounds(fileTree, path);
            if (pathBounds != null && pathBounds.contains(e.getX(), e.getY())) {
                File file = (File) fileTree.getLastSelectedPathComponent();
                lastSelectedFile = file;
            }
        }
    });
    return lastSelectedFile;
}

From source file:org.domainmath.gui.FileTreePanel.java

/**
 * Handle key events like F5,DELETE etc.
 * @param e /*from w  w  w  .  j  a  v a2  s  .co  m*/
 */
private void addGlobalAction(MouseEvent e) {
    TreePath path2 = fileTree.getPathForLocation(e.getX(), e.getY());
    Rectangle pathBounds2 = fileTree.getUI().getPathBounds(fileTree, path2);
    if (pathBounds2 != null && pathBounds2.contains(e.getX(), e.getY())) {
        TreePath[] selectionPaths = fileTree.getSelectionModel().getSelectionPaths();
        openAction = new OpenAction((File) fileTree.getLastSelectedPathComponent());
        refreshAction = new RefreshAction((File) fileTree.getLastSelectedPathComponent());
        deleteAction = new DeleteAction(selectionPaths);

        InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = getRootPane().getActionMap();

        inputMap.put(keyDeleteItem, "DeleteAction");
        actionMap.put("DeleteAction", deleteAction);
        inputMap.put(keyOpenItem, "OpenAction");
        actionMap.put("OpenAction", openAction);
        inputMap.put(keyRefreshItem, "RefreshAction");
        actionMap.put("RefreshAction", refreshAction);
    }
}