Java Code Examples for javafx.scene.shape.LineTo#getX()

The following examples show how to use javafx.scene.shape.LineTo#getX() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParagraphOverlayGraphicFactory.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected Rectangle2D getBounds(int start, int end) {
	PathElement[] shape = getShape(start, end);
	double minX = 0, minY = 0, maxX = 0, maxY = 0;
	for (PathElement pathElement : shape) {
		if (pathElement instanceof MoveTo) {
			MoveTo moveTo = (MoveTo) pathElement;
			minX = maxX = moveTo.getX();
			minY = maxY = moveTo.getY();
		} else if (pathElement instanceof LineTo) {
			LineTo lineTo = (LineTo) pathElement;
			double x = lineTo.getX();
			double y = lineTo.getY();
			minX = Math.min(minX, x);
			minY = Math.min(minY, y);
			maxX = Math.max(maxX, x);
			maxY = Math.max(maxY, y);
		}
	}
	return new Rectangle2D(minX, minY, maxX - minX, maxY - minY);
}
 
Example 2
Source File: SmoothedChart.java    From OEE-Designer with MIT License 5 votes vote down vote up
private void smooth(ObservableList<PathElement> strokeElements, ObservableList<PathElement> fillElements, final double HEIGHT) {
    if (fillElements.isEmpty()) return;
    // as we do not have direct access to the data, first recreate the list of all the data points we have
    final Point[] dataPoints = new Point[strokeElements.size()];
    for (int i = 0; i < strokeElements.size(); i++) {
        final PathElement element = strokeElements.get(i);
        if (element instanceof MoveTo) {
            final MoveTo move = (MoveTo) element;
            dataPoints[i] = new Point(move.getX(), move.getY());
        } else if (element instanceof LineTo) {
            final LineTo line = (LineTo) element;
            final double x = line.getX(), y = line.getY();
            dataPoints[i] = new Point(x, y);
        }
    }
    double firstX = dataPoints[0].getX();
    double lastX  = dataPoints[dataPoints.length - 1].getX();

    Point[] points = Helper.subdividePoints(dataPoints, getSubDivisions());

    fillElements.clear();
    fillElements.add(new MoveTo(firstX, HEIGHT));

    strokeElements.clear();
    strokeElements.add(new MoveTo(points[0].getX(), points[0].getY()));

    for (Point p : points) {
        if (Double.compare(p.getX(), firstX) >= 0) {
            fillElements.add(new LineTo(p.getX(), p.getY()));
            strokeElements.add(new LineTo(p.getX(), p.getY()));
        }
    }

    fillElements.add(new LineTo(lastX, HEIGHT));
    fillElements.add(new LineTo(0, HEIGHT));
    fillElements.add(new ClosePath());
}
 
Example 3
Source File: SmoothedChart.java    From tilesfx with Apache License 2.0 5 votes vote down vote up
private void smooth(ObservableList<PathElement> strokeElements, ObservableList<PathElement> fillElements, final double HEIGHT) {
    if (fillElements.isEmpty()) return;
    // as we do not have direct access to the data, first recreate the list of all the data points we have
    final Point[] dataPoints = new Point[strokeElements.size()];
    for (int i = 0; i < strokeElements.size(); i++) {
        final PathElement element = strokeElements.get(i);
        if (element instanceof MoveTo) {
            final MoveTo move = (MoveTo) element;
            dataPoints[i] = new Point(move.getX(), move.getY());
        } else if (element instanceof LineTo) {
            final LineTo line = (LineTo) element;
            final double x = line.getX(), y = line.getY();
            dataPoints[i] = new Point(x, y);
        }
    }
    double firstX = dataPoints[0].getX();
    double lastX  = dataPoints[dataPoints.length - 1].getX();

    Point[] points = Helper.subdividePoints(dataPoints, getSubDivisions());

    fillElements.clear();
    fillElements.add(new MoveTo(firstX, HEIGHT));

    strokeElements.clear();
    strokeElements.add(new MoveTo(points[0].getX(), points[0].getY()));

    for (Point p : points) {
        if (Double.compare(p.getX(), firstX) >= 0) {
            fillElements.add(new LineTo(p.getX(), p.getY()));
            strokeElements.add(new LineTo(p.getX(), p.getY()));
        }
    }

    fillElements.add(new LineTo(lastX, HEIGHT));
    fillElements.add(new LineTo(0, HEIGHT));
    fillElements.add(new ClosePath());
}
 
Example 4
Source File: Helper.java    From tilesfx with Apache License 2.0 5 votes vote down vote up
public static final Path smoothPath(final ObservableList<PathElement> ELEMENTS, final boolean FILLED) {
    if (ELEMENTS.isEmpty()) { return new Path(); }
    final Point[] dataPoints = new Point[ELEMENTS.size()];
    for (int i = 0; i < ELEMENTS.size(); i++) {
        final PathElement element = ELEMENTS.get(i);
        if (element instanceof MoveTo) {
            MoveTo move   = (MoveTo) element;
            dataPoints[i] = new Point(move.getX(), move.getY());
        } else if (element instanceof LineTo) {
            LineTo line   = (LineTo) element;
            dataPoints[i] = new Point(line.getX(), line.getY());
        }
    }
    double                 zeroY               = ((MoveTo) ELEMENTS.get(0)).getY();
    List<PathElement>      smoothedElements    = new ArrayList<>();
    Pair<Point[], Point[]> result              = calcCurveControlPoints(dataPoints);
    Point[]                firstControlPoints  = result.getKey();
    Point[]                secondControlPoints = result.getValue();
    // Start path dependent on filled or not
    if (FILLED) {
        smoothedElements.add(new MoveTo(dataPoints[0].getX(), zeroY));
        smoothedElements.add(new LineTo(dataPoints[0].getX(), dataPoints[0].getY()));
    } else {
        smoothedElements.add(new MoveTo(dataPoints[0].getX(), dataPoints[0].getY()));
    }
    // Add curves
    for (int i = 2; i < dataPoints.length; i++) {
        final int ci = i - 1;
        smoothedElements.add(new CubicCurveTo(
            firstControlPoints[ci].getX(), firstControlPoints[ci].getY(),
            secondControlPoints[ci].getX(), secondControlPoints[ci].getY(),
            dataPoints[i].getX(), dataPoints[i].getY()));
    }
    // Close the path if filled
    if (FILLED) {
        smoothedElements.add(new LineTo(dataPoints[dataPoints.length - 1].getX(), zeroY));
        smoothedElements.add(new ClosePath());
    }
    return new Path(smoothedElements);
}
 
Example 5
Source File: Helper.java    From OEE-Designer with MIT License 4 votes vote down vote up
public static final Path smoothPath(final ObservableList<PathElement> ELEMENTS, final boolean FILLED) {
	if (ELEMENTS.isEmpty()) {
		return new Path();
	}
	final Point[] dataPoints = new Point[ELEMENTS.size()];
	for (int i = 0; i < ELEMENTS.size(); i++) {
		final PathElement element = ELEMENTS.get(i);
		if (element instanceof MoveTo) {
			MoveTo move = (MoveTo) element;
			dataPoints[i] = new Point(move.getX(), move.getY());
		} else if (element instanceof LineTo) {
			LineTo line = (LineTo) element;
			dataPoints[i] = new Point(line.getX(), line.getY());
		}
	}
	double zeroY = ((MoveTo) ELEMENTS.get(0)).getY();
	List<PathElement> smoothedElements = new ArrayList<>();
	Pair<Point[], Point[]> result = calcCurveControlPoints(dataPoints);
	Point[] firstControlPoints = result.getKey();
	Point[] secondControlPoints = result.getValue();
	// Start path dependent on filled or not
	if (FILLED) {
		smoothedElements.add(new MoveTo(dataPoints[0].getX(), zeroY));
		smoothedElements.add(new LineTo(dataPoints[0].getX(), dataPoints[0].getY()));
	} else {
		smoothedElements.add(new MoveTo(dataPoints[0].getX(), dataPoints[0].getY()));
	}
	// Add curves
	for (int i = 2; i < dataPoints.length; i++) {
		final int ci = i - 1;
		smoothedElements.add(new CubicCurveTo(firstControlPoints[ci].getX(), firstControlPoints[ci].getY(),
				secondControlPoints[ci].getX(), secondControlPoints[ci].getY(), dataPoints[i].getX(),
				dataPoints[i].getY()));
	}
	// Close the path if filled
	if (FILLED) {
		smoothedElements.add(new LineTo(dataPoints[dataPoints.length - 1].getX(), zeroY));
		smoothedElements.add(new ClosePath());
	}
	return new Path(smoothedElements);
}
 
Example 6
Source File: Helper.java    From tilesfx with Apache License 2.0 4 votes vote down vote up
public static final void smoothPath(final Path PATH, final boolean FILLED) {
    List<PathElement> pathElements = PATH.getElements();
    if (pathElements.isEmpty()) { return; }
    final Point[] dataPoints = new Point[pathElements.size()];
    for (int i = 0; i < pathElements.size(); i++) {
        final PathElement element = pathElements.get(i);
        if (element instanceof MoveTo) {
            MoveTo move   = (MoveTo) element;
            dataPoints[i] = new Point(move.getX(), move.getY());
        } else if (element instanceof LineTo) {
            LineTo line   = (LineTo) element;
            dataPoints[i] = new Point(line.getX(), line.getY());
        }
    }
    double                 zeroY               = ((MoveTo) pathElements.get(0)).getY();
    List<PathElement>      smoothedElements    = new ArrayList<>();
    Pair<Point[], Point[]> result              = calcCurveControlPoints(dataPoints);
    Point[]                firstControlPoints  = result.getKey();
    Point[]                secondControlPoints = result.getValue();
    // Start path dependent on filled or not
    if (FILLED) {
        smoothedElements.add(new MoveTo(dataPoints[0].getX(), zeroY));
        smoothedElements.add(new LineTo(dataPoints[0].getX(), dataPoints[0].getY()));
    } else {
        smoothedElements.add(new MoveTo(dataPoints[0].getX(), dataPoints[0].getY()));
    }
    // Add curves
    for (int i = 2; i < dataPoints.length; i++) {
        final int ci = i - 1;
        smoothedElements.add(new CubicCurveTo(
            firstControlPoints[ci].getX(), firstControlPoints[ci].getY(),
            secondControlPoints[ci].getX(), secondControlPoints[ci].getY(),
            dataPoints[i].getX(), dataPoints[i].getY()));
    }
    // Close the path if filled
    if (FILLED) {
        smoothedElements.add(new LineTo(dataPoints[dataPoints.length - 1].getX(), zeroY));
        smoothedElements.add(new ClosePath());
    }
    PATH.getElements().setAll(smoothedElements);
}
 
Example 7
Source File: Shape2Geometry.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Converts the given JavaFX {@link Path} to a
 * {@link org.eclipse.gef.geometry.planar.Path}.
 *
 * @param path
 *            The JavaFX {@link Path} to convert.
 * @return The newly created {@link org.eclipse.gef.geometry.planar.Path}
 *         that describes the given {@link Path}.
 */
public static final org.eclipse.gef.geometry.planar.Path toPath(
		Path path) {
	ObservableList<PathElement> elements = path.getElements();
	org.eclipse.gef.geometry.planar.Path.Segment[] segments = new org.eclipse.gef.geometry.planar.Path.Segment[elements
			.size()];

	for (int i = 0; i < segments.length; i++) {
		PathElement element = elements.get(i);
		if (element instanceof MoveTo) {
			MoveTo moveTo = (MoveTo) element;
			segments[i] = new Segment(Segment.MOVE_TO,
					new Point(moveTo.getX(), moveTo.getY()));
		} else if (element instanceof LineTo) {
			LineTo lineTo = (LineTo) element;
			segments[i] = new Segment(Segment.LINE_TO,
					new Point(lineTo.getX(), lineTo.getY()));
		} else if (element instanceof QuadCurveTo) {
			QuadCurveTo quadTo = (QuadCurveTo) element;
			segments[i] = new Segment(Segment.QUAD_TO,
					new Point(quadTo.getControlX(), quadTo.getControlY()),
					new Point(quadTo.getX(), quadTo.getY()));
		} else if (element instanceof CubicCurveTo) {
			CubicCurveTo cubicTo = (CubicCurveTo) element;
			segments[i] = new Segment(Segment.CUBIC_TO,
					new Point(cubicTo.getControlX1(),
							cubicTo.getControlY1()),
					new Point(cubicTo.getControlX2(),
							cubicTo.getControlY2()),
					new Point(cubicTo.getX(), cubicTo.getY()));
		} else if (element instanceof ClosePath) {
			segments[i] = new Segment(Segment.CLOSE);
		}
	}

	int windingRule = path.getFillRule() == FillRule.EVEN_ODD
			? org.eclipse.gef.geometry.planar.Path.WIND_EVEN_ODD
			: org.eclipse.gef.geometry.planar.Path.WIND_NON_ZERO;

	return new org.eclipse.gef.geometry.planar.Path(windingRule, segments);
}
 
Example 8
Source File: TextFlowLayout.java    From RichTextFX with BSD 2-Clause "Simplified" License 4 votes vote down vote up
int getLineCount() {
   
    if ( lineCount > -1 ) return lineCount;

    lineCount = 0;
    lineMetrics.clear();
    double totLines = 0.0, prevMinY = 1.0, prevMaxY = -1.0;
    int totCharSoFar = 0;

    for ( Node n : flow.getChildren() ) if ( n.isManaged() ) {
       
        Bounds nodeBounds = n.getBoundsInParent();
        int length = (n instanceof Text) ? ((Text) n).getText().length() : 1;
        PathElement[] shape = flow.rangeShape( totCharSoFar, totCharSoFar+length );
        double lines = Math.max( 1.0, Math.floor( shape.length / 5 ) );
        double nodeMinY = Math.max( 0.0, nodeBounds.getMinY() );
        
        if ( nodeMinY >= prevMinY && lines > 1 )  totLines += lines - 1;  // Multiline Text node 
        else if ( nodeMinY >= prevMaxY )  totLines += lines;

        if ( lineMetrics.size() < totLines ) {                            // Add additional lines
           
            if ( shape.length == 0 ) {
               lineMetrics.add( new TextFlowSpan( totCharSoFar, length, nodeMinY, nodeBounds.getWidth(), nodeBounds.getHeight() ) );
                totCharSoFar += length;
            }
            else for ( int ele = 1; ele < shape.length; ele += 5 ) {
                // Calculate the segment's line's length and width up to this point
                LineTo eleLine = (LineTo) shape[ele];
                double segWidth = eleLine.getX(), lineMinY = eleLine.getY();
                double charHeight = ((LineTo) shape[ele+1]).getY() - lineMinY;
                Point2D endPoint = new Point2D( segWidth-1, lineMinY + charHeight / 2 );

                // hitTest queries TextFlow layout internally and returns the position of the
                // last char (nearest endPoint) on the line, irrespective of the current Text node !
                int segLen = flow.hitTest( endPoint ).getCharIndex();
                segLen -= totCharSoFar - 1;

                if ( ele == 1 && nodeMinY < prevMaxY ) {
                    adjustLineMetrics( segLen, segWidth - ((MoveTo) shape[ele-1]).getX(), charHeight );
                }
                else {
                   lineMetrics.add( new TextFlowSpan( totCharSoFar, segLen, lineMinY, segWidth, charHeight ) );
                }

                totCharSoFar += segLen;
            }
        }
        else {
            // Adjust current line metrics with additional Text or Node embedded in this line 
            adjustLineMetrics( length, nodeBounds.getWidth(), nodeBounds.getHeight() );
            totCharSoFar += length;
        }

        prevMaxY = nodeBounds.getMaxY();
        prevMinY = nodeMinY;
    }
    
    lineCount = (int) totLines;
    return lineCount;
}
 
Example 9
Source File: JFXToSVG.java    From latexdraw with GNU General Public License v3.0 4 votes vote down vote up
public SVGPathSegLineto createSVGPathSegLineto(final LineTo elt) {
	return new SVGPathSegLineto(elt.getX(), elt.getY(), !elt.isAbsolute());
}