Example usage for java.awt Shape getBounds

List of usage examples for java.awt Shape getBounds

Introduction

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

Prototype

public Rectangle getBounds();

Source Link

Document

Returns an integer Rectangle that completely encloses the Shape .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Shape s = new Rectangle2D.Double(0, 0, 72, 72);

    System.out.println(s.getBounds());
}

From source file:GraphicsUtil.java

public static Rectangle calculateIntersectionClip(int x, int y, int width, int height, Shape originalClip) {
    Rectangle bounds = originalClip.getBounds();
    SwingUtilities.computeIntersection(x, y, width, height, bounds);
    return bounds;
}

From source file:Main.java

/**
 * Fills a gradient using the start color and end color specified.
 *
 * @param g//from  w  ww .  ja  v  a 2  s.  co m
 *          the gradient
 * @param s
 *          the shape to fill
 * @param start
 *          the start color
 * @param end
 *          the end color
 * @param vertical
 *          true for a vertical gradient; false for horizontal
 */
public static void fillGradient(Graphics2D g, Shape s, Color start, Color end, boolean vertical) {
    Rectangle r = s.getBounds();
    Paint gp = new GradientPaint(0, 0, start, vertical ? 0 : r.width, vertical ? r.height : 0, end);
    Object o = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setPaint(gp);
    g.fill(s);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, o);
}

From source file:Main.java

public static Point arrangeWithin(final Shape shapeToArrange, final Rectangle window, final int arrangement,
        Insets padding) {//  w ww  . j  a va2s  .c  om
    if (shapeToArrange == null)
        throw new IllegalArgumentException("Parameter 'shapeToArrange' must not be null!");
    if (window == null)
        throw new IllegalArgumentException("Parameter 'window' must not be null!");
    if (padding == null)
        padding = new Insets(0, 0, 0, 0);

    final Rectangle bounds = shapeToArrange.getBounds();

    switch (arrangement) {
    case SwingConstants.NORTH:
        return new Point((window.width - bounds.width) / 2, padding.top);
    case SwingConstants.NORTH_EAST:
        return new Point(window.width - padding.right, padding.top);
    case SwingConstants.EAST:
        return new Point(window.width - padding.right, (window.height - bounds.height) / 2);
    case SwingConstants.SOUTH_EAST:
        return new Point(window.width - padding.right, window.height - padding.bottom);
    case SwingConstants.SOUTH:
        return new Point((window.width - bounds.width) / 2, window.height - padding.bottom);
    case SwingConstants.SOUTH_WEST:
        return new Point(padding.left, window.height - padding.bottom);
    case SwingConstants.WEST:
        return new Point(padding.left, (window.height - bounds.height) / 2);
    case SwingConstants.NORTH_WEST:
        return new Point(padding.left, padding.top);
    case SwingConstants.CENTER:
        return new Point((window.width - bounds.width) / 2, (window.height - bounds.height) / 2);
    default:
        throw new IllegalArgumentException("Illegal arrangement key, expected one of the SwingConstants keys");
    }
}

From source file:Hexagon.java

public static File write(String fileName, Shape shape) throws IOException {
    Rectangle bounds = shape.getBounds();
    BufferedImage img = createImage(bounds.width, bounds.height);
    Graphics2D g2 = (Graphics2D) img.createGraphics();
    //g2.setColor(WebColor.CornSilk.getColor());
    g2.fill(shape);//from   w ww  .ja v a 2 s  .  c o  m
    g2.setColor(Color.black);
    g2.draw(shape);
    return write(fileName, img);
}

From source file:FontPaint.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);/*  w ww. j a v  a 2s  .  c o m*/
    int width = getSize().width;
    int height = getSize().height;
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    FontRenderContext frc = g2.getFontRenderContext();
    Font f = new Font("Helvetica", 1, 60);
    String s = new String("Java Source and Support.");
    TextLayout textTl = new TextLayout(s, f, frc);
    AffineTransform transform = new AffineTransform();
    Shape outline = textTl.getOutline(null);
    Rectangle outlineBounds = outline.getBounds();
    transform = g2.getTransform();
    transform.translate(width / 2 - (outlineBounds.width / 2), height / 2 + (outlineBounds.height / 2));
    g2.transform(transform);
    g2.setColor(Color.blue);
    g2.draw(outline);
    g2.setClip(outline);
}

From source file:LineHighlightPainter.java

public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {

    Rectangle r0 = null, r1 = null, rbounds = bounds.getBounds();
    int xmax = rbounds.x + rbounds.width;
    try {/*from   www. j a  v  a 2  s .  c  o m*/
        r0 = c.modelToView(p0);
        r1 = c.modelToView(p1);
    } catch (BadLocationException ex) {
        return;
    }
    if ((r0 == null) || (r1 == null))
        return;

    g.setColor(c.getSelectionColor());

    // special case if p0 and p1 are on the same line
    if (r0.y == r1.y) {
        paintLine(g, r0, r1.x);
        return;
    }

    // first line, from p1 to end-of-line
    paintLine(g, r0, xmax);

    // all the full lines in between, if any (assumes that all lines have
    // the same height--not a good assumption with JEditorPane/JTextPane)
    r0.y += r0.height; // move r0 to next line
    r0.x = rbounds.x; // move r0 to left edge
    while (r0.y < r1.y) {
        paintLine(g, r0, xmax);
        r0.y += r0.height; // move r0 to next line
    }

    // last line, from beginning-of-line to p1
    paintLine(g, r0, r1.x);
}

From source file:LineHighlightPainter.java

public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {

    Rectangle r0 = null, r1 = null, rbounds = bounds.getBounds();
    int xmax = rbounds.x + rbounds.width; // x coordinate of right edge
    try { // convert positions to pixel coordinates
        r0 = c.modelToView(p0);//  w  w  w. j  a v  a 2  s  .  c o  m
        r1 = c.modelToView(p1);
    } catch (BadLocationException ex) {
        return;
    }
    if ((r0 == null) || (r1 == null))
        return;

    g.setColor(c.getSelectionColor());

    // special case if p0 and p1 are on the same line
    if (r0.y == r1.y) {
        paintLine(g, r0, r1.x);
        return;
    }

    // first line, from p1 to end-of-line
    paintLine(g, r0, xmax);

    // all the full lines in between, if any (assumes that all lines have
    // the same height--not a good assumption with JEditorPane/JTextPane)
    r0.y += r0.height; // move r0 to next line
    r0.x = rbounds.x; // move r0 to left edge
    while (r0.y < r1.y) {
        paintLine(g, r0, xmax);
        r0.y += r0.height; // move r0 to next line
    }

    // last line, from beginning-of-line to p1
    paintLine(g, r0, r1.x);
}

From source file:Starry.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);/*from  ww  w .j  a  va2 s .  c  o m*/
    w = getSize().width;
    h = getSize().height;
    Graphics2D g2;
    g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    FontRenderContext frc = g2.getFontRenderContext();
    Font f = new Font("Helvetica", 1, w / 10);
    String s = new String("The Starry Night");
    TextLayout textTl = new TextLayout(s, f, frc);
    AffineTransform transform = new AffineTransform();
    Shape outline = textTl.getOutline(null);
    Rectangle r = outline.getBounds();
    transform = g2.getTransform();
    transform.translate(w / 2 - (r.width / 2), h / 2 + (r.height / 2));
    g2.transform(transform);
    g2.setColor(Color.blue);
    g2.draw(outline);
    g2.setClip(outline);
    g2.drawImage(img, r.x, r.y, r.width, r.height, this);

}

From source file:Main.java

public void paintStrikeLine(Graphics g, Shape a) {
    Color c = (Color) getElement().getAttributes().getAttribute("strike-color");
    if (c != null) {
        int y = a.getBounds().y + a.getBounds().height - (int) getGlyphPainter().getDescent(this);
        y = y - (int) (getGlyphPainter().getAscent(this) * 0.3f);
        int x1 = (int) a.getBounds().getX();
        int x2 = (int) (a.getBounds().getX() + a.getBounds().getWidth());

        Color old = g.getColor();
        g.setColor(c);/*www . jav  a2 s .com*/
        g.drawLine(x1, y, x2, y);
        g.setColor(old);
    }
}