Java Code Examples for java.awt.Shape#getBounds()

The following examples show how to use java.awt.Shape#getBounds() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private static void draw(Shape clip, Shape shape, Image from, Image to) {
    Graphics2D g2d = (Graphics2D) to.getGraphics();
    g2d.setXORMode(Color.BLACK);
    g2d.setClip(clip);
    Rectangle toBounds = shape.getBounds();
    g2d.drawImage(from, toBounds.x, toBounds.y, toBounds.width,
                  toBounds.height, null);
    g2d.dispose();
}
 
Example 2
protected static Shape transformShape(int tx, int ty, Shape s) {
    if (s == null) {
        return null;
    }

    if (s instanceof Rectangle) {
        Rectangle r = s.getBounds();
        r.translate(tx, ty);
        return r;
    }
    if (s instanceof Rectangle2D) {
        Rectangle2D rect = (Rectangle2D) s;
        return new Rectangle2D.Double(rect.getX() + tx,
                                      rect.getY() + ty,
                                      rect.getWidth(),
                                      rect.getHeight());
    }

    if (tx == 0 && ty == 0) {
        return cloneShape(s);
    }

    AffineTransform mat = AffineTransform.getTranslateInstance(tx, ty);
    return mat.createTransformedShape(s);
}
 
Example 3
Source Project: ontopia   File: ColouredSquareMenuItem.java    License: Apache License 2.0 6 votes vote down vote up
/** Draw a coloured square at the end of the display. */
@Override
public void paintComponent(Graphics g) {
  super.paintComponent(g);

  Graphics2D g2 = (Graphics2D) g;

  Shape clip = g2.getClip();
  Rectangle bounds = clip.getBounds();
  double width = bounds.getWidth();
  double height = bounds.getHeight();

  g2.setColor(squareColor);
  g2.fill3DRect(((int) width - 25), 0, 25, (int) height, true);
  
  visibleIndicator.paintComponent(g);
}
 
Example 4
protected static Shape transformShape(int tx, int ty, Shape s) {
    if (s == null) {
        return null;
    }

    if (s instanceof Rectangle) {
        Rectangle r = s.getBounds();
        r.translate(tx, ty);
        return r;
    }
    if (s instanceof Rectangle2D) {
        Rectangle2D rect = (Rectangle2D) s;
        return new Rectangle2D.Double(rect.getX() + tx,
                                      rect.getY() + ty,
                                      rect.getWidth(),
                                      rect.getHeight());
    }

    if (tx == 0 && ty == 0) {
        return cloneShape(s);
    }

    AffineTransform mat = AffineTransform.getTranslateInstance(tx, ty);
    return mat.createTransformedShape(s);
}
 
Example 5
@Override
public void renderAttachments (Graphics2D g)
{
    if (attachments.isEmpty() || !ViewParameters.getInstance().isAttachmentPainting()) {
        return;
    }

    Color oldColor = g.getColor();
    g.setColor(Colors.ATTACHMENT);

    Font oldFont = g.getFont();
    g.setFont(oldFont.deriveFont(4f));

    for (Map.Entry<String, Shape> entry : attachments.entrySet()) {
        Shape shape = entry.getValue();
        g.draw(shape);

        String key = entry.getKey();
        Rectangle rect = shape.getBounds();
        g.drawString(key, rect.x + (rect.width / 2), rect.y + (rect.height / 2));
    }

    g.setFont(oldFont);
    g.setColor(oldColor);
}
 
Example 6
private static void draw(Shape clip, Shape to, Image vi, BufferedImage bi,
                         int scale) {
    Graphics2D big = bi.createGraphics();
    big.setComposite(AlphaComposite.Src);
    big.setClip(clip);
    Rectangle toBounds = to.getBounds();
    int x1 = toBounds.x;

    int y1 = toBounds.y;
    int x2 = x1 + toBounds.width;
    int y2 = y1 + toBounds.height;
    big.drawImage(vi, x1, y1, x2, y2, 0, 0, toBounds.width / scale,
                  toBounds.height / scale, null);
    big.dispose();
    vi.flush();
}
 
Example 7
protected static Shape transformShape(int tx, int ty, Shape s) {
    if (s == null) {
        return null;
    }

    if (s instanceof Rectangle) {
        Rectangle r = s.getBounds();
        r.translate(tx, ty);
        return r;
    }
    if (s instanceof Rectangle2D) {
        Rectangle2D rect = (Rectangle2D) s;
        return new Rectangle2D.Double(rect.getX() + tx,
                                      rect.getY() + ty,
                                      rect.getWidth(),
                                      rect.getHeight());
    }

    if (tx == 0 && ty == 0) {
        return cloneShape(s);
    }

    AffineTransform mat = AffineTransform.getTranslateInstance(tx, ty);
    return mat.createTransformedShape(s);
}
 
Example 8
protected static Shape transformShape(int tx, int ty, Shape s) {
    if (s == null) {
        return null;
    }

    if (s instanceof Rectangle) {
        Rectangle r = s.getBounds();
        r.translate(tx, ty);
        return r;
    }
    if (s instanceof Rectangle2D) {
        Rectangle2D rect = (Rectangle2D) s;
        return new Rectangle2D.Double(rect.getX() + tx,
                                      rect.getY() + ty,
                                      rect.getWidth(),
                                      rect.getHeight());
    }

    if (tx == 0 && ty == 0) {
        return cloneShape(s);
    }

    AffineTransform mat = AffineTransform.getTranslateInstance(tx, ty);
    return mat.createTransformedShape(s);
}
 
Example 9
protected Rectangle transformBounds(Rectangle rect,
                                    AffineTransform tx) {
    if (tx.isIdentity()) {
        return rect;
    }

    Shape s = transformShape(tx, rect);
    return s.getBounds();
}
 
Example 10
Source Project: netbeans   File: ViewHierarchyImpl.java    License: Apache License 2.0 5 votes vote down vote up
private double fallBackModelToY(int offset) {
    Shape s;
    try {
        s = textComponent.modelToView(offset);
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
        s = null;
    }

    if (s != null) {
        return s.getBounds().y;
    } else {
        return 0d;
    }
}
 
Example 11
private static void draw(Shape clip, Shape shape, Image from, Image to) {
    Graphics2D g2d = (Graphics2D) to.getGraphics();
    g2d.setXORMode(Color.BLACK);
    g2d.setClip(clip);
    Rectangle toBounds = shape.getBounds();
    g2d.drawImage(from, toBounds.x, toBounds.y, toBounds.width,
                  toBounds.height, null);
    g2d.dispose();
}
 
Example 12
private static void draw(Shape clip, Shape shape, Image from, Image to) {
    Graphics2D g2d = (Graphics2D) to.getGraphics();
    g2d.setXORMode(Color.BLACK);
    g2d.setClip(clip);
    Rectangle toBounds = shape.getBounds();
    g2d.drawImage(from, toBounds.x, toBounds.y, toBounds.width,
                  toBounds.height, null);
    g2d.dispose();
}
 
Example 13
/**
 * Enhanced constructor which takes bounds of the objects SHAPE to fill and the four
 * colors we need to create the bilinear interpolated gradient
 * @param SHAPE
 * @param COLOR_00
 * @param COLOR_10
 * @param COLOR_01
 * @param COLOR_11
 * @throws IllegalArgumentException
 */
public BiLinearGradientPaint(final Shape SHAPE, final Color COLOR_00,
                             final Color COLOR_10, final Color COLOR_01,
                             final Color COLOR_11) throws IllegalArgumentException {
    // Set the values
    this.BOUNDS = SHAPE.getBounds();
    this.COLOR_00 = COLOR_00;
    this.COLOR_10 = COLOR_10;
    this.COLOR_01 = COLOR_01;
    this.COLOR_11 = COLOR_11;
    this.FRACTION_X_STEPSIZE = 1.0f / (BOUNDS.getBounds().width);
    this.FRACTION_Y_STEPSIZE = 1.0f / (BOUNDS.getBounds().height);
    this.titleBarHeight = -1;
}
 
Example 14
Source Project: Bytecoder   File: SunGraphics2D.java    License: Apache License 2.0 5 votes vote down vote up
protected Rectangle transformBounds(Rectangle rect,
                                    AffineTransform tx) {
    if (tx.isIdentity()) {
        return rect;
    }

    Shape s = transformShape(tx, rect);
    return s.getBounds();
}
 
Example 15
protected Rectangle transformBounds(Rectangle rect,
                                    AffineTransform tx) {
    if (tx.isIdentity()) {
        return rect;
    }

    Shape s = transformShape(tx, rect);
    return s.getBounds();
}
 
Example 16
Source Project: seaglass   File: Effect.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Paint the effect based around a solid shape in the graphics supplied.
 *
 * @param g the graphics to paint into.
 * @param s the shape to base the effect around.
 */
public void fill(Graphics2D g, Shape s) {
    Rectangle bounds = s.getBounds();
    int       width  = bounds.width;
    int       height = bounds.height;

    BufferedImage bimage = Effect.createBufferedImage(width, height, true);
    Graphics2D    gbi    = bimage.createGraphics();

    gbi.setColor(Color.BLACK);
    gbi.fill(s);

    g.drawImage(applyEffect(bimage, null, width, height), 0, 0, null);
}
 
Example 17
protected Rectangle transformBounds(Rectangle rect,
                                    AffineTransform tx) {
    if (tx.isIdentity()) {
        return rect;
    }

    Shape s = transformShape(tx, rect);
    return s.getBounds();
}
 
Example 18
/**
 * Provides a mapping from the document model coordinate space
 * to the coordinate space of the view mapped to it.
 *
 * @param pos the position to convert
 * @param a the allocated region to render into
 * @return the bounding box of the given position
 * @exception BadLocationException  if the given position does not represent a
 *   valid location in the associated document
 * @see View#modelToView
 */
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
    int p0 = getStartOffset();
    int p1 = getEndOffset();
    if ((pos >= p0) && (pos <= p1)) {
        Rectangle r = a.getBounds();
        if (pos == p1) {
            r.x += r.width;
        }
        r.width = 0;
        return r;
    }
    return null;
}
 
Example 19
/**
 * Paints a highlight.
 *
 * @param g the graphics context
 * @param offs0 the starting model offset >= 0
 * @param offs1 the ending model offset >= offs1
 * @param bounds the bounding box for the highlight
 * @param c the editor
 */
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
    Rectangle alloc = bounds.getBounds();
    try {
        // --- determine locations ---
        TextUI mapper = c.getUI();
        Rectangle p0 = mapper.modelToView(c, offs0);
        Rectangle p1 = mapper.modelToView(c, offs1);

        // --- render ---
        Color color = getColor();

        if (color == null) {
            g.setColor(c.getSelectionColor());
        }
        else {
            g.setColor(color);
        }
        boolean firstIsDot = false;
        boolean secondIsDot = false;
        if (c.isEditable()) {
            int dot = c.getCaretPosition();
            firstIsDot = (offs0 == dot);
            secondIsDot = (offs1 == dot);
        }
        if (p0.y == p1.y) {
            // same line, render a rectangle
            Rectangle r = p0.union(p1);
            if (r.width > 0) {
                if (firstIsDot) {
                    r.x++;
                    r.width--;
                }
                else if (secondIsDot) {
                    r.width--;
                }
            }
            g.fillRect(r.x, r.y, r.width, r.height);
        } else {
            // different lines
            int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
            if (firstIsDot && p0ToMarginWidth > 0) {
                p0.x++;
                p0ToMarginWidth--;
            }
            g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
            if ((p0.y + p0.height) != p1.y) {
                g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
                           p1.y - (p0.y + p0.height));
            }
            if (secondIsDot && p1.x > alloc.x) {
                p1.x--;
            }
            g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
        }
    } catch (BadLocationException e) {
        // can't render
    }
}
 
Example 20
@Override
public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
    // PENDING(prinz) properly calculate bias
    bias[0] = Position.Bias.Forward;

    Rectangle alloc = a.getBounds();
    Document doc = getDocument();
    int x = (int) fx;
    int y = (int) fy;
    if (y < alloc.y) {
        // above the area covered by this icon, so the the position
        // is assumed to be the start of the coverage for this view.
        return getStartOffset();
    } else if (y > alloc.y + alloc.height) {
        // below the area covered by this icon, so the the position
        // is assumed to be the end of the coverage for this view.
        return getEndOffset() - 1;
    } else {
        // positioned within the coverage of this view vertically,
        // so we figure out which line the point corresponds to.
        // if the line is greater than the number of lines contained, then
        // simply use the last line as it represents the last possible place
        // we can position to.
      
        Element map = doc.getDefaultRootElement();
        int fontHeight = metrics.getHeight();
        int lineIndex = (fontHeight > 0 ?
                            Math.abs((y - alloc.y) / fontHeight) :
                            map.getElementCount() - 1);
        if (lineIndex >= map.getElementCount()) {
            return getEndOffset() - 1;
        }
        Element line = map.getElement(lineIndex);
        int dx = 0;
        if (lineIndex == 0) {
            //alloc.x += firstLineOffset;
           // alloc.width -= firstLineOffset;
        }
        if (x < alloc.x) {
            // point is to the left of the line
            return line.getStartOffset();
        } else if (x > alloc.x + alloc.width) {
            // point is to the right of the line
            return line.getEndOffset() - 1;
        } else {
            // Determine the offset into the text
            try {
                int p0 = line.getStartOffset();
                int p1 = line.getEndOffset() - 1;
                Segment s = new Segment();
                doc.getText(p0, p1 - p0, s);
                int tabBase = alloc.x;
                int offs = p0 + UniTools.getTabbedTextOffset(s, metrics,
                                                              tabBase, x, this, p0);
                //SegmentCache.releaseSharedSegment(s);
                return offs;
            } catch (BadLocationException e) {
                // should not happen
                return -1;
            }
        }
    }
}