Example usage for java.awt Graphics2D getStroke

List of usage examples for java.awt Graphics2D getStroke

Introduction

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

Prototype

public abstract Stroke getStroke();

Source Link

Document

Returns the current Stroke in the Graphics2D context.

Usage

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

public static void drawShapes(Graphics2D g2, Rectangle2D imageArea, Shape shape, JFreeChart chart) {
    Color color = Color.CYAN;
    Stroke oldStroke = g2.getStroke();
    g2.setStroke(new BasicStroke(2f));
    drawShape(g2, imageArea, shape, color, chart);
    g2.setStroke(oldStroke);//  w  ww  . j ava 2 s.co m
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Draws an arrow from <code>(xCenter,yCenter)</code> to <code>(x,y)</code>.
 * Code stolen from http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135.
 * /*from w  ww .  ja v a  2 s.  c  o m*/
 * @param g the graphics context to draw in
 * @param headSize the size of the arrow head
 * @param xCenter the x-coord of the arrow tail
 * @param yCenter the y-coord of the arrow tail
 * @param x the x-coord of the arrow head's tip
 * @param y the y-coord of the arrow head's tip
 * @param stroke the <code>Stroke</code> to use
 */
public static void drawArrow(Graphics g, int xCenter, int yCenter, int x, int y, int headSize, float stroke) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.addRenderingHints(hints);

    double aDir = Math.atan2(xCenter - x, yCenter - y);
    Stroke origStroke = g2d.getStroke();
    g2d.setStroke(new BasicStroke(stroke)); // make the arrow head solid even if dash pattern has been specified
    g2d.drawLine(x, y, xCenter, yCenter);
    Polygon tmpPoly = new Polygon();
    int i1 = 2 * headSize + (int) stroke; //(stroke * 2);
    int i2 = headSize + (int) stroke; // make the arrow head the same size regardless of the length length
    tmpPoly.addPoint(x, y); // arrow tip
    tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5));
    tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir));
    tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5));
    tmpPoly.addPoint(x, y); // arrow tip
    g2d.drawPolygon(tmpPoly);
    g2d.fillPolygon(tmpPoly); // remove this line to leave arrow head unpainted
    g2d.setStroke(origStroke);
}

From source file:ala.soils2sat.DrawingUtils.java

public static void drawRoundedRect(Graphics g, int left, int top, int right, int bottom) {
    Graphics2D g2d = (Graphics2D) g;
    g.drawLine(left + cornerSize, top, right - cornerSize, top);
    g.drawLine(left + cornerSize, bottom, right - cornerSize, bottom);
    g.drawLine(left, top + cornerSize, left, bottom - cornerSize);
    g.drawLine(right, top + cornerSize, right, bottom - cornerSize);
    final Object previousAntiAliasingHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    final Stroke previousStroke = g2d.getStroke();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
    try {/*from  w w w.  j a v  a2 s. co  m*/
        g.drawLine(left, top + cornerSize, left + cornerSize, top);
        g.drawLine(left, bottom - cornerSize, left + cornerSize, bottom);
        g.drawLine(right, top + cornerSize, right - cornerSize, top);
        g.drawLine(right, bottom - cornerSize, right - cornerSize, bottom);
    } finally {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, previousAntiAliasingHint);
        g2d.setStroke(previousStroke);
    }
}

From source file:com.controlj.addon.gwttree.server.OpaqueBarRenderer3D.java

private void drawMarker(Point2D.Double point, Graphics2D g2, Color color) {
    Stroke oldStroke = g2.getStroke();
    Paint oldPaint = g2.getPaint();
    g2.setPaint(color);//  w ww  . ja v a  2  s  . c  o m
    //Shape line = new Line2D.Double(point, point);
    Shape marker = new Ellipse2D.Double(point.getX() - 1.5, point.getY() - 1.5, 3, 3);
    g2.fill(marker);
    g2.setPaint(oldPaint);
}

From source file:edu.umd.cfar.lamp.chronicle.ChronicleRuler.java

private void drawLine(int x1, int y1, int x2, int y2, Paint p, Stroke stroke, PPaintContext paintContext) {
    Graphics2D g2 = paintContext.getGraphics();
    Paint old = g2.getPaint();/*from   ww  w .j  a  v  a  2 s .c  o  m*/
    Stroke oldS = g2.getStroke();
    g2.setPaint(p);
    g2.setStroke(stroke);
    g2.drawLine(x1, y1, x2, y2);
    g2.setPaint(old);
    g2.setStroke(oldS);
}

From source file:MWC.GUI.JFreeChart.StepperXYPlot.java

/**
 * draw the new stepper line into the plot
 * /*from w  w w. j  av a 2 s .  com*/
 * @param g2
 * @param linePosition
 * @param dataArea
 */
protected void plotStepperLine(final Graphics2D g2, final double linePosition, final Rectangle2D dataArea) {
    // prepare to draw
    final Stroke oldStroke = g2.getStroke();
    g2.setXORMode(Color.darkGray);

    // thicken up the line
    g2.setStroke(new BasicStroke(3));

    if (this.getOrientation() == PlotOrientation.VERTICAL) {
        // draw the line
        g2.drawLine((int) linePosition - 1, (int) dataArea.getY() + 1, (int) linePosition - 1,
                (int) dataArea.getY() + (int) dataArea.getHeight() - 1);
    } else {
        // draw the line
        g2.drawLine((int) dataArea.getY() + 1, (int) linePosition - 1,
                (int) dataArea.getY() + (int) dataArea.getHeight() - 1, (int) linePosition - 1);

    }

    // and restore everything
    g2.setStroke(oldStroke);
    g2.setPaintMode();
}

From source file:org.tsho.dmc2.core.chart.CowebRenderer.java

private void animateCowebPlot(Graphics2D g2, Rectangle2D dataArea) {

    Graphics2D g2bisec = (Graphics2D) g2.create();
    g2bisec.setColor(Color.blue);

    Stroke stroke = new BasicStroke(3f);
    Stroke origStroke = g2.getStroke();
    Color color = Color.BLACK;
    g2blink = (Graphics2D) g2.create();
    g2blink.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

    g2blink.setXORMode(color);/* w w  w. ja v a2s  .c om*/
    g2blink.setStroke(stroke);

    if (plot.isAlpha()) {
        g2bisec.setComposite(AlphaComposite.SrcOver);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, plot.getForegroundAlpha()));
    }

    /* transients */
    stepper.setInitialValue(initialValue);
    stepper.initialize();

    state = STATE_TRANSIENTS;

    for (int index = 0; index < transients; index++) {

        stepper.step();

        if (stopped) {
            state = STATE_STOPPED;
            return;
        }
    }

    state = STATE_RUNNING;

    Range xDataRange = plot.getDataRange(domainAxis);
    int transX, transY, transX1, transY1;

    transX = (int) this.domainAxis.valueToJava2D(xDataRange.getLowerBound(), dataArea, RectangleEdge.BOTTOM);
    transY = (int) this.rangeAxis.valueToJava2D(xDataRange.getLowerBound(), dataArea, RectangleEdge.LEFT);
    transX1 = (int) this.domainAxis.valueToJava2D(xDataRange.getUpperBound(), dataArea, RectangleEdge.BOTTOM);
    transY1 = (int) this.rangeAxis.valueToJava2D(xDataRange.getUpperBound(), dataArea, RectangleEdge.LEFT);

    g2bisec.drawLine(transX, transY, transX1, transY1);

    //renderer.reset();

    recurseCoweb(g2, dataArea);
}

From source file:edu.ku.brc.ui.dnd.SimpleGlassPane.java

/**
 * @param g2//from   w w  w .  j ava2 s .co  m
 * @param x
 * @param y
 * @param w
 * @param h
 * @param arcW
 * @param arcH
 */
private void drawBGContainer(final Graphics2D g2, final boolean doFill, final int x, final int y, final int w,
        final int h, final int arcW, final int arcH) {
    Stroke cacheStroke = g2.getStroke();

    g2.setStroke(UIHelper.getStdLineStroke());

    if (UIHelper.isWindows()) {
        if (doFill) {
            //g2.fillRect(x, y, w, h); // Make ugly for Windows
            g2.fillRoundRect(x, y, w, h, 4, 4);
        } else {
            //g2.drawRect(x, y, w, h); // Make ugly for Windows
            g2.drawRoundRect(x, y, w, h, 4, 4);
        }
    } else {
        if (doFill) {
            g2.fillRoundRect(x, y, w, h, arcW, arcH);
        } else {
            g2.drawRoundRect(x, y, w, h, arcW, arcH);
        }
    }
    g2.setStroke(cacheStroke);
}

From source file:org.tsho.dmc2.core.chart.TrajectoryRenderer.java

public void render(final Graphics2D g2, final Rectangle2D dataArea, final PlotRenderingInfo info) {
    ValueAxis domainAxis = plot.getDomainAxis();
    ValueAxis rangeAxis = plot.getRangeAxis();

    Stroke stroke = new BasicStroke(7f);
    Stroke origStroke = g2.getStroke();
    Color color = Color.BLACK;

    /* transients */
    if (!continua) {
        stepper.initialize();/*from   ww  w. j  a v  a 2s. co  m*/

        state = STATE_TRANSIENTS;

        try {
            for (index = 0; index < transients; index++) {
                stepper.step();
                if (stopped) {
                    state = STATE_STOPPED;
                    return;
                }
            }
        } catch (RuntimeException re) {
            throw new RuntimeException(re);
        }

        index = 0;
        prevX = 0;
        prevY = 0;
    }

    state = STATE_POINTS;

    Stepper.Point2D point;
    double[] fullPoint = new double[dataset.getNcol()];
    dataset.clearRows();
    int x, y;
    int start = index;
    int end = 0;
    if (index == 0) {
        end = start + iterations + 1;
    } else {
        end = start + iterations;
    }

    for (; index < end; index++) {
        point = stepper.getCurrentPoint2D();
        stepper.getCurrentValue(fullPoint);
        try {
            dataset.addRow((double[]) fullPoint.clone());
        } catch (DatasetException e) {
            System.err.println(e);
        }

        if (!timePlot) {
            x = (int) domainAxis.valueToJava2D(point.getX(), dataArea, RectangleEdge.BOTTOM);
        } else {
            x = (int) domainAxis.valueToJava2D(index + transients, dataArea, RectangleEdge.BOTTOM);
        }

        y = (int) rangeAxis.valueToJava2D(point.getY(), dataArea, RectangleEdge.LEFT);

        if (Double.isNaN(point.getX()) || Double.isNaN(point.getY())) {
            System.err.println("NaN values at iteration " + index);
            //FIXME
            //Don't simply throw exception: that would mess up the state machine!
            //throw new RuntimeException("NaN values at iteration " + index);
        }

        if (delay > 0) {
            boolean flag = false;

            try {
                Thread.sleep(delay * 5);

                drawItem(g2, index, x, y);

                g2.setXORMode(color);
                g2.setStroke(stroke);
                g2.drawRect(x - 1, y - 1, 3, 3);

                flag = true;

                Thread.sleep(delay * 5);
            } catch (final InterruptedException e) {
            }

            if (flag) {
                g2.drawRect(x - 1, y - 1, 3, 3);
                g2.setPaintMode();
                g2.setStroke(origStroke);
            } else {
                drawItem(g2, index, x, y);
            }
        } else {
            drawItem(g2, index, x, y);
        }

        try {
            stepper.step();
        } catch (RuntimeException re) {
            throw new RuntimeException(re);
        }

        if (stopped) {
            state = STATE_STOPPED;
            return;
        }
    }

    state = STATE_FINISHED;
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * Paints <code>e</code>, whose endpoints are at <code>(x1,y1)</code>
 * and <code>(x2,y2)</code>, on the graphics context <code>g</code>.
 * Uses the paint and stroke specified by this instance's 
 * <code>EdgeColorFunction</code> and <code>EdgeStrokeFunction</code>, 
 * respectively.  (If the paint is unspecified, the existing
 * paint for the graphics context is used; the same applies to stroke.)
 * The details of the actual rendering are delegated to
 * <code>drawSelfLoop</code> or <code>drawSimpleEdge</code>, 
 * depending on the type of the edge.  /*ww w  .j a va2  s .co m*/
 * Note that <code>(x1, y1)</code> is the location of
 * e.getEndpoints.getFirst() and <code>(x2, y2)</code> is the location of
 * e.getEndpoints.getSecond().
 * 
 */
public void paintEdge(Graphics g, Edge e, int x1, int y1, int x2, int y2) {
    if (!edgeIncludePredicate.evaluate(e))
        return;

    // don't draw edge if either incident vertex is not drawn
    Pair endpoints = e.getEndpoints();
    Vertex v1 = (Vertex) endpoints.getFirst();
    Vertex v2 = (Vertex) endpoints.getSecond();
    if (!vertexIncludePredicate.evaluate(v1) || !vertexIncludePredicate.evaluate(v2))
        return;

    Graphics2D g2d = (Graphics2D) g;

    Stroke new_stroke = edgeStrokeFunction.getStroke(e);
    Stroke old_stroke = g2d.getStroke();
    if (new_stroke != null)
        g2d.setStroke(new_stroke);

    drawSimpleEdge(g2d, e, x1, y1, x2, y2);

    // restore paint and stroke
    if (new_stroke != null)
        g2d.setStroke(old_stroke);

}