Example usage for java.awt Graphics2D draw

List of usage examples for java.awt Graphics2D draw

Introduction

In this page you can find the example usage for java.awt Graphics2D draw.

Prototype

public abstract void draw(Shape s);

Source Link

Document

Strokes the outline of a Shape using the settings of the current Graphics2D context.

Usage

From source file:CustomStrokes.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    // Get a shape to work with. Here we'll use the letter B
    Font f = new Font("Serif", Font.BOLD, 200);
    GlyphVector gv = f.createGlyphVector(g.getFontRenderContext(), "B");
    Shape shape = gv.getOutline();

    // Set drawing attributes and starting position
    g.setColor(Color.black);/*from w  ww.  j  av  a  2s .  c o  m*/
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.translate(10, 175);

    // Draw the shape once with each stroke
    for (int i = 0; i < strokes.length; i++) {
        g.setStroke(strokes[i]); // set the stroke
        g.draw(shape); // draw the shape
        g.translate(140, 0); // move to the right
    }
}

From source file:nl.b3p.viewer.image.ImageTool.java

public static BufferedImage drawGeometries(BufferedImage bi, CombineImageSettings settings, int srid, Bbox bbox,
        int width, int height) throws Exception {
    List wktGeoms = settings.getWktGeoms();
    if (wktGeoms == null || wktGeoms.size() <= 0) {
        return bi;
    }/*from  w ww.  j  a  v a  2  s .  com*/
    BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
    //        BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D gbi = newBufIm.createGraphics();
    gbi.drawImage(bi, 0, 0, null);
    for (int i = 0; i < wktGeoms.size(); i++) {
        gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        CombineImageWkt ciw = (CombineImageWkt) wktGeoms.get(i);
        Color color = settings.getDefaultWktGeomColor();
        if (ciw.getColor() != null) {
            color = ciw.getColor();
        }
        gbi.setColor(color);
        String wktGeom = ciw.getWktGeom();
        Geometry geom = geometrieFromText(wktGeom, srid);
        Shape shape = createImage(geom, srid, bbox, width, height);
        Point centerPoint = null;
        if (geom instanceof Polygon) {
            gbi.fill(shape);
        } else if (geom instanceof com.vividsolutions.jts.geom.Point) {
            centerPoint = calculateCenter(shape, srid, bbox, width, height);
            gbi.fill(new Ellipse2D.Double(centerPoint.getX(), centerPoint.getY(), 8, 8));
        } else {
            float strokeWidth = ciw.getStrokeWidth() != null ? ciw.getStrokeWidth() : 3f;
            gbi.setStroke(new BasicStroke(strokeWidth));
            gbi.draw(shape);
        }
        if (ciw.getLabel() != null) {
            if (centerPoint == null) {
                centerPoint = calculateCenter(shape, srid, bbox, width, height);
            }
            gbi.setColor(Color.black);
            gbi.drawString(ciw.getLabel(), (float) centerPoint.getX(), (float) centerPoint.getY());
        }
    }
    gbi.dispose();
    return newBufIm;
}

From source file:org.jfree.chart.demo.DrawStringPanel.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D graphics2d = (Graphics2D) g;
    Dimension dimension = getSize();
    Insets insets = getInsets();/*from w  w  w. jav  a  2  s.  c  o  m*/
    java.awt.geom.Rectangle2D.Double double1 = new java.awt.geom.Rectangle2D.Double(insets.left, insets.top,
            dimension.getWidth() - (double) insets.left - (double) insets.right,
            dimension.getHeight() - (double) insets.top - (double) insets.bottom);
    double d = double1.getCenterX();
    double d1 = double1.getCenterY();
    java.awt.geom.Line2D.Double double2 = new java.awt.geom.Line2D.Double(d - 2D, d1 + 2D, d + 2D, d1 - 2D);
    java.awt.geom.Line2D.Double double3 = new java.awt.geom.Line2D.Double(d - 2D, d1 - 2D, d + 2D, d1 + 2D);
    graphics2d.setPaint(Color.red);
    graphics2d.draw(double2);
    graphics2d.draw(double3);
    graphics2d.setFont(font);
    graphics2d.setPaint(Color.black);
    if (rotate)
        TextUtilities.drawRotatedString(text, graphics2d, (float) d, (float) d1, anchor, angle, rotationAnchor);
    else
        TextUtilities.drawAlignedString(text, graphics2d, (float) d, (float) d1, anchor);
}

From source file:org.squidy.designer.model.PipeShape.java

@Override
protected void paintDebug(PPaintContext paintContext) {
    super.paintDebug(paintContext);

    Graphics2D g = paintContext.getGraphics();

    if (DebugConstants.PAINTING_BOUNDS) {
        g.setColor(DebugConstants.COLOR_BOUNDS);
        g.draw(getBoundsReference());
    }/*from   ww w .  j a v  a 2 s  . c  om*/

    if (DebugConstants.PAINTING_FULL_BOUNDS) {
        g.setColor(DebugConstants.COLOR_FULL_BOUNDS);
        g.draw(getFullBounds());
    }

    if (DebugConstants.PAINTING_CENTER) {
        g.setColor(DebugConstants.COLOR_CENTER);

        PBounds bounds = getBoundsReference();
        g.fillOval((int) bounds.getCenterX() - 5, (int) bounds.getCenterY() - 5, 10, 10);
    }
}

From source file:FontTest.java

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

    String message = "Hello, World!";

    Font f = new Font("Serif", Font.BOLD, 36);
    g2.setFont(f);//from w ww .ja va  2  s  . c om

    // measure the size of the message

    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D bounds = f.getStringBounds(message, context);

    // set (x,y) = top left corner of text

    double x = (getWidth() - bounds.getWidth()) / 2;
    double y = (getHeight() - bounds.getHeight()) / 2;

    // add ascent to y to reach the baseline

    double ascent = -bounds.getY();
    double baseY = y + ascent;

    // draw the message

    g2.drawString(message, (int) x, (int) baseY);

    g2.setPaint(Color.LIGHT_GRAY);

    // draw the baseline

    g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY));

    // draw the enclosing rectangle

    Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
    g2.draw(rect);
}

From source file:FontPaint.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);//from   w w  w .java 2 s.  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:org.openfaces.component.chart.impl.renderers.XYLineFillRenderer.java

private void drawSeriesLines(Graphics2D g2, Collection<Line2D> lines, int series, int item) {
    g2.setPaint(getItemPaint(series, item));
    g2.setStroke(getItemStroke(series, item));

    for (Line2D line : lines) {
        g2.draw(line);
    }/*  w  w w  .  jav  a  2 s .co  m*/
}

From source file:extern.NpairsBoxAndWhiskerRenderer.java

/**
 * Draws a dot to represent an outlier./*from   www.j  a  v a2 s.  c  o  m*/
 *
 * @param point  the location.
 * @param oRadius  the radius.
 * @param g2  the graphics device.
 */
private void drawEllipse(Point2D point, double oRadius, Graphics2D g2) {
    Ellipse2D dot = new Ellipse2D.Double(point.getX() + oRadius / 2, point.getY(), oRadius, oRadius);
    g2.draw(dot);
}

From source file:Starry.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);/*from w w w  . ja  v  a 2s  . co 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:gov.nih.nci.caintegrator.application.graphing.BoxAndWhiskerDotsRenderer.java

/**
 * Draws a triangle to indicate the presence of far-out values.
 * //from  w  w w . j a va  2 s .c  o  m
 * @param aRadius  the radius.
 * @param g2  the graphics device.
 * @param xx  the x coordinate.
 * @param m  the y coordinate.
 */
private void drawHighFarOut(double aRadius, Graphics2D g2, double xx, double m) {
    double side = aRadius * 2;
    g2.draw(new Line2D.Double(xx - side, m + side, xx + side, m + side));
    g2.draw(new Line2D.Double(xx - side, m + side, xx, m));
    g2.draw(new Line2D.Double(xx + side, m + side, xx, m));
}