Example usage for java.awt Graphics2D setStroke

List of usage examples for java.awt Graphics2D setStroke

Introduction

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

Prototype

public abstract void setStroke(Stroke s);

Source Link

Document

Sets the Stroke for the Graphics2D context.

Usage

From source file:at.tuwien.ifs.somtoolbox.apps.viewer.MapPNode.java

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

    Graphics2D g2d = paintContext.getGraphics();
    g2d.setStroke(new BasicStroke(1.0f));
}

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.  //from  w  w w. j av  a  2  s.  c o 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);

}

From source file:org.kalypso.ogc.sensor.diagview.jfreechart.ObservationPlot.java

/**
 * Draw alarmlevels (horizontal line and text annotation)
 */// w  w w.  j a  va  2 s .  com
private void drawAlarmLevels(final Graphics2D g2, final Rectangle2D dataArea) {
    for (final Object element : m_yConsts.keySet()) {
        final AlarmLevelPlotElement vac = m_yConsts.get(element);

        final ValueAxis axis = m_diag2chartAxis.get(vac.getAxis());
        if (axis == null)
            continue;

        if (axis.getRange().contains(vac.getAlarm().value)) {
            final double yy = axis.valueToJava2D(vac.getAlarm().value, dataArea, RectangleEdge.LEFT);
            final Line2D line = new Line2D.Double(dataArea.getMinX(), yy, dataArea.getMaxX(), yy);
            // always set stroke, else we got the stroke from the last drawn line
            g2.setStroke(AlarmLevelPlotElement.STROKE_ALARM);
            g2.setPaint(vac.getAlarm().color);
            g2.draw(line);

            // and draw the text annotation: if annotation is outside (on top); label it below the line
            if (yy < dataArea.getMinY() + 20)
                vac.getAnnotation().setAngle(Math.toRadians(20));
            else
                vac.getAnnotation().setAngle(Math.toRadians(340));

            vac.getAnnotation().draw(g2, this, dataArea, getDomainAxis(), axis);
        }
    }
}

From source file:StrokeChooserPanel.java

/**
 * Draws a line using the sample stroke.
 *
 * @param g  the graphics device.//from   w w  w. j ava2  s  . com
 */
public void paintComponent(final Graphics g) {

    final Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    final Dimension size = getSize();
    final Insets insets = getInsets();
    final double xx = insets.left;
    final double yy = insets.top;
    final double ww = size.getWidth() - insets.left - insets.right;
    final double hh = size.getHeight() - insets.top - insets.bottom;

    // calculate point one
    final Point2D one = new Point2D.Double(xx + 6, yy + hh / 2);
    // calculate point two
    final Point2D two = new Point2D.Double(xx + ww - 6, yy + hh / 2);
    // draw a circle at point one
    final Ellipse2D circle1 = new Ellipse2D.Double(one.getX() - 5, one.getY() - 5, 10, 10);
    final Ellipse2D circle2 = new Ellipse2D.Double(two.getX() - 6, two.getY() - 5, 10, 10);

    // draw a circle at point two
    g2.draw(circle1);
    g2.fill(circle1);
    g2.draw(circle2);
    g2.fill(circle2);

    // draw a line connecting the points
    final Line2D line = new Line2D.Double(one, two);
    if (this.stroke != null) {
        g2.setStroke(this.stroke);
        g2.draw(line);
    }

}

From source file:no.met.jtimeseries.chart.XYWindArrowRenderer.java

private void drawArrow(Graphics2D g, double speed) {
    int startX = -(arrowWidth / 2);
    int startY = -(arrowHeight / 2);
    // draw main arrow line
    g.fill(new Rectangle2D.Double(startX, startY, arrowWidth, arrowHeight));

    // drawing speed feathers (representing 25 ms each)
    int featherOffset = 0;
    int featherHeight = arrowHeight / featherPadding;
    int flagFeathers = (int) speed / 25;
    int[] flagFeatherX = { 1, -featherWidth, 1 };
    for (int i = 0; i < flagFeathers; i++) {
        if (i > 0)
            featherOffset += 1; // more space if multiple flags
        int yValue = (arrowHeight / 2) - (featherOffset * featherHeight);
        int flagSize = (int) (featherHeight * 1.5f);
        int[] flagFeatherY = { yValue, (yValue - flagSize / 2), yValue - flagSize };
        g.fill(new Polygon(flagFeatherX, flagFeatherY, 3));
        featherOffset += 1;/*from www .  j a  v  a2s.  c o  m*/
    }
    // Add space between flag-feather and next
    if (flagFeathers > 0)
        featherOffset++;
    double remainingSpeed = speed - (flagFeathers * 25);

    // draw full feathers (representing 5 ms each)
    int fullFeathers = (int) remainingSpeed / 5;
    g.setStroke(new BasicStroke(arrowWidth));
    for (int i = 0; i < fullFeathers; i++) {
        int yValue = (arrowHeight / 2) - (featherOffset * featherHeight);
        yValue -= (arrowWidth / 2); // allign with start of arrow
        int[] x = { 1, -featherWidth };
        int[] y = { yValue, yValue };
        g.draw(new Polygon(x, y, 2));
        featherOffset += 1;
    }
    remainingSpeed = remainingSpeed - (fullFeathers * 5);

    // draw half feathers (representing 2.5 ms each)
    int halfFeathers = (int) (remainingSpeed / 2.5);
    // never draw half-feathers at the start
    if (featherOffset == 0)
        featherOffset = 1;
    for (int i = 0; i < halfFeathers; i++) {
        int yValue = (arrowHeight / 2) - (featherOffset * featherHeight);
        yValue -= (arrowWidth / 2); // allign with start of arrow
        int[] x = { 1, (-featherWidth / 2) };
        int[] y = { yValue, yValue };
        g.draw(new Polygon(x, y, 2));
        featherOffset += 1;
    }

    g.setStroke(new BasicStroke(1));
}

From source file:org.cruk.mga.CreateReport.java

/**
 * Creates a summary plot for the given set of multi-genome alignment summaries.
 *
 * @param multiGenomeAlignmentSummaries//from  www.  jav  a  2  s . c  o  m
 * @param the name of the image file
 * @throws IOException
 */
private void createSummaryPlot(Collection<MultiGenomeAlignmentSummary> multiGenomeAlignmentSummaries,
        String imageFilename) throws IOException {
    if (imageFilename == null)
        return;

    int n = multiGenomeAlignmentSummaries.size();
    log.debug("Number of summaries = " + n);

    scaleForPlotWidth();

    int fontHeight = getFontHeight();
    int rowHeight = (int) (fontHeight * ROW_HEIGHT_SCALING_FACTOR);
    int labelOffset = (rowHeight - fontHeight) / 2;
    int rowGap = (int) (fontHeight * ROW_GAP_SCALING_FACTOR);
    int height = (rowHeight + rowGap) * (n + 3);
    int rowSeparation = rowHeight + rowGap;

    BufferedImage image = new BufferedImage(plotWidth, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2 = image.createGraphics();

    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(Math.max(1.0f, 0.65f * scaleFactor)));

    g2.setFont(font);

    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, plotWidth, height);
    g2.setColor(Color.BLACK);

    int offset = rowGap + rowHeight - labelOffset;
    int x0 = drawLabels(g2, offset, rowSeparation, multiGenomeAlignmentSummaries);

    long maxSequenceCount = getMaximumSequenceCount(multiGenomeAlignmentSummaries);
    log.debug("Maximum sequence count: " + maxSequenceCount);

    maxSequenceCount = Math.max(maxSequenceCount, minimumSequenceCount);

    long tickInterval = (int) getTickInterval(maxSequenceCount);
    log.debug("Tick interval: " + tickInterval);
    int tickIntervals = (int) (Math.max(1, maxSequenceCount) / tickInterval);
    if (maxSequenceCount % tickInterval != 0)
        tickIntervals += 1;
    maxSequenceCount = tickIntervals * tickInterval;
    log.debug("No. tick intervals: " + tickIntervals);
    log.debug("Maximum sequence count: " + maxSequenceCount);

    int y = rowGap + n * rowSeparation;
    int x1 = drawAxisAndLegend(g2, x0, y, tickIntervals, maxSequenceCount);

    offset = rowGap;
    drawAlignmentBars(g2, offset, rowHeight, rowSeparation, x0, x1, maxSequenceCount,
            multiGenomeAlignmentSummaries);

    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(imageFilename));
    ImageIO.write(image, "png", out);
    out.close();
}

From source file:at.tuwien.ifs.somtoolbox.apps.viewer.GeneralUnitPNode.java

/** @see edu.umd.cs.piccolo.PNode#paint(edu.umd.cs.piccolo.util.PPaintContext) */
@Override/*w w w.  java 2 s. c  om*/
protected void paint(PPaintContext paintContext) {
    Graphics2D g2d = paintContext.getGraphics();

    if (state.exactUnitPlacement && getMapPNode().getCurrentVisualization() == null) { // black background for sky
        // vis
        border.setRect(X, Y, width, height);
        g2d.setColor(Color.BLACK);
        g2d.fill(border);
    }

    if (drawBorder) {
        GridGeometry helper = state.growingSOM.getLayer().getGridGeometry();
        Vector3D[] v = helper.shapeLinePoints(X, Y, width, height);

        g2d.setStroke(borderStroke);
        g2d.setPaint(Color.CYAN);
        g2d.setColor(borderColor);
        g2d.drawLine((int) v[0].getX(), (int) v[0].getY(), (int) v[1].getX(), (int) v[1].getY());

        for (int i = 1; i < v.length; i++) {
            int i_1 = (i + 1) % v.length;
            g2d.drawLine((int) v[i].getX(), (int) v[i].getY(), (int) v[i_1].getX(), (int) v[i_1].getY());
        }

        border.setRect(helper.getBorder(X, Y, width, height));
    }

    PCamera pCam = paintContext.getCamera();
    if (!((PCanvas) pCam.getComponent()).getClass().equals(MapOverviewPane.MapOverviewCanvas.class)) { // only for
        // main
        // display
        currentScale = paintContext.getScale();
        if (currentScale != oldScale) {
            // System.out.println("SCALE: "+currentScale);
            // double os = t10.getScale();
            // t10.setScale(1/currentScale);
            // if (t10.getGlobalFullBounds().width>this.width) {
            // t10.setScale(os);
            // }

            if (currentScale < state.scaleLimits[1]) { // no information
                if (currentDetailLevel != DETAIL_LEVEL_NO) {
                    currentDetailLevel = DETAIL_LEVEL_NO;
                    detailChanged();
                }
            } else if (currentScale >= state.scaleLimits[1] && currentScale < state.scaleLimits[2]) { // little
                // information
                if (currentDetailLevel != DETAIL_LEVEL_LOW) {
                    currentDetailLevel = DETAIL_LEVEL_LOW;
                    detailChanged();
                }
            } else if (currentScale >= state.scaleLimits[2] && currentScale < state.scaleLimits[3]) { // more labels
                if (currentDetailLevel != DETAIL_LEVEL_MEDIUM) {
                    currentDetailLevel = DETAIL_LEVEL_MEDIUM;
                    detailChanged();
                }
            } else if (currentScale >= state.scaleLimits[3]) { // detailed information
                if (currentDetailLevel != DETAIL_LEVEL_HIGH) {
                    currentDetailLevel = DETAIL_LEVEL_HIGH;
                    detailChanged();
                }
            }

            oldScale = currentScale;
        }
    }
}

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

/**
 * Draws the scale on the dial plot.//  ww w  . j a  v  a 2  s.  com
 *
 * @param g2  the graphics target (<code>null</code> not permitted).
 * @param plot  the dial plot (<code>null</code> not permitted).
 * @param frame  the reference frame that is used to construct the
 *     geometry of the plot (<code>null</code> not permitted).
 * @param view  the visible part of the plot (<code>null</code> not 
 *     permitted).
 */
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    Rectangle2D arcRect = DialPlot.rectangleByRadius(frame, this.getTickRadius(), this.getTickRadius());
    Rectangle2D arcRectMajor = DialPlot.rectangleByRadius(frame,
            this.getTickRadius() - this.getMajorTickLength(), this.getTickRadius() - this.getMajorTickLength());
    Rectangle2D arcRectMinor = arcRect;
    if (this.getMinorTickCount() > 0 && this.getMinorTickLength() > 0.0) {
        arcRectMinor = DialPlot.rectangleByRadius(frame, this.getTickRadius() - this.getMinorTickLength(),
                this.getTickRadius() - this.getMinorTickLength());
    }
    Rectangle2D arcRectForLabels = DialPlot.rectangleByRadius(frame,
            this.getTickRadius() - this.getTickLabelOffset(), this.getTickRadius() - this.getTickLabelOffset());

    boolean firstLabel = true;

    Arc2D arc = new Arc2D.Double();
    Line2D workingLine = new Line2D.Double();
    Stroke arcStroke = new BasicStroke(0.75f);

    for (double v = this.getLowerBound(); v <= this.getUpperBound(); v += this.getMajorTickIncrement()) {
        arc.setArc(arcRect, this.getStartAngle(), valueToAngle(v) - this.getStartAngle(), Arc2D.OPEN);
        g2.setPaint(this.getMajorTickPaint());
        g2.setStroke(arcStroke);
        g2.draw(arc);

        Point2D pt0 = arc.getEndPoint();
        arc.setArc(arcRectMajor, this.getStartAngle(), valueToAngle(v) - this.getStartAngle(), Arc2D.OPEN);
        Point2D pt1 = arc.getEndPoint();
        g2.setPaint(this.getMajorTickPaint());
        g2.setStroke(this.getMajorTickStroke());
        workingLine.setLine(pt0, pt1);
        g2.draw(workingLine);
        arc.setArc(arcRectForLabels, this.getStartAngle(), valueToAngle(v) - this.getStartAngle(), Arc2D.OPEN);
        Point2D pt2 = arc.getEndPoint();

        if (this.getTickLabelsVisible()) {
            if (!firstLabel || this.getFirstTickLabelVisible()) {
                g2.setFont(this.getTickLabelFont());
                TextUtilities.drawAlignedString(this.getTickLabelFormatter().format(v), g2, (float) pt2.getX(),
                        (float) pt2.getY(), TextAnchor.CENTER);
            }
        }
        firstLabel = false;

        // now do the minor tick marks
        if (this.getMinorTickCount() > 0 && this.getMinorTickLength() > 0.0) {
            double minorTickIncrement = this.getMajorTickIncrement() / (this.getMinorTickCount() + 1);
            for (int i = 0; i < this.getMinorTickCount(); i++) {
                double vv = v + ((i + 1) * minorTickIncrement);
                if (vv >= this.getUpperBound()) {
                    break;
                }
                double angle = valueToAngle(vv);

                arc.setArc(arcRect, this.getStartAngle(), angle - this.getStartAngle(), Arc2D.OPEN);
                pt0 = arc.getEndPoint();
                arc.setArc(arcRectMinor, this.getStartAngle(), angle - this.getStartAngle(), Arc2D.OPEN);
                Point2D pt3 = arc.getEndPoint();
                g2.setStroke(this.getMinorTickStroke());
                g2.setPaint(this.getMinorTickPaint());
                workingLine.setLine(pt0, pt3);
                g2.draw(workingLine);
            }
        }

    }
}

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

/**
 * Draws the scale on the dial plot./*from   www  .  j  a  v a 2 s  .  co  m*/
 *
 * @param g2  the graphics target (<code>null</code> not permitted).
 * @param plot  the dial plot (<code>null</code> not permitted).
 * @param frame  the reference frame that is used to construct the
 *     geometry of the plot (<code>null</code> not permitted).
 * @param view  the visible part of the plot (<code>null</code> not 
 *     permitted).
 */
@Override
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    Rectangle2D arcRect = DialPlot.rectangleByRadius(frame, this.getTickRadius(), this.getTickRadius());
    Rectangle2D arcRectMajor = DialPlot.rectangleByRadius(frame,
            this.getTickRadius() - this.getMajorTickLength(), this.getTickRadius() - this.getMajorTickLength());
    Rectangle2D arcRectMinor = arcRect;
    if (this.getMinorTickCount() > 0 && this.getMinorTickLength() > 0.0) {
        arcRectMinor = DialPlot.rectangleByRadius(frame, this.getTickRadius() - this.getMinorTickLength(),
                this.getTickRadius() - this.getMinorTickLength());
    }
    Rectangle2D arcRectForLabels = DialPlot.rectangleByRadius(frame,
            this.getTickRadius() - this.getTickLabelOffset(), this.getTickRadius() - this.getTickLabelOffset());

    boolean firstLabel = true;

    Arc2D arc = new Arc2D.Double();
    Line2D workingLine = new Line2D.Double();
    Stroke arcStroke = new BasicStroke(0.75f);

    for (double v = this.getLowerBound(); v <= this.getUpperBound(); v += this.getMajorTickIncrement()) {
        arc.setArc(arcRect, this.getStartAngle(), valueToAngle(v) - this.getStartAngle(), Arc2D.OPEN);
        g2.setPaint(this.getMajorTickPaint());
        g2.setStroke(arcStroke);
        g2.draw(arc);

        Point2D pt0 = arc.getEndPoint();
        arc.setArc(arcRectMajor, this.getStartAngle(), valueToAngle(v) - this.getStartAngle(), Arc2D.OPEN);
        Point2D pt1 = arc.getEndPoint();
        g2.setPaint(this.getMajorTickPaint());
        g2.setStroke(this.getMajorTickStroke());
        workingLine.setLine(pt0, pt1);
        g2.draw(workingLine);
        arc.setArc(arcRectForLabels, this.getStartAngle(), valueToAngle(v) - this.getStartAngle(), Arc2D.OPEN);
        Point2D pt2 = arc.getEndPoint();

        if (this.getTickLabelsVisible()) {
            if (!firstLabel || this.getFirstTickLabelVisible()) {
                g2.setFont(this.getTickLabelFont());
                TextUtilities.drawAlignedString(this.getTickLabelFormatter().format(v), g2, (float) pt2.getX(),
                        (float) pt2.getY(), TextAnchor.CENTER);
            }
        }
        firstLabel = false;

        // now do the minor tick marks
        if (this.getMinorTickCount() > 0 && this.getMinorTickLength() > 0.0) {
            double minorTickIncrement = this.getMajorTickIncrement() / (this.getMinorTickCount() + 1);
            for (int i = 0; i < this.getMinorTickCount(); i++) {
                double vv = v + ((i + 1) * minorTickIncrement);
                if (vv >= this.getUpperBound()) {
                    break;
                }
                double angle = valueToAngle(vv);

                arc.setArc(arcRect, this.getStartAngle(), angle - this.getStartAngle(), Arc2D.OPEN);
                pt0 = arc.getEndPoint();
                arc.setArc(arcRectMinor, this.getStartAngle(), angle - this.getStartAngle(), Arc2D.OPEN);
                Point2D pt3 = arc.getEndPoint();
                g2.setStroke(this.getMinorTickStroke());
                g2.setPaint(this.getMinorTickPaint());
                workingLine.setLine(pt0, pt3);
                g2.draw(workingLine);
            }
        }

    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.graphics.internal.LogicalPageDrawable.java

private void drawTextDecoration(final FontDecorationSpec decorationSpec) {
    final Graphics2D graphics = (Graphics2D) getGraphics().create();
    graphics.setColor(decorationSpec.getTextColor());
    graphics.setStroke(new BasicStroke((float) decorationSpec.getLineWidth()));
    graphics.draw(new Line2D.Double(decorationSpec.getStart(), decorationSpec.getVerticalPosition(),
            decorationSpec.getEnd(), decorationSpec.getVerticalPosition()));
    graphics.dispose();/*from   w w  w. j  a  v  a  2s .c  om*/
}