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(int x, int y, int width, int height) 

Source Link

Document

Constructs a new Rectangle whose upper-left corner is specified as (x,y) and whose width and height are specified by the arguments of the same name.

Usage

From source file:ClippedDragImage.java

private Rectangle getAffectedArea(int oldx, int oldy, int newx, int newy, int width, int height) {
    int x = Math.min(oldx, newx);
    int y = Math.min(oldy, newy);
    int w = (Math.max(oldx, newx) + width) - x;
    int h = (Math.max(oldy, newy) + height) - y;
    return new Rectangle(x, y, w, h);
}

From source file:ClipBetweenRectangleEllipse2D.java

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

    Graphics2D g2 = (Graphics2D) g;

    int w = getSize().width;
    int h = getSize().height;
    Ellipse2D e = new Ellipse2D.Float(w / 4.0f, h / 4.0f, w / 2.0f, h / 2.0f);
    g2.setClip(e);/*from   w w  w .j a  v  a  2  s . c  o  m*/

    g2.setColor(Color.red);
    g2.fillRect(0, 0, w, h);

    Rectangle r = new Rectangle(w / 2, h / 2, w / 2, h / 2);
    g2.clip(r);

    g2.setColor(Color.green);
    g2.fillRect(0, 0, w, h);

}

From source file:Main.java

/**
 *  Updates the passed window's position to center it with respect to the
 *  screen. May be called before or after the window is made visible (but
 *  remember to call <code>pack()</code> first!).
 *  <p>//w w  w . j  a va  2 s  . c  o  m
 *  Deals with multi-monitor setups via the following hack: if the screen
 *  size reported by the default toolkit has a width:height ration > 2:1,
 *  then the width is divided by 2. This works well for 1, 2, or 3 screen
 *  desktops: the window will appear in the left screen of a 2-screen
 *  setup, in the middle of a 3-screen setup.
 *  <p>
 *  If the window is larger than the screen size, it's positioned at the
 *  top-left corner. Hopefully the user will be able to shrink it.
 */
public static void center(Window window) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int width = screenSize.width;
    int height = screenSize.height;
    if (width > height * 2) {
        width /= 2;
    }

    center(window, new Rectangle(0, 0, width, height), false);
}

From source file:Main.java

/**
 * Returns component size represented as a rectangle with zero X and Y coordinates.
 *
 * @param component component to process
 * @return component size rectangle//from   w w  w.  ja  v  a  2  s .  c  om
 */
public static Rectangle size(final Component component) {
    return new Rectangle(0, 0, component.getWidth(), component.getHeight());
}

From source file:Main.java

/**
 * Implements autoscrolling.//w  ww.ja  v a  2 s  .  c o m
 *
 * @param comp
 *          the component
 * @param cursorLocn
 *          the cursor location
 */
public static void defaultAutoScroll(JComponent comp, Point cursorLocn) {
    Rectangle visible = comp.getVisibleRect();
    int x = 0, y = 0, width = 0, height = 0;

    // Scroll left.
    if (cursorLocn.x < visible.x + AUTOSCROLL_INSET_SIZE) {
        x = -SCROLL_AMOUNT;
        width = SCROLL_AMOUNT;
    } // Scroll right.
    else if (cursorLocn.x > visible.x + visible.width - AUTOSCROLL_INSET_SIZE) {
        x = visible.width + SCROLL_AMOUNT;
        width = SCROLL_AMOUNT;
    }

    // Scroll up.
    if (cursorLocn.y < visible.y + AUTOSCROLL_INSET_SIZE) {
        y = -SCROLL_AMOUNT;
        height = SCROLL_AMOUNT;
    } // Scroll down.
    else if (cursorLocn.y > visible.y + visible.height - AUTOSCROLL_INSET_SIZE) {
        y = visible.height + SCROLL_AMOUNT;
        height = SCROLL_AMOUNT;
    }

    ((JComponent) comp.getParent()).scrollRectToVisible(new Rectangle(x, y, width, height));
}

From source file:MainClass.java

public void paint(Graphics g) {
    BufferedImage bim;/*from   w  w w .  ja  v a  2s. co  m*/

    TexturePaint tp;

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}

From source file:Main.java

public void paint(Graphics g) {
    BufferedImage bim;/*from   ww w  .  j  a v a 2s .  c o  m*/

    TexturePaint tp;

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
    System.out.println(tp.getAnchorRect());

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}

From source file:Main.java

public void paint(Graphics g) {
    BufferedImage bim;/*ww w  . j a v a2  s  .co m*/

    TexturePaint tp;

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
    System.out.println(tp.getImage());

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}

From source file:Main.java

public void paint(Graphics g) {
    BufferedImage bim;// w  w  w. j  av a  2 s  .  c o  m

    TexturePaint tp;

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
    System.out.println(tp.getTransparency());

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}

From source file:Main.java

public void paint(Graphics g) {
    BufferedImage bim;//  w  ww.  j a v  a2 s  .  c  o  m

    TexturePaint tp;

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
    tp.createContext(ColorModel.getRGBdefault(), new Rectangle(10, 10, 20, 20), null, null, null);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}