Example usage for java.awt Rectangle Rectangle

List of usage examples for java.awt Rectangle Rectangle

Introduction

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

Prototype

public Rectangle(Dimension d) 

Source Link

Document

Constructs a new Rectangle whose top left corner is (0, 0) and whose width and height are specified by the Dimension argument.

Usage

From source file:Main.java

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

    Rectangle r = new Rectangle(new Point(10, 10));

    r.setSize(30, 30);//from w w  w.ja  v a  2s. co  m

    g2.fill(r);

    System.out.println(r.isEmpty());
}

From source file:Main.java

/**
 * Returns rectangle copy.//w w  w  .  j  a  v a2  s  .com
 *
 * @param rectangle
 *            rectangle to copy
 * @return rectangle copy
 */
public static Rectangle copy(final Rectangle rectangle) {
    return new Rectangle(rectangle);
}

From source file:Main.java

public Main() {
    try {// w ww.ja va  2 s.co m
        robot = new Robot();
    } catch (Exception e1) {
        e1.printStackTrace();
    }

    timer = new Timer(3000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Rectangle size = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            Image image = robot.createScreenCapture(size);
            label.setIcon(new ImageIcon(image));
            frame.setVisible(true);
        }
    });
    timer.setRepeats(false);

    button.addActionListener(e -> {
        frame.setVisible(false);
        timer.start();
    });

    frame.add(button, BorderLayout.NORTH);
    frame.add(label, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1024, 768);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Centers the given component over another component.
 * <p/>//from w  ww  .  ja  v  a  2s  .c  o m
 * <p> The method performs the alignment by setting a newly computed location for the component. It does not alter
 * the component's size.
 *
 * @param comp      the component whose location is to be altered
 * @param alignComp the component used for the alignment of the first component, if <code>null</code> the component
 *                  is ceneterd within the screen area
 *
 * @throws IllegalArgumentException if the component is <code>null</code>
 */
public static void centerComponent(Component comp, Component alignComp) {

    if (comp == null) {
        throw new IllegalArgumentException("comp must not be null");
    }

    Dimension compSize = comp.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    int x1, y1;

    if (alignComp != null && !new Rectangle(alignComp.getSize()).isEmpty()) {
        Point alignCompOffs = alignComp.getLocation();
        Dimension alignCompSize = alignComp.getSize();
        x1 = alignCompOffs.x + (alignCompSize.width - compSize.width) / 2;
        y1 = alignCompOffs.y + (alignCompSize.height - compSize.height) / 2;
    } else {
        x1 = (screenSize.width - compSize.width) / 2;
        y1 = (screenSize.height - compSize.height) / 2;
    }

    int x2 = x1 + compSize.width;
    int y2 = y1 + compSize.height;

    if (x2 >= screenSize.width) {
        x1 = screenSize.width - compSize.width - 1;
    }
    if (y2 >= screenSize.height) {
        y1 = screenSize.height - compSize.height - 1;
    }
    if (x1 < 0) {
        x1 = 0;
    }
    if (y1 < 0) {
        y1 = 0;
    }

    comp.setLocation(x1, y1);
}

From source file:Main.java

public static void scrollVertically(JComponent c, int from, int to, int bias) {
    Rectangle visible = c.getVisibleRect(), dest = new Rectangle(visible);

    dest.y = from;//  w  ww. ja va 2s . c  om
    dest.height = to - from;

    if (dest.height > visible.height) {
        if (bias == VIEWPORT) {
            // leave as is
        } else if (bias == UNCHANGED) {
            if (dest.y <= visible.y && dest.y + dest.height >= visible.y + visible.height) {
                dest.y = visible.y;
                dest.height = visible.height;
            }
        } else {
            if (bias == CENTER)
                dest.y += (dest.height - visible.height) / 2;
            else if (bias == LAST)
                dest.y += dest.height - visible.height;

            dest.height = visible.height;
        }
    }

    if (!visible.contains(dest))
        c.scrollRectToVisible(dest);
}

From source file:Main.java

public static void scrollHorizontally(JComponent c, int from, int to, int bias) {
    Rectangle visible = c.getVisibleRect(), dest = new Rectangle(visible);

    dest.x = from;/*  w ww.ja  va  2  s . co  m*/
    dest.width = to - from;

    if (dest.width > visible.width) {
        if (bias == VIEWPORT) {
            // leave as is
        } else if (bias == UNCHANGED) {
            if (dest.x <= visible.x && dest.x + dest.width >= visible.x + visible.width) {
                dest.x = visible.x;
                dest.width = visible.width;
            }
        } else {
            if (bias == CENTER)
                dest.x += (dest.width - visible.width) / 2;
            else if (bias == LAST)
                dest.x += dest.width - visible.width;

            dest.width = visible.width;
        }
    }

    if (!visible.contains(dest))
        c.scrollRectToVisible(dest);
}

From source file:GUIUtils.java

public static boolean isWithinParent(Component wind) {
    if (wind == null) {
        throw new IllegalArgumentException("Null Component passed");
    }/* ww  w.  jav  a  2 s  .c  o m*/

    Rectangle windowBounds = wind.getBounds();
    Component parent = wind.getParent();
    Rectangle parentRect = null;
    if (parent != null) {
        parentRect = new Rectangle(parent.getSize());
    } else {
        // parentRect = new
        // Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        parentRect = getScreenBoundsFor(windowBounds);
    }

    // if (windowBounds.x > (parentRect.width - 20)
    // || windowBounds.y > (parentRect.height - 20)
    // || (windowBounds.x + windowBounds.width) < 20
    // || (windowBounds.y + windowBounds.height) < 20)
    // {
    // return false;
    // }
    if (windowBounds.x < (parentRect.x - 20) || windowBounds.y < (parentRect.y - 20)) {
        return false;
    }
    return true;
}

From source file:Main.java

public PolygonButton(Polygon p, String text) {
    polygon = p;/*from w w w.  j  a  v  a  2  s .c  o m*/
    setText(text);

    setOpaque(false);

    addMouseListener(this);
    addMouseMotionListener(this);

    rectangle = new Rectangle(polygon.getBounds()); // Bug alert!
    rectangle.grow(1, 1);

    setBounds(rectangle);
    polygon.translate(-rectangle.x, -rectangle.y);
}

From source file:com.github.srec.rec.DefaultScreenShot.java

@Override
public String captureDesktop(String subdir, Robot robot) {
    log.info("Capturing desktop screenshot");
    Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    return capture(subdir, screenRect, robot);
}

From source file:com.qspin.qtaste.testapi.impl.generic.UtilityImpl.java

public void createScreenshot(String fileName) throws QTasteException {
    try {/*  w  w w.j  ava2s  .  c om*/
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(fileName));
    } catch (Exception e) {
        throw new QTasteException("Error in createScreenshot: " + e.getMessage());
    }
}