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:com.igormaznitsa.jhexed.values.HexSVGImageValue.java

@Override
public void prerasterizeIcon(final Shape shape) {
    try {//from ww  w.  java 2 s.  c o  m
        final Rectangle r = shape.getBounds();
        final BufferedImage img = this.image.rasterize(r.width, r.height, BufferedImage.TYPE_INT_ARGB);
        final BufferedImage result = new BufferedImage(r.width, r.height, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g = result.createGraphics();
        g.setClip(shape);
        g.drawImage(img, 0, 0, null);
        g.dispose();
        this.prerasterized = result;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public void paint(Graphics g, Shape a) {
    super.paint(g, a);
    Color c = (Color) getElement().getAttributes().getAttribute("Underline-Color");
    if (c != null) {
        int y = a.getBounds().y + (int) getGlyphPainter().getAscent(this);
        int x1 = a.getBounds().x;
        int x2 = a.getBounds().width + x1;

        g.setColor(c);/*from  w w  w. j av a2 s .c  o  m*/
        g.drawLine(x1, y, x2, y);
    }
}

From source file:edu.uci.ics.jung.visualization.decorators.VertexIconShapeTransformer.java

/**
 * get the shape from the image. If not available, get
 * the shape from the delegate VertexShapeFunction
 *///w  w  w  . j a v a 2 s .  co  m
public Shape transform(V v) {
    Icon icon = iconMap.get(v);
    if (icon != null && icon instanceof ImageIcon) {
        Image image = ((ImageIcon) icon).getImage();
        Shape shape = (Shape) shapeMap.get(image);
        if (shape == null) {
            shape = FourPassImageShaper.getShape(image, 30);
            if (shape.getBounds().getWidth() > 0 && shape.getBounds().getHeight() > 0) {
                // don't cache a zero-sized shape, wait for the image
                // to be ready
                int width = image.getWidth(null);
                int height = image.getHeight(null);
                AffineTransform transform = AffineTransform.getTranslateInstance(-width / 2, -height / 2);
                shape = transform.createTransformedShape(shape);
                shapeMap.put(image, shape);
            }
        }
        return shape;
    } else {
        return delegate.transform(v);
    }
}

From source file:DrawShapes_2008.java

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    // Paint a gradient for the sky
    GradientPaint background = new GradientPaint(0f, 0f, Color.GRAY.darker(), 0f, (float) getHeight(),
            Color.GRAY.brighter());
    g2d.setPaint(background);/*  w w w . j  av  a 2s . c om*/
    g2d.fillRect(0, 0, getWidth(), 4 * getHeight() / 5);

    // Paint a gradient for the ground
    background = new GradientPaint(0f, (float) 4 * getHeight() / 5, Color.BLACK, 0f, (float) getHeight(),
            Color.GRAY.darker());
    g2d.setPaint(background);
    g2d.fillRect(0, 4 * getHeight() / 5, getWidth(), getHeight() / 5);

    // Enable anti-aliasing to get smooth outlines
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Iterate through all of the current shapes
    for (Shape shape : shapes) {
        // Get the bounds to compute the RadialGradient properties
        Rectangle rect = shape.getBounds();
        Point2D center = new Point2D.Float(rect.x + (float) rect.width / 2.0f,
                rect.y + (float) rect.height / 2.0f);
        float radius = (float) rect.width / 2.0f;
        float[] dist = { 0.1f, 0.9f };
        Color[] colors = { Color.WHITE, Color.BLACK };

        // Create and set a RadialGradient centered on the object,
        // going from white at the center to black at the edges
        RadialGradientPaint paint = new RadialGradientPaint(center, radius, dist, colors);
        g2d.setPaint(paint);

        // Finally, render our shape
        g2d.fill(shape);
    }
}

From source file:com.rapidminer.gui.graphs.ShapeBasedTreeLayout.java

private double calculateWidth(V v) {
    double childrenWidthSum = 0;
    int childrenNum = graph.getSuccessors(v).size();
    if (childrenNum != 0) {
        boolean first = true;
        for (V element : graph.getSuccessors(v)) {
            if (!first) {
                childrenWidthSum += MARGIN;
            }/*  www .ja  va 2  s.  c om*/
            childrenWidthSum += calculateWidth(element);
            first = false;
        }
    }

    double width = DEFAULT_WIDTH;
    if (this.shapeTransformer != null) {
        Shape shape = this.shapeTransformer.transform(v);
        if (shape != null) {
            width = shape.getBounds().getWidth();
        }
    }
    double size = Math.max(width, childrenWidthSum);
    size = Math.max(0, size);
    return size;
}

From source file:ExtendedParagraphExample.java

public void paint(Graphics g, Shape a) {
    Container comp = getContainer();
    Rectangle alloc = new Rectangle(a.getBounds());

    alloc.x += paraInsets.left;/*from   w  w w  . ja va 2  s . c o m*/
    alloc.y += paraInsets.top;
    alloc.width -= paraInsets.left + paraInsets.right;
    alloc.height -= paraInsets.top + paraInsets.bottom;

    if (bgColor != null) {
        Color origColor = g.getColor();
        g.setColor(bgColor);
        g.fillRect(alloc.x, alloc.y, alloc.width, alloc.height);
        g.setColor(origColor);
    }

    if (border != null) {
        // Paint the border
        border.paintBorder(comp, g, alloc.x, alloc.y, alloc.width, alloc.height);
    }
    super.paint(g, a); // Note: pass ORIGINAL allocation
}

From source file:Main.java

@Override
public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) {
    g.setColor(color == null ? c.getSelectionColor() : color);
    Rectangle rect = null;/*from ww  w. j  ava2  s  .c o m*/
    if (offs0 == view.getStartOffset() && offs1 == view.getEndOffset()) {
        if (bounds instanceof Rectangle) {
            rect = (Rectangle) bounds;
        } else {
            rect = bounds.getBounds();
        }
    } else {
        try {
            Shape shape = view.modelToView(offs0, Position.Bias.Forward, offs1, Position.Bias.Backward, bounds);
            rect = (shape instanceof Rectangle) ? (Rectangle) shape : shape.getBounds();
        } catch (BadLocationException e) {
            return null;
        }
    }
    FontMetrics fm = c.getFontMetrics(c.getFont());
    int baseline = rect.y + rect.height - fm.getDescent() + 1;
    g.drawLine(rect.x, baseline, rect.x + rect.width, baseline);
    g.drawLine(rect.x, baseline + 1, rect.x + rect.width, baseline + 1);
    return rect;
}

From source file:org.apache.jetspeed.security.mfa.impl.CaptchaImageResource.java

/**
 * Renders this image//from   w ww . j av  a2s.  c om
 * 
 * @return The image data
 */
private final byte[] render() throws IOException {
    Graphics2D gfx = (Graphics2D) this.image.getGraphics();
    if (config.isFontAntialiasing())
        gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    int curWidth = config.getTextMarginLeft();
    FontRenderContext ctx = new FontRenderContext(null, config.isFontAntialiasing(), false);
    for (int i = 0; i < charAttsList.size(); i++) {
        CharAttributes cf = (CharAttributes) charAttsList.get(i);
        TextLayout text = new TextLayout(cf.getChar() + "", getFont(cf.getName()), ctx); //gfx.getFontRenderContext());
        AffineTransform textAt = new AffineTransform();
        textAt.translate(curWidth, this.height - cf.getRise());
        if (cf.getRotation() != 0) {
            textAt.rotate(cf.getRotation());
        }
        if (cf.getShearX() > 0.0)
            textAt.shear(cf.getShearX(), cf.getShearY());
        Shape shape = text.getOutline(textAt);
        curWidth += shape.getBounds().getWidth() + config.getTextSpacing();
        if (config.isUseImageBackground())
            gfx.setColor(Color.BLACK);
        else
            gfx.setXORMode(Color.BLACK);
        gfx.fill(shape);
    }
    if (config.isEffectsNoise()) {
        noiseEffects(gfx, image);
    }
    if (config.isUseTimestamp()) {
        if (config.isEffectsNoise())
            gfx.setColor(Color.WHITE);
        else
            gfx.setColor(Color.BLACK);

        TimeZone tz = TimeZone.getTimeZone(config.getTimestampTZ());
        Calendar cal = new GregorianCalendar(tz);
        SimpleDateFormat formatter;
        if (config.isUseTimestamp24hr())
            formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");
        else
            formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a, z");
        formatter.setTimeZone(tz);
        Font font = gfx.getFont();
        Font newFont = new Font(font.getName(), font.getStyle(), config.getTimestampFontSize());
        gfx.setFont(newFont);
        gfx.drawString(formatter.format(cal.getTime()), config.getTextMarginLeft() * 4, this.height - 1);
    }

    return toImageData(image);
}

From source file:FormattedTextFieldExample.java

public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
    a = adjustAllocation(a);//from   ww w  . j  a v  a  2 s  . c  o  m
    Rectangle r = new Rectangle(a.getBounds());
    FontMetrics fm = getFontMetrics();
    r.height = fm.getHeight();

    int oldCount = contentBuff.count;

    if (pos < offsets.length) {
        contentBuff.count = offsets[pos];
    } else {
        // Beyond the end: point to the location
        // after the last model position.
        contentBuff.count = offsets[offsets.length - 1] + 1;
    }

    int offset = Utilities.getTabbedTextWidth(contentBuff, metrics, 0, this, element.getStartOffset());
    contentBuff.count = oldCount;

    r.x += offset;
    r.width = 1;

    return r;
}

From source file:savant.view.tracks.Track.java

/**
 * Given a record, determine the bounds which would be used for displaying
 * that record./*from   w ww  .  ja  v a 2  s.com*/
 *
 * @param rec the record whose bounds we're interested in
 * @return the record's bounds in pixels, relative to the track's bounds (or
 * null
 */
@Override
public Rectangle getRecordBounds(Record rec) {
    Shape s = renderer.recordToShapeMap.get(rec);
    if (s != null) {
        return s.getBounds();
    }
    return null;
}