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:org.tros.logo.swing.LogoPanel.java

@Override
public void setXY(final double x, final double y) {
    Drawable command = new Drawable() {

        @Override//from w  w w.j ava  2s.c  o  m
        public void draw(Graphics2D g2, TurtleState turtleState) {
            double x2 = (turtleState.width > 0 ? turtleState.width : getWidth()) / 2.0 + x;
            double y2 = (turtleState.height > 0 ? turtleState.height : getHeight()) / 2.0 + y;

            if (!turtleState.penup) {
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.draw(new Line2D.Double(turtleState.penX, turtleState.penY, x2, y2));
            }
            turtleState.penX = x2;
            turtleState.penY = y2;
        }

        @Override
        public void addListener(DrawListener listener) {
        }

        @Override
        public void removeListener(DrawListener listener) {
        }

        @Override
        public Drawable cloneDrawable() {
            return this;
        }
    };
    submitCommand(command);
}

From source file:net.sf.fspdfs.chartthemes.spring.ScaledDialValueIndicator.java

/**
 * Draws the background to the specified graphics device.  If the dial
 * frame specifies a window, the clipping region will already have been
 * set to this window before this method is called.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param plot  the plot (ignored here).
 * @param frame  the dial frame (ignored here).
 * @param view  the view rectangle (<code>null</code> not permitted).
 *///from w w  w.  j a  va 2s .c  o  m
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    // work out the anchor point
    Rectangle2D f = DialPlot.rectangleByRadius(frame, getRadius(), this.getRadius());
    Arc2D arc = new Arc2D.Double(f, this.getAngle(), 0.0, Arc2D.OPEN);
    Point2D pt = arc.getStartPoint();

    // calculate the bounds of the template value
    FontMetrics fm = g2.getFontMetrics(this.getFont());
    String s = this.getNumberFormat().format(this.getTemplateValue());
    Rectangle2D tb = TextUtilities.getTextBounds(s, g2, fm);

    // align this rectangle to the frameAnchor
    Rectangle2D bounds = RectangleAnchor.createRectangle(new Size2D(tb.getWidth(), tb.getHeight()), pt.getX(),
            pt.getY(), this.getFrameAnchor());

    // add the insets
    Rectangle2D fb = this.getInsets().createOutsetRectangle(bounds);

    // draw the background
    g2.setPaint(this.getBackgroundPaint());
    g2.fill(fb);

    // draw the border
    g2.setStroke(this.getOutlineStroke());
    g2.setPaint(this.getOutlinePaint());
    g2.draw(fb);

    // now find the text anchor point
    String valueStr = this.getNumberFormat()
            .format(ChartThemesUtilities.getScaledValue(plot.getValue(this.getDatasetIndex()), scale));
    Point2D pt2 = RectangleAnchor.coordinates(bounds, this.getValueAnchor());
    g2.setPaint(this.getPaint());
    g2.setFont(this.getFont());
    TextUtilities.drawAlignedString(valueStr, g2, (float) pt2.getX(), (float) pt2.getY(), this.getTextAnchor());

}

From source file:Main.java

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

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

    int cx = getSize().width / 2;
    int cy = getSize().height / 2;

    g2.translate(cx, cy);/* w  w w. ja  va  2  s . com*/
    g2.rotate(theta * Math.PI / 180);

    Shape oldClip = g2.getClip();
    Shape e = new Ellipse2D.Float(-cx, -cy, cx * 2, cy * 2);
    g2.clip(e);

    Shape c = new Ellipse2D.Float(-cx, -cy, cx * 3 / 4, cy * 2);
    g2.setPaint(new GradientPaint(40, 40, Color.blue, 60, 50, Color.white, true));
    g2.fill(c);

    g2.setPaint(Color.yellow);
    g2.fillOval(cx / 4, 0, cx, cy);

    g2.setClip(oldClip);

    g2.setFont(new Font("Times New Roman", Font.PLAIN, 64));
    g2.setPaint(new GradientPaint(-cx, 0, Color.red, cx, 0, Color.black, false));
    g2.drawString("Hello, 2D!", -cx * 3 / 4, cy / 4);

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) .75);
    g2.setComposite(ac);

    Shape r = new RoundRectangle2D.Float(0, -cy * 3 / 4, cx * 3 / 4, cy * 3 / 4, 20, 20);
    g2.setStroke(new BasicStroke(4));
    g2.setPaint(Color.magenta);
    g2.fill(r);
    g2.setPaint(Color.green);
    g2.draw(r);

    g2.drawImage(image, -cx / 2, -cy / 2, this);
}

From source file:net.sf.jasperreports.chartthemes.spring.ScaledDialValueIndicator.java

/**
 * Draws the background to the specified graphics device.  If the dial
 * frame specifies a window, the clipping region will already have been
 * set to this window before this method is called.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param plot  the plot (ignored here).
 * @param frame  the dial frame (ignored here).
 * @param view  the view rectangle (<code>null</code> not permitted).
 *///  w  w w  . ja v  a 2 s  .  c  om
@Override
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    // work out the anchor point
    Rectangle2D f = DialPlot.rectangleByRadius(frame, getRadius(), this.getRadius());
    Arc2D arc = new Arc2D.Double(f, this.getAngle(), 0.0, Arc2D.OPEN);
    Point2D pt = arc.getStartPoint();

    // calculate the bounds of the template value
    FontMetrics fm = g2.getFontMetrics(this.getFont());
    String s = this.getNumberFormat().format(this.getTemplateValue());
    Rectangle2D tb = TextUtilities.getTextBounds(s, g2, fm);

    // align this rectangle to the frameAnchor
    Rectangle2D bounds = RectangleAnchor.createRectangle(new Size2D(tb.getWidth(), tb.getHeight()), pt.getX(),
            pt.getY(), this.getFrameAnchor());

    // add the insets
    Rectangle2D fb = this.getInsets().createOutsetRectangle(bounds);

    // draw the background
    g2.setPaint(this.getBackgroundPaint());
    g2.fill(fb);

    // draw the border
    g2.setStroke(this.getOutlineStroke());
    g2.setPaint(this.getOutlinePaint());
    g2.draw(fb);

    // now find the text anchor point
    String valueStr = this.getNumberFormat()
            .format(ChartThemesUtilities.getScaledValue(plot.getValue(this.getDatasetIndex()), scale));
    Point2D pt2 = RectangleAnchor.coordinates(bounds, this.getValueAnchor());
    g2.setPaint(this.getPaint());
    g2.setFont(this.getFont());
    TextUtilities.drawAlignedString(valueStr, g2, (float) pt2.getX(), (float) pt2.getY(), this.getTextAnchor());

}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.SpectraPanel.java

public void mouseReleased(MouseEvent e) {
    if (MouseUtils.isPopupTrigger(e)) {
        // clear all
        if (zoom_rectangle != null) {
            Graphics2D g2 = (Graphics2D) getGraphics();
            g2.setXORMode(java.awt.Color.gray);
            g2.draw(zoom_rectangle);
            g2.dispose();//from w w w.  jav  a 2  s  .c o  m
        }
        mouse_start_point = null;
        zoom_rectangle = null;

        // open popup
        current_peak = findPeakAt(e.getPoint());
        enforceSelection(current_peak);
        createPopupMenu(current_peak != null).show(theChartPanel, e.getX(), e.getY());
    } else {
        if (zoom_rectangle != null && mouse_start_point != null) {
            if (Math.abs(e.getX() - mouse_start_point.getX()) > 10) {
                //if( e.getX() < mouse_start_point.getX() ) {
                // unzoom all
                //    onZoomNone();
                //}
                //else {        

                // zoom area           
                double start_x = Math.min(e.getX(), mouse_start_point.getX());
                double end_x = Math.max(e.getX(), mouse_start_point.getX());

                Rectangle2D data_area = theChartPanel.getScreenDataArea((int) start_x,
                        (int) mouse_start_point.getY());

                double new_lower_bound = screenToDataCoordX(start_x);
                double new_upper_bound = screenToDataCoordX(Math.min(end_x, data_area.getMaxX()));
                thePlot.getDomainAxis().setRange(new Range(new_lower_bound, new_upper_bound));
            } else {
                // clear rectangle
                Graphics2D g2 = (Graphics2D) getGraphics();
                g2.setXORMode(java.awt.Color.gray);
                g2.draw(zoom_rectangle);
                g2.dispose();
            }
        }

        // restore zooming
        if (!was_moving && is_moving)
            onActivateZooming();

        zoom_rectangle = null;
        mouse_start_point = null;
    }
}

From source file:business.ImageManager.java

public ImageIcon getArrow(double angle) {
    BufferedImage img = new BufferedImage(40, 40, BufferedImage.TRANSLUCENT);
    Graphics2D big = img.createGraphics();
    //setup para os rastros
    big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    big.setStroke(/*from   w  w  w . j  a va  2  s  .c o  m*/
            new BasicStroke(2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1f, new float[] { 5f }, 0f));
    big.setColor(Color.red);

    int cx = this.getYellowBall().getIconWidth() / 2;
    int cy = this.getYellowBall().getIconHeight() / 2;
    AffineTransform at = AffineTransform.getTranslateInstance(cx, cy);
    at.rotate(Math.toRadians(angle));
    //        at.scale(2.0, 2.0);
    Shape shape = at.createTransformedShape(createArrow());
    big.setPaint(Color.red);
    big.draw(shape);
    ImageIcon ret = new ImageIcon(img);
    return (ret);
    //        tenta com o icone...angle.
}

From source file:business.ImageManager.java

private void doDrawPathOrdemArmy(Graphics2D big, Point ori, Point dest, Color color) {
    //setup para os rastros
    big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    big.setComposite(alcom);//from   www .  j  ava2  s.  c  o  m
    big.setStroke(new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1f,
            new float[] { 3f, 5f, 7f, 5f, 11f, 5f, 15f, 5f, 21f, 5f, 27f, 5f, 33f, 5f }, 0f));
    big.setColor(color);
    //draw path
    Path2D.Double path = new Path2D.Double();
    path.moveTo(ori.getX(), ori.getY());
    path.curveTo(dest.getX() + 10, dest.getY() - 10, dest.getX() - 10, dest.getY() + 10, dest.getX(),
            dest.getY());

    //draw on graph
    big.draw(path);
}

From source file:org.openmeetings.app.data.record.BatikMethods.java

public void drawThickLine2DPaint(Graphics2D g2d, double x1, double y1, double x2, double y2, int width, Color c,
        double xObj, double yObj, float alpha) throws Exception {
    g2d.setPaint(c);/*from w ww  .ja  va2 s .  c o  m*/

    int[] rules = new int[8];

    //all possible Compositing Rules:
    rules[0] = AlphaComposite.SRC_OVER;
    rules[1] = AlphaComposite.DST_OVER;
    rules[2] = AlphaComposite.CLEAR;
    rules[3] = AlphaComposite.SRC;
    rules[4] = AlphaComposite.SRC_IN;
    rules[5] = AlphaComposite.DST_IN;
    rules[6] = AlphaComposite.SRC_OUT;
    rules[7] = AlphaComposite.DST_OUT;

    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, alpha));
    g2d.setStroke(new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
    Line2D line = new Line2D.Double(x1, y1, x2, y2);
    g2d.draw(line);
}

From source file:com.github.lindenb.jvarkit.tools.misc.BamCmpCoverage.java

private void paint(Graphics2D g, BitDepthMatrix matrix) {
    final Line2D.Double segment = new Line2D.Double();
    for (int i = 0; i < matrix.owner.bitSize; ++i) {
        for (int j = 0; j < matrix.owner.bitSize; ++j) {
            if (!matrix.getXY(i, j))
                continue;

            double x = this.marginWidth + matrix.sample_x * this.sampleWidth;
            x += this.sampleWidth * (i) / ((double) matrix.owner.bitSize);

            double y = this.marginWidth + matrix.sample_y * this.sampleWidth;
            y += this.sampleWidth * (j) / ((double) matrix.owner.bitSize);

            segment.x1 = x;/*www. j  a  v  a  2 s . c  o  m*/
            segment.y1 = y;
            segment.x2 = x + 0.01;
            segment.y2 = y + 0.01;
            g.draw(segment);
        }
    }
}

From source file:business.ImageManager.java

private void doDrawPathOrdem(Graphics2D big, Point ori, Point dest, Color color) {
    //setup para os rastros
    big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    big.setStroke(new BasicStroke(1.75f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1f,
            new float[] { 3f, 5f, 7f, 5f, 11f, 5f, 15f, 5f, 21f, 5f, 27f, 5f, 33f, 5f }, 0f));
    big.setColor(color);//from ww w.  j  a  va 2  s .  co  m
    //draw path
    Path2D.Double path = new Path2D.Double();
    path.moveTo(ori.getX(), ori.getY());
    path.curveTo(dest.getX() - 20, dest.getY() + 20, dest.getX() + 20, dest.getY() - 20, dest.getX() + 12,
            dest.getY());
    //path.lineTo(dest.getX(), dest.getY());

    //draw on graph
    big.draw(path);
}