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:org.openaltimeter.desktopapp.annotations.XYHeightAnnotation.java

@Override
public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis,
        int rendererIndex, PlotRenderingInfo info) {
    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot.getDomainAxisLocation(), orientation);
    RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot.getRangeAxisLocation(), orientation);

    float anchorX = (float) domainAxis.valueToJava2D(this.getX(), dataArea, domainEdge);
    float anchorY = (float) rangeAxis.valueToJava2D(this.getY(), dataArea, rangeEdge);

    if (orientation == PlotOrientation.HORIZONTAL) {
        float tempAnchor = anchorX;
        anchorX = anchorY;//from   www  .ja v  a 2  s.c  om
        anchorY = tempAnchor;
    }

    g2.setFont(getFont());
    Shape hotspot = TextUtilities.calculateRotatedStringBounds(getText(), g2, anchorX, anchorY, getTextAnchor(),
            getRotationAngle(), getRotationAnchor());
    if (this.getBackgroundPaint() != null) {
        g2.setPaint(this.getBackgroundPaint());
        g2.fill(hotspot);
    }

    g2.setPaint(getPaint());
    TextUtilities.drawRotatedString(getText(), g2, anchorX + (3.6f * DOT_SIZE), anchorY + (0.2f * DOT_SIZE),
            getTextAnchor(), getRotationAngle(), getRotationAnchor());
    if (this.isOutlineVisible()) {
        g2.setStroke(this.getOutlineStroke());
        g2.setPaint(this.getOutlinePaint());
        g2.draw(hotspot);
    }

    // cross drawing
    //      g2.setPaint(getPaint());
    //      g2.setStroke(new BasicStroke(1.0f));
    //      g2.drawLine((int) anchorX - CROSS_SIZE / 2, (int) anchorY, (int) anchorX + CROSS_SIZE / 2, (int) anchorY);
    //      g2.drawLine((int) anchorX, (int) anchorY - CROSS_SIZE / 2, (int) anchorX, (int) anchorY + CROSS_SIZE / 2);

    // dot drawing
    g2.setPaint(getPaint());
    g2.setStroke(new BasicStroke(1.0f));
    Ellipse2D e = new Ellipse2D.Double(anchorX - DOT_SIZE / 2, anchorY - DOT_SIZE / 2, DOT_SIZE, DOT_SIZE);
    g2.fill(e);

    String toolTip = getToolTipText();
    String url = getURL();
    if (toolTip != null || url != null) {
        addEntity(info, hotspot, rendererIndex, toolTip, url);
    }
}

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

public void drawPlot(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info) {
    Object originalAntialiasHint = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);

    if (plotAntialias) {
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    } else {//from  w  w w .j  a  v a  2s  .c  o m
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    }

    Composite originalComposite = g2.getComposite();
    if (alpha == true) {
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));
    }

    /* if automatic bounds... */
    if (!isNoData()) {
        if (this instanceof DmcRenderablePlot) {
            DmcPlotRenderer renderer;
            renderer = ((DmcRenderablePlot) this).getPlotRenderer();
            if (renderer != null) {
                renderer.initialize();
            }
        }
    }

    g2.setStroke(stroke);
    g2.setPaint(paint);
    render(g2, dataArea, info); /** This is where the computations method is called.*/
    g2.setComposite(originalComposite);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, originalAntialiasHint);
}

From source file:org.trade.ui.chart.renderer.MACDItemRenderer.java

public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, double x0, double y0, double x1, double y1,
        int lastItem, int series, int item, CrosshairState crosshairState, int pass, int numX, double minX,
        double maxX, Paint color, XYDataset dataset) {

    boolean itemVisible = getItemVisible(series, item);

    // setup for collecting optional entity info...
    Shape entityArea = null;/*from  ww  w .jav  a 2  s .  c o  m*/
    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }

    PlotOrientation orientation = plot.getOrientation();
    Paint paint = getItemPaint(series, item);
    paint = color;
    Stroke seriesStroke = getItemStroke(series, item);
    g2.setPaint(paint);
    g2.setStroke(seriesStroke);

    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);

    if (getPlotLines()) {
        if (this.drawSeriesLineAsPath) {
            State s = (State) state;
            if (s.getSeriesIndex() != series) {
                // we are starting a new series path
                s.seriesPath.reset();
                s.lastPointGood = false;
                s.setSeriesIndex(series);
            }

            // update path to reflect latest point
            if (itemVisible && !Double.isNaN(transX1) && !Double.isNaN(transY1)) {
                float x = (float) transX1;
                float y = (float) transY1;
                if (orientation == PlotOrientation.HORIZONTAL) {
                    x = (float) transY1;
                    y = (float) transX1;
                }
                if (s.isLastPointGood()) {
                    // TODO: check threshold
                    s.seriesPath.lineTo(x, y);
                } else {
                    s.seriesPath.moveTo(x, y);
                }
                s.setLastPointGood(true);
            } else {
                s.setLastPointGood(false);
            }
            if (item == lastItem) {
                if (s.seriesIndex == series) {
                    // draw path
                    g2.setStroke(lookupSeriesStroke(series));
                    g2.setPaint(lookupSeriesPaint(series));
                    g2.draw(s.seriesPath);
                }
            }
        }

        else if (item != 0 && itemVisible) {
            // get the previous data point...

            if (!Double.isNaN(x0) && !Double.isNaN(y0)) {
                boolean drawLine = true;
                if (getPlotDiscontinuous()) {
                    // only draw a line if the gap between the current and
                    // previous data point is within the threshold
                    if (this.gapThresholdType == UnitType.ABSOLUTE) {
                        drawLine = Math.abs(x1 - x0) <= this.gapThreshold;
                    } else {
                        drawLine = Math.abs(x1 - x0) <= ((maxX - minX) / numX * getGapThreshold());
                    }
                }
                if (drawLine) {
                    double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
                    double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);

                    // only draw if we have good values
                    if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1)
                            || Double.isNaN(transY1)) {
                        return;
                    }

                    if (orientation == PlotOrientation.HORIZONTAL) {
                        state.workingLine.setLine(transY0, transX0, transY1, transX1);
                    } else if (orientation == PlotOrientation.VERTICAL) {
                        state.workingLine.setLine(transX0, transY0, transX1, transY1);
                    }

                    if (state.workingLine.intersects(dataArea)) {
                        g2.draw(state.workingLine);
                    }
                }
            }
        }
    }

    // we needed to get this far even for invisible items, to ensure that
    // seriesPath updates happened, but now there is nothing more we need
    // to do for non-visible items...
    if (!itemVisible) {
        return;
    }

    if (getBaseShapesVisible()) {

        Shape shape = getItemShape(series, item);
        if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
        } else if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
        }
        if (shape.intersects(dataArea)) {
            if (getItemShapeFilled(series, item)) {
                g2.fill(shape);
            } else {
                g2.draw(shape);
            }
        }
        entityArea = shape;

    }

    if (getPlotImages()) {
        Image image = getImage(plot, series, item, transX1, transY1);
        if (image != null) {
            Point hotspot = getImageHotspot(plot, series, item, transX1, transY1, image);
            g2.drawImage(image, (int) (transX1 - hotspot.getX()), (int) (transY1 - hotspot.getY()), null);
            entityArea = new Rectangle2D.Double(transX1 - hotspot.getX(), transY1 - hotspot.getY(),
                    image.getWidth(null), image.getHeight(null));
        }

    }

    double xx = transX1;
    double yy = transY1;
    if (orientation == PlotOrientation.HORIZONTAL) {
        xx = transY1;
        yy = transX1;
    }

    // draw the item label if there is one...
    if (isItemLabelVisible(series, item)) {
        drawItemLabel(g2, orientation, dataset, series, item, xx, yy, (y1 < 0.0));
    }

    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, rangeAxisIndex, transX1, transY1,
            orientation);

    // add an entity for the item...
    if (entities != null && isPointInRect(dataArea, xx, yy)) {
        addEntity(entities, entityArea, dataset, series, item, xx, yy);
    }
}

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

protected void configureStroke(final StyleSheet layoutContext, final Graphics2D g2) {
    final Stroke styleProperty = (Stroke) layoutContext.getStyleProperty(ElementStyleKeys.STROKE);
    if (styleProperty != null) {
        g2.setStroke(styleProperty);
    } else {/* w  w w  .ja v  a 2s.c  om*/
        // Apply a default one ..
        g2.setStroke(LogicalPageDrawable.DEFAULT_STROKE);
    }
}

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

/**
 *
 * <P>//from   w w w  .  j  a va 2 s. c  om
 * The tree area needs to be set before this is called!
 */
protected void drawDatesToLeafs(Graphics2D g2, double cursor, Rectangle2D plotArea) {

    double ol = fDateAxis.getTickMarkOutsideLength();

    for (Iterator<Element> li = fLeafNodes.iterator(); li.hasNext();) {
        Element clade = li.next();
        Calendar leafDate = fLeafDates.get(clade);

        /**
         * Check to see if this clade even has a date; not all clades have to have them.
         */
        if (leafDate == null) {
            continue;
        }

        double dateY = fDateAxis.dateToJava2D(leafDate.getTime(), plotArea);

        Point2D datePt = new Point2D.Double(cursor - ol, dateY);

        /**
         * If we are drawing a phylogram then,
         * we need to draw this further towards the tree.
         */
        Point2D nodePt = this.fTreePainter.cladeToJava2D(clade, this.fLeftTreeArea);
        Point2D lfPt = new Point2D.Double(plotArea.getX(), nodePt.getY());

        g2.setPaint(this.getCladeBranchColor(clade));
        g2.setStroke(this.getCladeBranchStroke(clade));
        g2.draw(new Line2D.Double(lfPt, datePt));
    }
}

From source file:com.android.ddmuilib.log.event.OccurrenceRenderer.java

@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {
    TimeSeriesCollection timeDataSet = (TimeSeriesCollection) dataset;

    // get the x value for the series/item.
    double x = timeDataSet.getX(series, item).doubleValue();

    // get the min/max of the range axis
    double yMin = rangeAxis.getLowerBound();
    double yMax = rangeAxis.getUpperBound();

    RectangleEdge domainEdge = plot.getDomainAxisEdge();
    RectangleEdge rangeEdge = plot.getRangeAxisEdge();

    // convert the coordinates to java2d.
    double x2D = domainAxis.valueToJava2D(x, dataArea, domainEdge);
    double yMin2D = rangeAxis.valueToJava2D(yMin, dataArea, rangeEdge);
    double yMax2D = rangeAxis.valueToJava2D(yMax, dataArea, rangeEdge);

    // get the paint information for the series/item
    Paint p = getItemPaint(series, item);
    Stroke s = getItemStroke(series, item);

    Line2D line = null;//from   ww  w.j a  v  a 2s. c om
    PlotOrientation orientation = plot.getOrientation();
    if (orientation == PlotOrientation.HORIZONTAL) {
        line = new Line2D.Double(yMin2D, x2D, yMax2D, x2D);
    } else if (orientation == PlotOrientation.VERTICAL) {
        line = new Line2D.Double(x2D, yMin2D, x2D, yMax2D);
    }
    g2.setPaint(p);
    g2.setStroke(s);
    g2.draw(line);
}

From source file:de.codesourcery.planning.swing.DateAxis.java

public BoundingBox render(ITimelineCallback callback, boolean layoutOnly) {
    final Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);//from w  ww.ja v a 2s.c om
    cal.set(Calendar.MILLISECOND, 0);
    Date currentDate = cal.getTime();

    final Date endDate = duration.addTo(startDate);

    BoundingBox lastLabel = null;

    final int labelSpacing = 10;

    final Graphics2D graphics = callback.getGraphics();
    final int fontHeight = graphics.getFontMetrics().getHeight();

    final double scalingFactor = getXScalingFactor(callback.getBoundingBox());
    final BoundingBox box = callback.getBoundingBox();
    double x = callback.getBoundingBox().getX();
    final int tickToLabelSpacing = 2;
    final int tickLength = fontHeight;
    final int axisHeight = fontHeight + tickLength + tickToLabelSpacing;
    final double xIncrement = Math.floor(tickDuration.toSeconds() * scalingFactor);

    final Color oldColor = graphics.getColor();
    //      
    while (currentDate.compareTo(endDate) <= 0) {
        final int currentX = (int) Math.floor(x);
        if (lastLabel == null || lastLabel.getMaxX() < x) {
            final String labelText = callback.getLabelProvider().getTimelineLabel(currentDate);
            if (!StringUtils.isBlank(labelText)) {
                final Rectangle2D stringBounds = callback.getStringBounds(labelText);

                if (!layoutOnly) {
                    graphics.setColor(Color.BLACK);
                    // draw tick
                    final Stroke oldStroke = graphics.getStroke();
                    graphics.setStroke(new BasicStroke(2.0f));
                    graphics.drawLine(currentX, box.getY() + axisHeight, currentX,
                            box.getY() + fontHeight + tickToLabelSpacing);
                    graphics.setStroke(oldStroke);

                    // draw label
                    callback.drawString(Color.BLACK, currentX, box.getY(), labelText);
                }

                final BoundingBox labelBox = new BoundingBox(currentX, box.getY(),
                        currentX + (int) stringBounds.getWidth() + labelSpacing,
                        box.getY() + (int) stringBounds.getHeight());

                if (lastLabel == null) {
                    lastLabel = labelBox;
                } else {
                    lastLabel.add(labelBox);
                }

            }
        } else {
            // draw short tick
            if (!layoutOnly) {

                final int halfTickHeight = (int) Math.floor(tickLength / 2.0d);

                graphics.drawLine(currentX, box.getY() + axisHeight, currentX,
                        box.getY() + axisHeight - halfTickHeight);
            }
        }

        // draw part of axis
        if (!layoutOnly) {
            graphics.drawLine((int) x, box.getY() + axisHeight, (int) (x + xIncrement),
                    box.getY() + axisHeight);
        }
        x += xIncrement;
        currentDate = tickDuration.addTo(currentDate);
    }

    callback.getGraphics().setColor(oldColor);
    final BoundingBox result = lastLabel != null ? lastLabel : new BoundingBox(0, 0, 0, 0);
    result.incHeight(axisHeight);
    return result;
}

From source file:VASSAL.build.module.map.MapShader.java

public void draw(Graphics g, Map map) {
    if (shadingVisible) {

        double zoom = map.getZoom();
        buildStroke(zoom);/*from w  w  w  .jav a 2 s.c  o  m*/

        final Graphics2D g2 = (Graphics2D) g;

        final Composite oldComposite = g2.getComposite();
        final Color oldColor = g2.getColor();
        final Paint oldPaint = g2.getPaint();
        final Stroke oldStroke = g2.getStroke();

        g2.setComposite(getComposite());
        g2.setColor(getColor());
        g2.setPaint(scaleImage && pattern.equals(TYPE_IMAGE) && imageName != null ? getTexture(zoom)
                : getTexture());
        Area area = getShadeShape(map);
        if (zoom != 1.0) {
            area = new Area(AffineTransform.getScaleInstance(zoom, zoom).createTransformedShape(area));
        }
        g2.fill(area);
        if (border) {
            g2.setComposite(getBorderComposite());
            g2.setStroke(getStroke(map.getZoom()));
            g2.setColor(getBorderColor());
            g2.draw(area);
        }

        g2.setComposite(oldComposite);
        g2.setColor(oldColor);
        g2.setPaint(oldPaint);
        g2.setStroke(oldStroke);
    }
}

From source file:org.jfree.experimental.chart.plot.dial.DialValueIndicator.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  ww w  . j  a  v a2  s  .  co m
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

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

    // calculate the bounds of the template value
    FontMetrics fm = g2.getFontMetrics(this.font);
    String s = this.formatter.format(this.templateValue);
    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.frameAnchor);

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

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

    // draw the border
    g2.setStroke(this.outlineStroke);
    g2.setPaint(this.outlinePaint);
    g2.draw(fb);

    // now find the text anchor point
    double value = plot.getValue(this.datasetIndex);
    String valueStr = this.formatter.format(value);
    Point2D pt2 = RectangleAnchor.coordinates(bounds, this.valueAnchor);
    g2.setPaint(this.paint);
    g2.setFont(this.font);
    TextUtilities.drawAlignedString(valueStr, g2, (float) pt2.getX(), (float) pt2.getY(), this.textAnchor);

}

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

/**
 * Draws the axis line, tick marks and tick mark labels.
 * //from w w w . j a v a  2s.  c o m
 * @param g2  the graphics device.
 * @param cursor  the cursor.
 * @param plotArea  the plot area.
 * @param dataArea  the data area.
 * @param edge  the edge that the axis is aligned with.
 *
 * 
 * Original method is in <code>org.jfree.chart.axis.ValueAxis</code>
 */
protected void drawTickMarksAndLabels(Graphics2D g2, double cursor, Rectangle2D dataArea) {
    //AxisState state = new AxisState(cursor);
    RectangleEdge edge = RectangleEdge.RIGHT;

    drawAxisLine(g2, cursor, dataArea);

    double ol = getTickMarkOutsideLength();
    double il = getTickMarkInsideLength();

    // Their's miss the first and last label
    //List ticks = refreshTicks(g2, state, dataArea, edge);
    List ticks = refreshTicksVertical(g2, dataArea);
    g2.setFont(getTickLabelFont());
    Iterator iterator = ticks.iterator();

    while (iterator.hasNext()) {
        ValueTick tick = (ValueTick) iterator.next();
        if (isTickLabelsVisible()) {
            g2.setPaint(getTickLabelPaint());
            float[] anchorPoint = calculateAnchorPoint(tick, cursor, dataArea, edge);
            TextUtilities.drawRotatedString(tick.getText(), g2, anchorPoint[0], anchorPoint[1],
                    tick.getTextAnchor(), tick.getAngle(), tick.getRotationAnchor());
        }

        if (isTickMarksVisible()) {
            float xx = (float) valueToJava2D(tick.getValue(), dataArea, edge);
            Line2D mark = null;
            g2.setStroke(getTickMarkStroke());
            g2.setPaint(getTickMarkPaint());
            if (edge == RectangleEdge.LEFT) {
                mark = new Line2D.Double(cursor - ol, xx, cursor + il, xx);
            } else if (edge == RectangleEdge.RIGHT) {
                mark = new Line2D.Double(cursor + ol, xx, cursor - il, xx);
            } else if (edge == RectangleEdge.TOP) {
                mark = new Line2D.Double(xx, cursor - ol, xx, cursor + il);
            } else if (edge == RectangleEdge.BOTTOM) {
                mark = new Line2D.Double(xx, cursor + ol, xx, cursor - il);
            }
            g2.draw(mark);
        }
    } // end tick list iterator
}