Example usage for java.awt.geom Line2D setLine

List of usage examples for java.awt.geom Line2D setLine

Introduction

In this page you can find the example usage for java.awt.geom Line2D setLine.

Prototype

public abstract void setLine(double x1, double y1, double x2, double y2);

Source Link

Document

Sets the location of the end points of this Line2D to the specified double coordinates.

Usage

From source file:org.jfree.chart.demo.Performance.java

/**
 * Creates one line, then repeatedly calls the setLine method.
 *
 * @param count  the number of times to call the setLine method.
 *//*from   w w  w. ja v a2s. c om*/
public void setLines(final int count) {

    final Line2D line = new Line2D.Double(0.0, 0.0, 0.0, 0.0);
    for (int i = 0; i < count; i++) {
        line.setLine(1.0, 1.0, 1.0, 1.0);
    }

}

From source file:umontreal.iro.lecuyer.charts.EmpiricalRenderer.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 axis.//ww  w .  jav  a 2 s. com
 * @param rangeAxis    the range axis.
 * @param dataset      the dataset.
 * @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.
 */
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 (!getItemVisible(series, item))
        return;
    PlotOrientation orientation = plot.getOrientation();
    java.awt.Paint seriesPaint = getItemPaint(series, item);
    java.awt.Stroke seriesStroke = getItemStroke(series, item);
    g2.setPaint(seriesPaint);
    g2.setStroke(seriesStroke);
    double x0 = dataset.getXValue(series, item);
    double y0 = dataset.getYValue(series, item);
    if (java.lang.Double.isNaN(y0))
        return;
    org.jfree.ui.RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    org.jfree.ui.RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
    double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);

    double x1 = 0, y1 = 0;
    if (item < dataset.getItemCount(series) - 1) {
        x1 = dataset.getXValue(series, item + 1);
        y1 = dataset.getYValue(series, item + 1);
    } else {
        x1 = dataArea.getMaxX();
        y1 = dataArea.getMaxY();
    }

    boolean useFillPaint = getUseFillPaint();
    ;
    boolean drawOutlines = getDrawOutlines();
    if (!java.lang.Double.isNaN(y0)) {
        double transX1;
        double transY1;
        if (item < dataset.getItemCount(series) - 1) {
            transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
            transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
        } else {
            transX1 = x1;
            transY1 = y1;
        }
        Line2D line = state.workingLine;
        if (orientation == PlotOrientation.HORIZONTAL) {
            line.setLine(transY0, transX0, transY0, transX1);
            g2.draw(line);
        } else if (orientation == PlotOrientation.VERTICAL) {
            line.setLine(transX0, transY0, transX1, transY0);
            g2.draw(line);
        }
    }
    if (getItemShapeVisible(series, item)) {
        Shape shape = getItemShape(series, item);
        if (orientation == PlotOrientation.HORIZONTAL)
            shape = ShapeUtilities.createTranslatedShape(shape, transY0, transX0);
        else if (orientation == PlotOrientation.VERTICAL)
            shape = ShapeUtilities.createTranslatedShape(shape, transX0, transY0);
        if (shape.intersects(dataArea)) {
            if (getItemShapeFilled(series, item)) {
                if (useFillPaint)
                    g2.setPaint(getItemFillPaint(series, item));
                else
                    g2.setPaint(getItemPaint(series, item));
                g2.fill(shape);
            }
            if (drawOutlines) {
                if (getUseOutlinePaint())
                    g2.setPaint(getItemOutlinePaint(series, item));
                else
                    g2.setPaint(getItemPaint(series, item));
                g2.setStroke(getItemOutlineStroke(series, item));
                g2.draw(shape);
            }
        }
    }
    if (isItemLabelVisible(series, item)) {
        double xx = transX0;
        double yy = transY0;
        if (orientation == PlotOrientation.HORIZONTAL) {
            xx = transY0;
            yy = transX0;
        }
        drawItemLabel(g2, orientation, dataset, series, item, xx, yy, y0 < 0.0D);
    }
    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x0, y0, domainAxisIndex, rangeAxisIndex, transX0, transY0,
            orientation);
    if (state.getInfo() != null) {
        EntityCollection entities = state.getEntityCollection();
        if (entities != null) {
            int r = getDefaultEntityRadius();
            java.awt.Shape shape = orientation != PlotOrientation.VERTICAL
                    ? ((java.awt.Shape) (new java.awt.geom.Rectangle2D.Double(transY0 - (double) r,
                            transX0 - (double) r, 2 * r, 2 * r)))
                    : ((java.awt.Shape) (new java.awt.geom.Rectangle2D.Double(transX0 - (double) r,
                            transY0 - (double) r, 2 * r, 2 * r)));
            if (shape != null) {
                String tip = null;
                XYToolTipGenerator generator = getToolTipGenerator(series, item);
                if (generator != null)
                    tip = generator.generateToolTip(dataset, series, item);
                String url = null;
                if (getURLGenerator() != null)
                    url = getURLGenerator().generateURL(dataset, series, item);
                XYItemEntity entity = new XYItemEntity(shape, dataset, series, item, tip, url);
                entities.add(entity);
            }
        }
    }
}

From source file:edu.jhuapl.graphs.jfreechart.utils.SparselyLabeledCategoryAxis.java

@SuppressWarnings("unchecked")
@Override/*from   www  . j a va  2  s  . co m*/
public void drawTickMarks(Graphics2D g2, double cursor, Rectangle2D dataArea, RectangleEdge edge,
        AxisState state) {
    Plot p = getPlot();

    if (p == null) {
        return;
    }

    CategoryPlot plot = (CategoryPlot) p;
    double il = getTickMarkInsideLength();
    double ol = getTickMarkOutsideLength();
    Line2D line = new Line2D.Double();
    List categories = plot.getCategoriesForAxis(this);
    int tickEvery = categories.size() / (maxLabeledTicks == 0 ? 1 : maxLabeledTicks);

    if (tickEvery < 1) {
        tickEvery = 1;
    }

    if (edge.equals(RectangleEdge.TOP)) {
        Iterator iterator = categories.iterator();
        int i = 0;

        while (iterator.hasNext()) {
            Comparable key = (Comparable) iterator.next();

            if (i % tickEvery == 0) {
                double x = getCategoryMiddle(key, categories, dataArea, edge);
                g2.setPaint(getTickMarkPaint());
                g2.setStroke(getTickMarkStroke());
                line.setLine(x, cursor, x, cursor + il);
                g2.draw(line);
                line.setLine(x, cursor, x, cursor - ol);
                g2.draw(line);

                if (domainGridlinePaint != null) {
                    drawDomainGridline(g2, plot, dataArea, x);
                }
            }

            i++;
        }

        state.cursorUp(ol);
    } else if (edge.equals(RectangleEdge.BOTTOM)) {
        Iterator iterator = categories.iterator();
        int i = 0;

        while (iterator.hasNext()) {
            Comparable key = (Comparable) iterator.next();

            if (i % tickEvery == 0) {
                double x = getCategoryMiddle(key, categories, dataArea, edge);
                g2.setPaint(getTickMarkPaint());
                g2.setStroke(getTickMarkStroke());
                line.setLine(x, cursor, x, cursor - il);
                g2.draw(line);
                line.setLine(x, cursor, x, cursor + ol);
                g2.draw(line);

                if (domainGridlinePaint != null) {
                    drawDomainGridline(g2, plot, dataArea, x);
                }
            }

            i++;
        }

        state.cursorDown(ol);
    } else if (edge.equals(RectangleEdge.LEFT)) {
        Iterator iterator = categories.iterator();
        int i = 0;

        while (iterator.hasNext()) {
            Comparable key = (Comparable) iterator.next();

            if (i % tickEvery == 0) {
                double y = getCategoryMiddle(key, categories, dataArea, edge);
                g2.setPaint(getTickMarkPaint());
                g2.setStroke(getTickMarkStroke());
                line.setLine(cursor, y, cursor + il, y);
                g2.draw(line);
                line.setLine(cursor, y, cursor - ol, y);
                g2.draw(line);

                if (domainGridlinePaint != null) {
                    drawDomainGridline(g2, plot, dataArea, y);
                }
            }

            i++;
        }

        state.cursorLeft(ol);
    } else if (edge.equals(RectangleEdge.RIGHT)) {
        Iterator iterator = categories.iterator();
        int i = 0;

        while (iterator.hasNext()) {
            Comparable key = (Comparable) iterator.next();

            if (i % tickEvery == 0) {
                double y = getCategoryMiddle(key, categories, dataArea, edge);
                g2.setPaint(getTickMarkPaint());
                g2.setStroke(getTickMarkStroke());
                line.setLine(cursor, y, cursor - il, y);
                g2.draw(line);
                line.setLine(cursor, y, cursor + ol, y);
                g2.draw(line);

                if (domainGridlinePaint != null) {
                    drawDomainGridline(g2, plot, dataArea, y);
                }
            }

            i++;
        }

        state.cursorRight(ol);
    }
}

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

/**
 * divide a Line2D into 2 new Line2Ds that are returned
 * in the passed left and right instances, if non-null
 * @param src the line to divide//from   w  w  w  .j  a va  2 s  .c o  m
 * @param left the left side, or null
 * @param right the right side, or null
 */
protected void subdivide(Line2D src, Line2D left, Line2D right) {
    double x1 = src.getX1();
    double y1 = src.getY1();
    double x2 = src.getX2();
    double y2 = src.getY2();

    double mx = x1 + (x2 - x1) / 2.0;
    double my = y1 + (y2 - y1) / 2.0;
    if (left != null) {
        left.setLine(x1, y1, mx, my);
    }
    if (right != null) {
        right.setLine(mx, my, x2, y2);
    }
}

From source file:LineUtilities.java

/**
 * Clips the specified line to the given rectangle.
 *
 * @param line  the line (<code>null</code> not permitted).
 * @param rect  the clipping rectangle (<code>null</code> not permitted).
 *
 * @return <code>true</code> if the clipped line is visible, and
 *     <code>false</code> otherwise.
 *///  w w w .ja  va  2  s  .  co m
public static boolean clipLine(Line2D line, Rectangle2D rect) {

    double x1 = line.getX1();
    double y1 = line.getY1();
    double x2 = line.getX2();
    double y2 = line.getY2();

    double minX = rect.getMinX();
    double maxX = rect.getMaxX();
    double minY = rect.getMinY();
    double maxY = rect.getMaxY();

    int f1 = rect.outcode(x1, y1);
    int f2 = rect.outcode(x2, y2);

    while ((f1 | f2) != 0) {
        if ((f1 & f2) != 0) {
            return false;
        }
        double dx = (x2 - x1);
        double dy = (y2 - y1);
        // update (x1, y1), (x2, y2) and f1 and f2 using intersections
        // then recheck
        if (f1 != 0) {
            // first point is outside, so we update it against one of the
            // four sides then continue
            if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT && dx != 0.0) {
                y1 = y1 + (minX - x1) * dy / dx;
                x1 = minX;
            } else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT && dx != 0.0) {
                y1 = y1 + (maxX - x1) * dy / dx;
                x1 = maxX;
            } else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM && dy != 0.0) {
                x1 = x1 + (maxY - y1) * dx / dy;
                y1 = maxY;
            } else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP && dy != 0.0) {
                x1 = x1 + (minY - y1) * dx / dy;
                y1 = minY;
            }
            f1 = rect.outcode(x1, y1);
        } else if (f2 != 0) {
            // second point is outside, so we update it against one of the
            // four sides then continue
            if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT && dx != 0.0) {
                y2 = y2 + (minX - x2) * dy / dx;
                x2 = minX;
            } else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT && dx != 0.0) {
                y2 = y2 + (maxX - x2) * dy / dx;
                x2 = maxX;
            } else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM && dy != 0.0) {
                x2 = x2 + (maxY - y2) * dx / dy;
                y2 = maxY;
            } else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP && dy != 0.0) {
                x2 = x2 + (minY - y2) * dx / dy;
                y2 = minY;
            }
            f2 = rect.outcode(x2, y2);
        }
    }

    line.setLine(x1, y1, x2, y2);
    return true; // the line is visible - if it wasn't, we'd have
                 // returned false from within the while loop above

}

From source file:org.gumtree.vis.awt.JChartPanel.java

private Line2D convertDomainAxisMarker(Line2D marker) {
    Line2D newLine = (Line2D) marker.clone();
    Rectangle2D imageArea = getScreenDataArea();
    double maxY = imageArea.getBounds2D().getMaxY();
    if (maxY == 0) {
        isShapeValid = false;/*from ww w. j a  v  a2  s. c o  m*/
    }
    newLine.setLine(marker.getX1(),
            ChartMaskingUtilities.translateScreenY(maxY - marker.getY1(), imageArea, getChart(), 0),
            marker.getX2(),
            ChartMaskingUtilities.translateScreenY(maxY - marker.getY2(), imageArea, getChart(), 0));
    return newLine;
}

From source file:org.gumtree.vis.awt.JChartPanel.java

private Line2D convertRangeAxisMarker(Line2D marker) {
    Line2D newLine = (Line2D) marker.clone();
    Rectangle2D imageArea = getScreenDataArea();
    double minX = imageArea.getBounds2D().getMinX();
    if (imageArea.getBounds2D().getMaxX() == 0) {
        isShapeValid = false;/*from w w  w .j  a  v  a  2s.co m*/
    }
    newLine.setLine(ChartMaskingUtilities.translateScreenX(minX + marker.getX1(), imageArea, getChart()),
            marker.getY1(),
            ChartMaskingUtilities.translateScreenX(minX + marker.getX2(), imageArea, getChart()),
            marker.getY2());
    return newLine;
}

From source file:nl.strohalm.cyclos.utils.jfreeAsymmetric.AsymmetricStatisticalLineAndShapeRenderer.java

/**
 * Draw a single data item./*from w  ww. j  a va 2 s.  c  o  m*/
 * 
 * @param g2 the graphics device.
 * @param state the renderer state.
 * @param dataArea the area in which the data is drawn.
 * @param plot the plot.
 * @param domainAxis the domain axis.
 * @param rangeAxis the range axis.
 * @param dataset the dataset (a {@link StatisticalCategoryDataset} is required).
 * @param row the row index (zero-based).
 * @param column the column index (zero-based).
 * @param pass the pass.
 */
@Override
public void drawItem(final Graphics2D g2, final CategoryItemRendererState state, final Rectangle2D dataArea,
        final CategoryPlot plot, final CategoryAxis domainAxis, final ValueAxis rangeAxis,
        final CategoryDataset dataset, final int row, final int column, final int pass) {

    // nothing is drawn for null...
    final Number v = dataset.getValue(row, column);
    if (v == null) {
        return;
    }
    // *************** This line was changed relative to StatisticalLineAndShapeRenderer*****
    final AsymmetricStatisticalCategoryDataset statData = (AsymmetricStatisticalCategoryDataset) dataset;
    // *************** end of changed line **********************************************

    final Number meanValue = statData.getMeanValue(row, column);

    final PlotOrientation orientation = plot.getOrientation();

    // current data point...
    final double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea,
            plot.getDomainAxisEdge());

    final double y1 = rangeAxis.valueToJava2D(meanValue.doubleValue(), dataArea, plot.getRangeAxisEdge());

    Shape shape = getItemShape(row, column);
    if (orientation == PlotOrientation.HORIZONTAL) {
        shape = ShapeUtilities.createTranslatedShape(shape, y1, x1);
    } else if (orientation == PlotOrientation.VERTICAL) {
        shape = ShapeUtilities.createTranslatedShape(shape, x1, y1);
    }
    if (getItemShapeVisible(row, column)) {

        if (getItemShapeFilled(row, column)) {
            g2.setPaint(getItemPaint(row, column));
            g2.fill(shape);
        } else {
            if (getUseOutlinePaint()) {
                g2.setPaint(getItemOutlinePaint(row, column));
            } else {
                g2.setPaint(getItemPaint(row, column));
            }
            g2.setStroke(getItemOutlineStroke(row, column));
            g2.draw(shape);
        }
    }

    if (getItemLineVisible(row, column)) {
        if (column != 0) {

            final Number previousValue = statData.getValue(row, column - 1);
            if (previousValue != null) {

                // previous data point...
                final double previous = previousValue.doubleValue();
                final double x0 = domainAxis.getCategoryMiddle(column - 1, getColumnCount(), dataArea,
                        plot.getDomainAxisEdge());
                final double y0 = rangeAxis.valueToJava2D(previous, dataArea, plot.getRangeAxisEdge());

                Line2D line = null;
                if (orientation == PlotOrientation.HORIZONTAL) {
                    line = new Line2D.Double(y0, x0, y1, x1);
                } else if (orientation == PlotOrientation.VERTICAL) {
                    line = new Line2D.Double(x0, y0, x1, y1);
                }
                g2.setPaint(getItemPaint(row, column));
                g2.setStroke(getItemStroke(row, column));
                g2.draw(line);
            }
        }
    }

    final RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    final RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    double rectX = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, xAxisLocation);

    rectX = rectX + row * state.getBarWidth();

    g2.setPaint(getItemPaint(row, column));
    // ************* This is the block with changes relative to StatisticalLineAndShapeRenderer *********
    // standard deviation lines
    final Number highValObj = statData.getUpperValue(row, column);
    final Number lowValObj = statData.getLowerValue(row, column);

    if (highValObj != null && lowValObj != null) { // rinke added this test
        double highVal = highValObj.doubleValue();
        double lowVal = lowValObj.doubleValue();
        if (highVal > rangeAxis.getRange().getUpperBound()) {
            highVal = rangeAxis.valueToJava2D(rangeAxis.getRange().getUpperBound(), dataArea, yAxisLocation);
        } else {
            highVal = rangeAxis.valueToJava2D(highVal, dataArea, yAxisLocation);
        }

        if (lowVal < rangeAxis.getRange().getLowerBound()) {
            lowVal = rangeAxis.valueToJava2D(rangeAxis.getRange().getLowerBound(), dataArea, yAxisLocation);
        } else {
            lowVal = rangeAxis.valueToJava2D(lowVal, dataArea, yAxisLocation);
        }
        // ****************** end of changed block **********************************

        if (errorIndicatorPaint != null) {
            g2.setPaint(errorIndicatorPaint);
        } else {
            g2.setPaint(getItemPaint(row, column));
        }
        final Line2D line = new Line2D.Double();
        if (orientation == PlotOrientation.HORIZONTAL) {
            line.setLine(lowVal, x1, highVal, x1);
            g2.draw(line);
            line.setLine(lowVal, x1 - 5.0d, lowVal, x1 + 5.0d);
            g2.draw(line);
            line.setLine(highVal, x1 - 5.0d, highVal, x1 + 5.0d);
            g2.draw(line);
        } else { // PlotOrientation.VERTICAL
            line.setLine(x1, lowVal, x1, highVal);
            g2.draw(line);
            line.setLine(x1 - 5.0d, highVal, x1 + 5.0d, highVal);
            g2.draw(line);
            line.setLine(x1 - 5.0d, lowVal, x1 + 5.0d, lowVal);
            g2.draw(line);
        }

    }

    // draw the item label if there is one...
    if (isItemLabelVisible(row, column)) {
        if (orientation == PlotOrientation.HORIZONTAL) {
            drawItemLabel(g2, orientation, dataset, row, column, y1, x1, (meanValue.doubleValue() < 0.0));
        } else if (orientation == PlotOrientation.VERTICAL) {
            drawItemLabel(g2, orientation, dataset, row, column, x1, y1, (meanValue.doubleValue() < 0.0));
        }
    }

    // collect entity and tool tip information...
    if (state.getInfo() != null) {
        final EntityCollection entities = state.getEntityCollection();
        if (entities != null && shape != null) {
            String tip = null;
            final CategoryToolTipGenerator tipster = getToolTipGenerator(row, column);
            if (tipster != null) {
                tip = tipster.generateToolTip(dataset, row, column);
            }
            String url = null;
            if (getItemURLGenerator(row, column) != null) {
                url = getItemURLGenerator(row, column).generateURL(dataset, row, column);
            }
            final CategoryItemEntity entity = new CategoryItemEntity(shape, tip, url, dataset, row,
                    dataset.getColumnKey(column), column);
            entities.add(entity);

        }

    }

}

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

/**
 * @see Graphics#drawPolyline(int[], int[], int)
 */// ww  w  .ja  v a  2 s. c  om
@Override
public void drawPolyline(final int[] x, final int[] y, final int nPoints) {
    final Line2D line = new Line2D.Double(x[0], y[0], x[0], y[0]);
    for (int i = 1; i < nPoints; i++) {
        line.setLine(line.getX2(), line.getY2(), x[i], y[i]);
        draw(line);
    }
}