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

protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    long startTime, endTime, totalTime;

    g.setColor(Color.WHITE);/*w  ww  . j av a  2  s  .  co m*/
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.BLACK);

    g.drawString("Bad vs. Good Primitive Rendering", 50, 20);
    g.drawString("(" + ITERATIONS + " iterations)", 100, 35);
    g.drawString("Bad: ", 10, BAD_Y + 30);
    g.drawString("Good: ", 10, GOOD_Y + 30);

    // Bad line
    Shape line = new Line2D.Double(LINE_X, BAD_Y, LINE_X + 50, BAD_Y + 50);
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g2d.draw(line);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("bad line = " + totalTime);
    g.drawString(totalTime + " ms", LINE_X, BAD_Y + 70);

    // Good line
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g.drawLine(LINE_X, GOOD_Y, LINE_X + 50, GOOD_Y + 50);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("good line = " + totalTime);
    g.drawString(totalTime + " ms", LINE_X, GOOD_Y + 70);

    // Bad rect
    Shape rect = new Rectangle(RECT_X, BAD_Y, 50, 50);
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g2d.fill(rect);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("bad rect = " + totalTime);
    g.drawString(totalTime + " ms", RECT_X, BAD_Y + 70);

    // Good rect
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g.fillRect(RECT_X, GOOD_Y, 50, 50);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("good rect = " + totalTime);
    g.drawString(totalTime + " ms", RECT_X, GOOD_Y + 70);
}

From source file:net.sourceforge.processdash.ui.lib.chart.StandardDiscItemRenderer.java

protected void drawOutline(Graphics2D g2, Ellipse2D shape, Comparable key) {
    Paint outlinePaint = lookupOutlinePaint(key);
    Stroke outlineStroke = lookupOutlineStroke(key);
    if (outlinePaint != null && outlineStroke != null) {
        g2.setPaint(outlinePaint);/*from  w w w .jav a 2s  . c o  m*/
        g2.draw(shape);
    }
}

From source file:GradientPane.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Point2D.Float p1 = new Point2D.Float(150.f, 75.f); // Gradient line start
    Point2D.Float p2 = new Point2D.Float(250.f, 75.f); // Gradient line end
    float width = 300;
    float height = 50;
    Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
    GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, false); // Acyclic
                                                                                       // gradient
    rect1.setRect(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g2); // Gradient color fill
    g2D.fill(rect1); // Fill the rectangle
    g2D.setPaint(Color.BLACK); // Outline in black
    g2D.draw(rect1); // Fill the rectangle
    g2D.draw(new Line2D.Float(p1, p2));
}

From source file:org.fhcrc.cpl.toolbox.gui.chart.ChartMouseAndMotionListener.java

/**
 * Draws zoom rectangle (if present).//from www. ja v a 2 s .co  m
 * The drawing is performed in XOR mode, therefore
 * when this method is called twice in a row,
 * the second call will completely restore the state
 * of the canvas.
 *
 * @param selectedRegion
 * @param stroke
 * @param fillColor
 */
protected void drawSelectedRegion(Rectangle2D selectedRegion, Stroke stroke, Color fillColor, boolean xorMode,
        Graphics2D g2) {
    // Set XOR mode to draw the zoom rectangle
    if (g2 == null)
        return;
    Paint origColor = g2.getPaint();

    if (selectedRegion != null) {
        if (xorMode) {
            g2.setXORMode(fillColor);
        } else
            g2.setPaint(fillColor);
        g2.fill(selectedRegion);
        g2.setPaint(Color.black);
        g2.setStroke(stroke);
        if (xorMode)
            g2.setPaint(Color.white);
        else
            g2.setPaint(Color.black);

        g2.draw(selectedRegion);
        g2.setPaintMode();
        g2.setPaint(origColor);
    }
}

From source file:figs.treeVisualization.gui.PhyloDateAxis.java

/**
  * Draws an axis line at the current cursor position and edge.
  * /*from   w w  w  .ja va2 s .c  om*/
  * This always uses RectangleEdge.RIGHT, so the cursor is the x position.
  * 
  * @param g2  the graphics device.
  * @param cursor  the cursor position. 
  * @param dataArea  the data area.
  * @param edge  the edge.
  * 
  * Original method is in <code>org.jfree.chart.axis.ValueAxis</code>
  */
protected void drawAxisLine(Graphics2D g2, double cursor, Rectangle2D dataArea) {

    if (!isAxisLineVisible()) {
        // originally in drawTickMarksAndLabels
        return;
    }

    Line2D axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor, dataArea.getHeight());

    g2.setPaint(getAxisLinePaint());
    g2.setStroke(getAxisLineStroke());
    g2.draw(axisLine);

}

From source file:PrintTest.java

/**
 * This method draws the page both on the screen and the printer graphics context.
 * @param g2 the graphics context/*from w ww  .  j a  v  a 2 s.  c  o m*/
 */
public void drawPage(Graphics2D g2) {
    FontRenderContext context = g2.getFontRenderContext();
    Font f = new Font("Serif", Font.PLAIN, 72);
    GeneralPath clipShape = new GeneralPath();

    TextLayout layout = new TextLayout("Hello", f, context);
    AffineTransform transform = AffineTransform.getTranslateInstance(0, 72);
    Shape outline = layout.getOutline(transform);
    clipShape.append(outline, false);

    layout = new TextLayout("World", f, context);
    transform = AffineTransform.getTranslateInstance(0, 144);
    outline = layout.getOutline(transform);
    clipShape.append(outline, false);

    g2.draw(clipShape);
    g2.clip(clipShape);

    final int NLINES = 50;
    Point2D p = new Point2D.Double(0, 0);
    for (int i = 0; i < NLINES; i++) {
        double x = (2 * getWidth() * i) / NLINES;
        double y = (2 * getHeight() * (NLINES - 1 - i)) / NLINES;
        Point2D q = new Point2D.Double(x, y);
        g2.draw(new Line2D.Double(p, q));
    }
}

From source file:org.geotools.renderer.chart.GeometryRenderer.java

void drawCoordinate(Coordinate c, Graphics2D g2, int series, int item, Rectangle2D dataArea, XYPlot plot,
        ValueAxis domainAxis, ValueAxis rangeAxis) {

    double tx = domainAxis.valueToJava2D(c.x, dataArea, plot.getDomainAxisEdge());
    double ty = rangeAxis.valueToJava2D(c.y, dataArea, plot.getRangeAxisEdge());

    Shape shape = getItemShape(series, item);
    shape = ShapeUtilities.createTranslatedShape(shape, tx, ty);

    if (fillCoordinates) {
        g2.fill(shape);/*from  w  w  w. j a va2 s  . c  o  m*/
    } else {
        g2.draw(shape);
    }
}

From source file:com.bdb.weather.display.current.WindDirPointer.java

/**
 * Draws the pointer.//from w w w .  java 2 s. c o  m
 * 
 * @param g2 The graphics target.
 * @param plot The plot.
 * @param frame The dial's reference frame.
 * @param view The dial's view.
 */
@Override
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {
    g2.setPaint(Color.blue);
    g2.setStroke(new BasicStroke(1.0f));
    DialScale scale = plot.getScaleForDataset(getDatasetIndex());
    double value = plot.getValue(getDatasetIndex());
    double angle = scale.valueToAngle(value % 360.0);

    Rectangle2D outerRect = DialPlot.rectangleByRadius(frame, outerRadius, outerRadius);
    Rectangle2D innerRect = DialPlot.rectangleByRadius(frame, outerRadius - innerOffset,
            outerRadius - innerOffset);

    g2.setPaint(getOutlinePaint());
    Arc2D arc1 = new Arc2D.Double(outerRect, angle - (ARC_LENGTH / 2), ARC_LENGTH, Arc2D.OPEN);
    g2.draw(arc1);
    Arc2D arc2 = new Arc2D.Double(innerRect, angle, 0.0, Arc2D.OPEN);
    GeneralPath gp = new GeneralPath();
    gp.moveTo(arc1.getStartPoint().getX(), arc1.getStartPoint().getY());
    gp.lineTo(arc2.getStartPoint().getX(), arc2.getStartPoint().getY());
    gp.lineTo(arc1.getEndPoint().getX(), arc1.getEndPoint().getY());
    g2.draw(gp);
    if (fill) {
        g2.setPaint(getFillPaint());
        g2.fill(gp);
    }
}

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

/**
 * Paints the panel./* w  w  w  .  j  a  va 2 s  . c o  m*/
 *
 * @param g  the graphics device.
 */
public void paintComponent(final Graphics g) {

    super.paintComponent(g);
    final Graphics2D g2 = (Graphics2D) g;

    final Dimension size = getSize();
    final Insets insets = getInsets();
    final Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top,
            size.getWidth() - insets.left - insets.right, size.getHeight() - insets.top - insets.bottom);

    final double x = available.getCenterX();
    final double y = available.getCenterY();

    final Line2D line1 = new Line2D.Double(x - 2.0, y + 2.0, x + 2.0, y - 2.0);
    final Line2D line2 = new Line2D.Double(x - 2.0, y - 2.0, x + 2.0, y + 2.0);
    g2.setPaint(Color.red);
    g2.draw(line1);
    g2.draw(line2);

    g2.setFont(this.font);
    g2.setPaint(Color.black);
    if (this.rotate) {
        TextUtilities.drawRotatedString(this.text, g2, (float) x, (float) y, this.anchor, this.angle,
                this.rotationAnchor);
    } else {
        TextUtilities.drawAlignedString(this.text, g2, (float) x, (float) y, this.anchor);
    }

}

From source file:FontRenderContextRenderingHints.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    int w = getSize().width;
    int h = getSize().height;

    RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2D.setRenderingHints(qualityHints);

    AffineTransform at = new AffineTransform();
    at.setToTranslation(-300, -400);//from w  w  w.j a v  a 2s.c  o  m
    at.shear(-0.5, 0.0);
    FontRenderContext frc = new FontRenderContext(at, false, false);
    TextLayout tl = new TextLayout("World!", font, frc);
    Shape outline = tl.getOutline(null);
    g2D.setColor(Color.blue);

    BasicStroke wideStroke = new BasicStroke(2.0f);
    g2D.setStroke(wideStroke);
    g2D.draw(outline);

}