Example usage for java.awt.geom Line2D clone

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

Introduction

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

Prototype

public Object clone() 

Source Link

Document

Creates a new object of the same class as this object.

Usage

From source file:ShapeTransform.java

/**
 * Normalize the line; the point with the lowest X is the primary point, if
 * both points have the same X, that point with the lowest Y value wins.
 * /*from   w w  w. java2 s  . c o  m*/
 * @param line
 *          the original line
 * @return the normalized line
 */
private static Line2D getNormalizedLine(final Line2D line) {
    final Line2D lineClone = (Line2D) line.clone();

    final Point2D p1 = line.getP1();
    final Point2D p2 = line.getP2();
    if (p1.getX() < p2.getX()) {
        return lineClone;
    }
    if (p1.getX() > p2.getX()) {
        lineClone.setLine(p2, p1);
        return lineClone;
    }
    if (p1.getY() < p2.getY()) {
        return lineClone;
    }
    lineClone.setLine(p2, p1);
    return lineClone;
}

From source file:ShapeTransform.java

/**
 * Translates a given shape. Special care is taken to preserve the shape's
 * original class, if the shape is a rectangle or a line.
 * //from w w  w  .j  a  v a 2 s .c  o m
 * @param s
 *          the shape
 * @param x
 *          the x coordinate where the shape is translated to
 * @param y
 *          the y coordinate where the shape is translated to
 * @return the translated shape
 */
public static Shape translateShape(final Shape s, final double x, final double y) {
    if (s instanceof RectangularShape) {
        final RectangularShape rect = (RectangularShape) s;
        final RectangularShape retval = (RectangularShape) rect.clone();
        retval.setFrame(retval.getX() + x, retval.getY() + y, retval.getWidth(), retval.getHeight());
        return retval;
    }
    if (s instanceof Line2D) {
        final Line2D line = (Line2D) s;
        final Line2D retval = (Line2D) line.clone();
        retval.setLine(retval.getX1() + x, retval.getY1() + y, retval.getX2() + x, retval.getY2() + y);
        return retval;
    }

    final AffineTransform af = AffineTransform.getTranslateInstance(x, y);
    return af.createTransformedShape(s);
}

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

public static Shape translateChartShape(Shape shape, Rectangle2D imageArea, JFreeChart chart) {
    if (shape instanceof Line2D) {
        Line2D line = (Line2D) shape;
        double length = line.getP1().distance(line.getP2());
        if (length == 0) {
            Point2D point = line.getP1();
            Point2D newPoint = ChartMaskingUtilities.translateChartPoint(point, imageArea, chart);
            Shape oShape = ShapeUtilities.createDiagonalCross(5f, 0.2f);
            //             Shape oShape = ShapeUtilities.createRegularCross(3f, 0.5f);
            Shape newShape = ShapeUtilities.createTranslatedShape(oShape, newPoint.getX(), newPoint.getY());
            return newShape;
        } else if (length < 1e-6) {
            if (line.getP1().getX() == line.getP2().getX()) {
                double newX = ChartMaskingUtilities.translateChartPoint(line.getP1(), imageArea, chart).getX();
                Line2D newLine = new Line2D.Double(newX, imageArea.getMinY(), newX, imageArea.getMaxY());
                return newLine;
            } else {
                double newY = ChartMaskingUtilities.translateChartPoint(line.getP1(), imageArea, chart).getY();
                Line2D newLine = new Line2D.Double(imageArea.getMinX(), newY, imageArea.getMaxX(), newY);
                return newLine;
            }/*from  w  w  w.jav a2 s.c  o m*/
        }
        Line2D newShape = (Line2D) line.clone();
        Point2D newP1 = translateChartPoint(line.getP1(), imageArea, chart);
        Point2D newP2 = translateChartPoint(line.getP2(), imageArea, chart);
        newShape.setLine(newP1, newP2);
        return newShape;
    } else if (shape instanceof RectangularShape) {
        RectangularShape rect = (RectangularShape) shape;
        RectangularShape newShape = (RectangularShape) rect.clone();
        Rectangle2D bound = rect.getBounds2D();
        Point2D start = new Point2D.Double(bound.getMinX(), bound.getMinY());
        Point2D end = new Point2D.Double(bound.getMaxX(), bound.getMaxY());
        Point2D screenStart = translateChartPoint(start, imageArea, chart);
        Point2D screenEnd = translateChartPoint(end, imageArea, chart);
        newShape.setFrame(new Rectangle2D.Double(Math.min(screenStart.getX(), screenEnd.getX()),
                Math.min(screenStart.getY(), screenEnd.getY()), Math.abs(screenStart.getX() - screenEnd.getX()),
                Math.abs(screenStart.getY() - screenEnd.getY())));
        return newShape;
    } else {
        return shape;
    }
}

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  .ja  va 2  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;/* w  w  w .  j  av  a 2 s. c  om*/
    }
    newLine.setLine(ChartMaskingUtilities.translateScreenX(minX + marker.getX1(), imageArea, getChart()),
            marker.getY1(),
            ChartMaskingUtilities.translateScreenX(minX + marker.getX2(), imageArea, getChart()),
            marker.getY2());
    return newLine;
}