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:edu.uci.ics.jung.visualization.PluggableRenderer.java

public void paintShapeForVertex(Graphics2D g2d, Vertex v, Shape shape) {
    Paint oldPaint = g2d.getPaint();
    Paint fillPaint = vertexPaintFunction.getFillPaint(v);
    if (fillPaint != null) {
        g2d.setPaint(fillPaint);//from   w  ww . j  a va  2s . c  o m
        g2d.fill(shape);
        g2d.setPaint(oldPaint);
    }
    Paint drawPaint = vertexPaintFunction.getDrawPaint(v);
    if (drawPaint != null) {
        g2d.setPaint(drawPaint);
        g2d.draw(shape);
        g2d.setPaint(oldPaint);
    }
}

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

/**
 *
 * <P>/* w ww.  j a v a  2 s.  co  m*/
 * 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:net.sf.maltcms.common.charts.api.overlay.SelectionOverlay.java

private void drawEntity(Shape entity, Graphics2D g2, Color fill, ChartPanel chartPanel, boolean scale) {
    if (entity != null) {
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        Color c = g2.getColor();//from w ww  . j  a v a2 s.c  om
        Composite comp = g2.getComposite();
        g2.clip(dataArea);
        g2.setColor(fill);
        AffineTransform originalTransform = g2.getTransform();
        Shape transformed = entity;
        if (scale) {
            transformed = scaleAtOrigin(entity, hoverScaleX, hoverScaleY).createTransformedShape(entity);
        }
        transformed = getTranslateInstance(
                entity.getBounds2D().getCenterX() - transformed.getBounds2D().getCenterX(),
                entity.getBounds2D().getCenterY() - transformed.getBounds2D().getCenterY())
                        .createTransformedShape(transformed);
        g2.setComposite(getInstance(AlphaComposite.SRC_OVER, fillAlpha));
        g2.fill(transformed);
        g2.setColor(Color.DARK_GRAY);
        g2.draw(transformed);
        g2.setComposite(comp);
        g2.setColor(c);
        g2.setClip(savedClip);
    }
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.link_and_brush.LinkAndBrushChartPanel.java

/**
 * Draws zoom rectangle (if present). 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./*from   w  w  w  . j a  v a 2  s .c o m*/
 * 
 * @param g2
 *            the graphics device.
 * @param xor
 *            use XOR for drawing?
 */
private void drawZoomRectangle(Graphics2D g2, boolean xor) {
    Rectangle2D zoomRectangle = (Rectangle2D) getChartFieldValueByName("zoomRectangle");
    if (zoomRectangle != null) {
        // fix rectangle parameters when chart is transformed
        zoomRectangle = coordinateTransformation.transformRectangle(zoomRectangle, this);
        if (!(coordinateTransformation instanceof NullCoordinateTransformation)) {
            g2 = coordinateTransformation.getTransformedGraphics(this);
        }
        if (xor) {
            // Set XOR mode to draw the zoom rectangle
            g2.setXORMode(Color.gray);
        }
        if ((Boolean) getChartFieldValueByName("fillZoomRectangle")) {
            g2.setPaint((Paint) getChartFieldValueByName("zoomFillPaint"));
            g2.fill(zoomRectangle);
        } else {
            g2.setPaint((Paint) getChartFieldValueByName("zoomOutlinePaint"));
            g2.draw(zoomRectangle);
        }
        if (xor) {
            // Reset to the default 'overwrite' mode
            g2.setPaintMode();
        }
    }
}

From source file:org.proteosuite.FastScatterPlot.java

/**
 * Draws the gridlines for the plot, if they are visible.
 *
 * @param g2  the graphics device./*from  w  ww.  ja va  2  s.  co  m*/
 * @param dataArea  the data area.
 * @param ticks  the ticks.
 */
protected void drawRangeGridlines(Graphics2D g2, Rectangle2D dataArea, List<ValueTick> ticks) {

    // draw the range grid lines, if any...
    if (isRangeGridlinesVisible()) {
        Stroke gridStroke = getRangeGridlineStroke();
        Paint gridPaint = getRangeGridlinePaint();
        if ((gridStroke != null) && (gridPaint != null)) {
            Iterator<ValueTick> iterator = ticks.iterator();
            while (iterator.hasNext()) {
                ValueTick tick = (ValueTick) iterator.next();
                double v = this.rangeAxis.valueToJava2D(tick.getValue(), dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(dataArea.getMinX(), v, dataArea.getMaxX(), v);
                g2.setPaint(gridPaint);
                g2.setStroke(gridStroke);
                g2.draw(line);
            }
        }
    }

}

From source file:org.proteosuite.FastScatterPlot.java

/**
 * Draws the gridlines for the plot, if they are visible.
 *
 * @param g2  the graphics device./*from   w  w  w . j a v a 2 s .  com*/
 * @param dataArea  the data area.
 * @param ticks  the ticks.
 */
protected void drawDomainGridlines(Graphics2D g2, Rectangle2D dataArea, List<ValueTick> ticks) {

    // draw the domain grid lines, if any...
    if (isDomainGridlinesVisible()) {
        Stroke gridStroke = getDomainGridlineStroke();
        Paint gridPaint = getDomainGridlinePaint();
        if ((gridStroke != null) && (gridPaint != null)) {
            Iterator<ValueTick> iterator = ticks.iterator();
            while (iterator.hasNext()) {
                ValueTick tick = (ValueTick) iterator.next();
                double v = this.domainAxis.valueToJava2D(tick.getValue(), dataArea, RectangleEdge.BOTTOM);
                Line2D line = new Line2D.Double(v, dataArea.getMinY(), v, dataArea.getMaxY());
                g2.setPaint(gridPaint);
                g2.setStroke(gridStroke);
                g2.draw(line);
            }
        }
    }
}

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 w  w w . ja va2  s .c om
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:org.jfree.experimental.chart.renderer.xy.VectorRenderer.java

/**
 * Draws the block representing the specified item.
 * // w  ww  . ja  v  a 2 s  . c om
 * @param g2  the graphics device.
 * @param state  the state.
 * @param dataArea  the data area.
 * @param info  the plot rendering info.
 * @param plot  the plot.
 * @param domainAxis  the x-axis.
 * @param rangeAxis  the y-axis.
 * @param dataset  the dataset.
 * @param series  the series index.
 * @param item  the item index.
 * @param crosshairState  the crosshair state.
 * @param pass  the pass index.
 */
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) {

    double x = dataset.getXValue(series, item);
    double y = dataset.getYValue(series, item);
    double dx = 0.0;
    double dy = 0.0;
    if (dataset instanceof VectorXYDataset) {
        dx = ((VectorXYDataset) dataset).getDeltaXValue(series, item);
        dy = ((VectorXYDataset) dataset).getDeltaYValue(series, item);
    }
    double xx0 = domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge());
    double yy0 = rangeAxis.valueToJava2D(y, dataArea, plot.getRangeAxisEdge());
    double xx1 = domainAxis.valueToJava2D(x + dx, dataArea, plot.getDomainAxisEdge());
    double yy1 = rangeAxis.valueToJava2D(y + dy, dataArea, plot.getRangeAxisEdge());
    Line2D line;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation.equals(PlotOrientation.HORIZONTAL)) {
        line = new Line2D.Double(yy0, xx0, yy1, xx1);
    } else {
        line = new Line2D.Double(xx0, yy0, xx1, yy1);
    }
    g2.setPaint(getItemPaint(series, item));
    g2.setStroke(getItemStroke(series, item));
    g2.draw(line);

    // calculate the arrow head and draw it...
    double dxx = (xx1 - xx0);
    double dyy = (yy1 - yy0);
    double bx = xx0 + (1.0 - this.baseLength) * dxx;
    double by = yy0 + (1.0 - this.baseLength) * dyy;

    double cx = xx0 + (1.0 - this.headLength) * dxx;
    double cy = yy0 + (1.0 - this.headLength) * dyy;

    double angle = 0.0;
    if (dxx != 0.0) {
        angle = Math.PI / 2.0 - Math.atan(dyy / dxx);
    }
    double deltaX = 2.0 * Math.cos(angle);
    double deltaY = 2.0 * Math.sin(angle);

    double leftx = cx + deltaX;
    double lefty = cy - deltaY;
    double rightx = cx - deltaX;
    double righty = cy + deltaY;

    GeneralPath p = new GeneralPath();
    p.moveTo((float) xx1, (float) yy1);
    p.lineTo((float) rightx, (float) righty);
    p.lineTo((float) bx, (float) by);
    p.lineTo((float) leftx, (float) lefty);
    p.closePath();
    g2.draw(p);

}

From source file:org.mwc.cmap.grideditor.chart.RendererWithDynamicFeedback.java

/**
 * All parameters are domain coordinates that have to be translated to
 * Java2D points./*from   w w w.  j  a v  a 2s. c  o m*/
 */
private void drawFeedbackEdge(final double x0, final double y0, final double x1, final double y1,
        final XYItemRendererState state, final Graphics2D g2, final XYPlot plot, final ValueAxis domainAxis,
        final ValueAxis rangeAxis, final Rectangle2D dataArea) {
    if (Double.isNaN(y0) || Double.isNaN(x0) || Double.isNaN(y1) || Double.isNaN(x1)) {
        return;
    }

    final RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    final RectangleEdge yAxisLocation = plot.getRangeAxisEdge();

    final double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
    final double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);

    final double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    final double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);

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

    final PlotOrientation orientation = plot.getOrientation();
    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.setStroke(getFeedbackStroke());
        g2.setPaint(getFeedbackEdgePaint());
        g2.draw(state.workingLine);
    }

}

From source file:edu.ucla.stat.SOCR.motionchart.MotionBubbleRenderer.java

/**
 * Draws the visual representation of a single data item.
 *
 * @param g2             the graphics device.
 * @param state          the renderer state.
 * @param dataArea       the area within which the data is being drawn.
 * @param info           collects information about the drawing.
 * @param plot           the plot (can be used to obtain standard color
 *                       information etc).
 * @param domainAxis     the domain (horizontal) axis.
 * @param rangeAxis      the range (vertical) axis.
 * @param dataset        the dataset (a {@link edu.ucla.stat.SOCR.motionchart.MotionDataSet} is expected).
 * @param series         the series index (zero-based).
 * @param item           the item index (zero-based).
 * @param crosshairState crosshair information for the plot
 *                       (<code>null</code> permitted).
 * @param pass           the pass index.
 *///from  w w w  . ja v  a 2  s  . c  o  m
@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) {
    if (!(dataset instanceof MotionDataSet)) {
        throw new IllegalArgumentException("The dataset must be of type MotionDataSet.");
    }

    // return straight away if the item is not visible
    if (!getItemVisible(series, item)) {
        return;
    }

    PlotOrientation orientation = plot.getOrientation();

    Ellipse2D.Double shape = (Ellipse2D.Double) getItemShape(series, item);

    if (shape.getWidth() != 0 && shape.getHeight() != 0) {
        Ellipse2D.Double circle = translateShape(shape, plot, dataArea);
        g2.setPaint(getItemPaint(series, item));
        g2.fill(circle);
        g2.setStroke(getItemOutlineStroke(series, item));
        g2.setPaint(getItemOutlinePaint(series, item));
        g2.draw(circle);

        if (isItemLabelVisible(series, item)) {
            currCircle = circle;
            if (orientation == PlotOrientation.VERTICAL) {
                drawItemLabel(g2, orientation, dataset, series, item, circle.getCenterX(), circle.getCenterY(),
                        false);
            } else if (orientation == PlotOrientation.HORIZONTAL) {
                drawItemLabel(g2, orientation, dataset, series, item, circle.getCenterY(), circle.getCenterX(),
                        false);
            }
        }

        // add an entity if this info is being collected
        EntityCollection entities;
        if (info != null) {
            entities = info.getOwner().getEntityCollection();
            if (entities != null && circle.intersects(dataArea)) {
                addEntity(entities, circle, dataset, series, item, circle.getCenterX(), circle.getCenterY());
            }
        }

        int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
        int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
        updateCrosshairValues(crosshairState, shape.getCenterX(), shape.getCenterY(), domainAxisIndex,
                rangeAxisIndex, circle.getCenterX(), circle.getCenterY(), orientation);
    }
}