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:WatermarkTextField.java

public WatermarkTextField(File file) {
    super();/*  w  w  w .j a  va  2s  .c o  m*/
    try {
        img = ImageIO.read(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Rectangle rect = new Rectangle(0, 0, img.getWidth(null), img.getHeight(null));
    texture = new TexturePaint(img, rect);
    setOpaque(false);
}

From source file:Textures.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    TexturePaint slatetp = new TexturePaint(s, new Rectangle(0, 0, 90, 60));

    g2d.setPaint(slatetp);/*from  w  w w  .  j  a  v a  2  s  . c  o  m*/
    g2d.fillRect(10, 15, 90, 60);

}

From source file:DashedStrokeDemo.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    float dash[] = { 10.0f };
    g2.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f));

    g2.setPaint(Color.blue);//w ww. j  av  a 2s .  c  o m

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

    g2.draw(r);
}

From source file:Main.java

public static Rectangle calculateBoundsForComponent(JComponent comp, String text, int xOffset, int yOffset) {

    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    Rectangle2D bounds = fm.getStringBounds(text, comp.getGraphics());

    return new Rectangle(xOffset + (int) bounds.getX(), yOffset,
            (int) Math.ceil(bounds.getWidth()) + (PADDING * 2),
            (int) Math.ceil(bounds.getHeight() + fm.getDescent()) + (PADDING * 2));
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    float strokeThickness = 5.0f;

    BasicStroke stroke = new BasicStroke(strokeThickness);
    g2d.setStroke(stroke);//w  w w .  j  a v a  2s. com

    g2d.draw(new Rectangle(20, 20, 200, 200));

}

From source file:Shear.java

public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;

    AffineTransform tx1 = new AffineTransform();
    tx1.translate(50, 90);//from w  w w.  jav  a 2  s  .c  o m

    g2d.setTransform(tx1);
    g2d.setColor(Color.green);
    g2d.drawRect(0, 0, 80, 50);

    AffineTransform tx2 = new AffineTransform();
    tx2.translate(50, 90);
    tx2.shear(0, 1);

    g2d.setTransform(tx2);
    g2d.setColor(Color.blue);

    g2d.draw(new Rectangle(0, 0, 80, 50));

}

From source file:Main.java

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

    Ellipse2D e1 = new Ellipse2D.Double(20.0, 20.0, 80.0, 70.0);
    Ellipse2D e2 = new Ellipse2D.Double(20.0, 70.0, 40.0, 40.0);

    Area a1 = new Area(e1);
    Area a2 = new Area(e2);

    a1.subtract(a2);/* w  w w  .j  ava  2s.  c o  m*/

    g2.setColor(Color.orange);
    g2.fill(a1);

    g2.setColor(Color.black);
    g2.drawString("subtract", 20, 140);

    System.out.println(a1.contains(new Rectangle(50, 50, 5, 5)));
}

From source file:GraphicsUtil.java

static public Rectangle getTextBounds(Graphics g, String text, int x, int y, int halign, int valign) {
    if (g == null)
        return new Rectangle(x, y, 0, 0);
    FontMetrics mets = g.getFontMetrics();
    int width = mets.stringWidth(text);
    int ascent = mets.getAscent();
    int height = ascent + mets.getDescent();
    Rectangle ret = new Rectangle(x, y, width, height);

    switch (halign) {
    case H_CENTER:
        ret.translate(-(width / 2), 0);//from  w  w  w. j ava  2s  .c om
        break;
    case H_RIGHT:
        ret.translate(-width, 0);
        break;
    default:
        ;
    }
    switch (valign) {
    case V_TOP:
        break;
    case V_CENTER:
        ret.translate(0, -(ascent / 2));
        break;
    case V_BASELINE:
        ret.translate(0, -ascent);
        break;
    case V_BOTTOM:
        ret.translate(0, -height);
        break;
    default:
        ;
    }
    return ret;
}

From source file:SlidePuzzle.java

public void init() {
    //Define and populate Rectangle array
    squares = new Rectangle[9];
    squares[0] = new Rectangle(0, 0, 60, 60);
    squares[1] = new Rectangle(0, 60, 60, 60);
    squares[2] = new Rectangle(0, 120, 60, 60);
    squares[3] = new Rectangle(60, 0, 60, 60);
    squares[4] = new Rectangle(60, 60, 60, 60);
    squares[5] = new Rectangle(60, 120, 60, 60);
    squares[6] = new Rectangle(120, 0, 60, 60);
    squares[7] = new Rectangle(120, 60, 60, 60);
    squares[8] = new Rectangle(120, 120, 60, 60);

    //Define and populate int array
    order = new int[9];
    order[0] = 6;/*  w w w .  j a va  2s  .  c  om*/
    order[1] = 2;
    order[2] = 8;
    order[3] = 1;
    order[4] = 0;
    order[5] = 7;
    order[6] = 5;
    order[7] = 4;
    order[8] = 3;
}

From source file:Main.java

public static Rectangle parseRectangle(String s) {
    if (rectanglePattern == null)
        rectanglePattern = Pattern.compile("\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*");

    Matcher m = rectanglePattern.matcher(s);

    if (m.matches()) {
        return new Rectangle(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)),
                Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4)));
    }//  ww  w .j  a  v  a2s  .c  om
    return null;
}