Search in sources :

Example 6 with PathElement

use of javafx.scene.shape.PathElement in project latexdraw by arnobl.

the class ViewPolyPoint method initPath.

private void initPath(final Path path) {
    final ObservableList<PathElement> elts = path.getElements();
    final MoveTo moveTo = ViewFactory.INSTANCE.createMoveTo(0d, 0d);
    moveTo.xProperty().bind(model.getPtAt(0).xProperty());
    moveTo.yProperty().bind(model.getPtAt(0).yProperty());
    elts.add(moveTo);
    IntStream.range(1, model.getNbPoints()).forEach(i -> {
        final LineTo lineto = ViewFactory.INSTANCE.createLineTo(0d, 0d);
        lineto.xProperty().bind(model.getPtAt(i).xProperty());
        lineto.yProperty().bind(model.getPtAt(i).yProperty());
        elts.add(lineto);
    });
}
Also used : PathElement(javafx.scene.shape.PathElement) MoveTo(javafx.scene.shape.MoveTo) LineTo(javafx.scene.shape.LineTo)

Example 7 with PathElement

use of javafx.scene.shape.PathElement in project RichTextFX by FXMisc.

the class TextFlowExt method getUnderlineShape.

/**
 * @param from The index of the first character.
 * @param to The index of the last character.
 * @return An array with the PathElement objects which define an
 *         underline from the first to the last character.
 */
PathElement[] getUnderlineShape(int from, int to) {
    // get a Path for the text underline
    PathElement[] shape = textLayout().getRange(from, to, TextLayout.TYPE_UNDERLINE, 0, 0);
    // The shape is returned as a closed Path (a thin rectangle).
    // If we use the Path as it is, this causes rendering issues.
    // Hence we only use the MoveTo and the succeeding LineTo elements for the result
    // so that simple line segments instead of rectangles are returned.
    List<PathElement> result = new ArrayList<>();
    boolean collect = false;
    for (PathElement elem : shape) {
        if (elem instanceof MoveTo) {
            // There seems to be no API to get the type of the PathElement
            result.add(elem);
            collect = true;
        } else if (elem instanceof LineTo) {
            if (collect) {
                result.add(elem);
                collect = false;
            }
        }
    }
    return result.toArray(new PathElement[0]);
}
Also used : PathElement(javafx.scene.shape.PathElement) MoveTo(javafx.scene.shape.MoveTo) LineTo(javafx.scene.shape.LineTo) ArrayList(java.util.ArrayList)

Example 8 with PathElement

use of javafx.scene.shape.PathElement in project FXyzLib by Birdasaur.

the class Text3DHelper method getPoints.

private void getPoints(PathElement elem) {
    if (elem instanceof MoveTo) {
        list = new ArrayList<>();
        p0 = new Point3D((float) ((MoveTo) elem).getX(), (float) ((MoveTo) elem).getY(), 0f);
        list.add(p0);
    } else if (elem instanceof LineTo) {
        list.add(new Point3D((float) ((LineTo) elem).getX(), (float) ((LineTo) elem).getY(), 0f));
    } else if (elem instanceof CubicCurveTo) {
        Point3D ini = (list.size() > 0 ? list.get(list.size() - 1) : p0);
        IntStream.rangeClosed(1, POINTS_CURVE).forEach(i -> list.add(evalCubicBezier((CubicCurveTo) elem, ini, ((double) i) / POINTS_CURVE)));
    } else if (elem instanceof QuadCurveTo) {
        Point3D ini = (list.size() > 0 ? list.get(list.size() - 1) : p0);
        IntStream.rangeClosed(1, POINTS_CURVE).forEach(i -> list.add(evalQuadBezier((QuadCurveTo) elem, ini, ((double) i) / POINTS_CURVE)));
    } else if (elem instanceof ClosePath) {
        list.add(p0);
        // stored in a LineSegment: a continuous line that can change direction
        if (Math.abs(getArea()) > 0.001) {
            LineSegment line = new LineSegment(text);
            line.setHole(isHole());
            line.setPoints(list);
            line.setPath(generatePath());
            line.setOrigen(p0);
            polis.add(line);
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) Path(javafx.scene.shape.Path) QuadCurveTo(javafx.scene.shape.QuadCurveTo) Color(javafx.scene.paint.Color) ClosePath(javafx.scene.shape.ClosePath) CubicCurveTo(javafx.scene.shape.CubicCurveTo) LineTo(javafx.scene.shape.LineTo) Font(javafx.scene.text.Font) PathElement(javafx.scene.shape.PathElement) Rectangle(javafx.scene.shape.Rectangle) DoubleProperty(javafx.beans.property.DoubleProperty) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Text(javafx.scene.text.Text) List(java.util.List) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) MoveTo(javafx.scene.shape.MoveTo) Point2D(javafx.geometry.Point2D) Shape(javafx.scene.shape.Shape) Point3D(org.fxyz.geometry.Point3D) ClosePath(javafx.scene.shape.ClosePath) MoveTo(javafx.scene.shape.MoveTo) LineTo(javafx.scene.shape.LineTo) QuadCurveTo(javafx.scene.shape.QuadCurveTo) Point3D(org.fxyz.geometry.Point3D) CubicCurveTo(javafx.scene.shape.CubicCurveTo)

Example 9 with PathElement

use of javafx.scene.shape.PathElement in project tilesfx by HanSolo.

the class SmoothedChart method select.

private void select(final MouseEvent EVT) {
    if (!isInteractive()) {
        return;
    }
    final double EVENT_X = EVT.getX();
    final double EVENT_Y = EVT.getY();
    final double CHART_X = chartPlotBackground.getBoundsInParent().getMinX();
    final double CHART_MIN_Y = chartPlotBackground.getBoundsInParent().getMinY();
    final double CHART_HEIGHT = chartPlotBackground.getBoundsInParent().getHeight();
    if (!(getYAxis() instanceof NumberAxis)) {
        return;
    }
    double upperBound = ((NumberAxis) getYAxis()).getUpperBound();
    double lowerBound = ((NumberAxis) getYAxis()).getLowerBound();
    double range = upperBound - lowerBound;
    double factor = range / getYAxis().getLayoutBounds().getHeight();
    List<PathElement> elements = null;
    int noOfElements = 0;
    Bounds pathBounds = null;
    double pathMinX = 0;
    double pathWidth = 0;
    PathElement lastElement = null;
    Series<X, Y> series = null;
    for (Series<X, Y> s : getData()) {
        Path[] paths = getPaths(s);
        // AREA == 0, LINE == 1 in ChartType enum
        int type = getChartType().ordinal();
        if (paths[type].contains(EVENT_X, EVENT_Y)) {
            series = s;
            elements = paths[type].getElements();
            noOfElements = elements.size();
            lastElement = elements.get(0);
            pathBounds = paths[1].getLayoutBounds();
            pathMinX = pathBounds.getMinX();
            pathWidth = pathBounds.getWidth();
            break;
        }
    }
    if (null == series || series.getData().isEmpty()) {
        return;
    }
    if (isSnapToTicks()) {
        double reverseFactor = CHART_HEIGHT / range;
        int noOfDataElements = series.getData().size();
        double interval = pathWidth / (double) (noOfDataElements - 1);
        int selectedIndex = Helper.roundDoubleToInt((EVENT_X - pathMinX) / interval);
        Data<X, Y> selectedData = series.getData().get(selectedIndex);
        Y selectedYValue = selectedData.getYValue();
        if (!(selectedYValue instanceof Number)) {
            return;
        }
        double selectedValue = ((Number) selectedYValue).doubleValue();
        selector.setCenterX(pathMinX + CHART_X + interval * selectedIndex);
        selector.setCenterY((CHART_MIN_Y + CHART_HEIGHT) - (selectedValue * reverseFactor));
        selector.setVisible(true);
        fadeInFadeOut.playFrom(Duration.millis(0));
        Point2D tooltipLocation = selector.localToScreen(selector.getCenterX(), selector.getCenterY());
        String tooltipText = new StringBuilder(selectedData.getXValue().toString()).append("\n").append(selectedData.getYValue()).toString();
        selectorTooltip.setText(tooltipText);
        selectorTooltip.setX(tooltipLocation.getX());
        selectorTooltip.setY(tooltipLocation.getY());
        selectorTooltip.show(getScene().getWindow());
        fireEvent(new SmoothedChartEvent(SmoothedChart.this, null, SmoothedChartEvent.DATA_SELECTED, selectedValue));
    } else {
        for (int i = 1; i < noOfElements; i++) {
            PathElement element = elements.get(i);
            double[] xy = getXYFromPathElement(lastElement);
            double[] xy1 = getXYFromPathElement(element);
            if (xy[0] < 0 || xy[1] < 0 || xy1[0] < 0 || xy1[1] < 0) {
                continue;
            }
            if (EVENT_X > xy[0] && EVENT_X < xy1[0]) {
                double deltaX = xy1[0] - xy[0];
                double deltaY = xy1[1] - xy[1];
                double m = deltaY / deltaX;
                double y = m * (EVT.getX() - xy[0]) + xy[1];
                double selectedValue = ((getYAxis().getLayoutBounds().getHeight() - y) * factor + lowerBound);
                selector.setCenterX(CHART_X + EVT.getX());
                selector.setCenterY(CHART_MIN_Y + y);
                selector.setVisible(true);
                fadeInFadeOut.playFrom(Duration.millis(0));
                Point2D tooltipLocation = selector.localToScreen(selector.getCenterX(), selector.getCenterY());
                String tooltipText = new StringBuilder(String.format(Locale.US, formatString, selectedValue)).toString();
                selectorTooltip.setText(tooltipText);
                selectorTooltip.setX(tooltipLocation.getX());
                selectorTooltip.setY(tooltipLocation.getY());
                selectorTooltip.show(getScene().getWindow());
                fireEvent(new SmoothedChartEvent(SmoothedChart.this, null, SmoothedChartEvent.DATA_SELECTED, selectedValue));
                break;
            }
            lastElement = element;
        }
    }
}
Also used : ClosePath(javafx.scene.shape.ClosePath) Path(javafx.scene.shape.Path) NumberAxis(javafx.scene.chart.NumberAxis) Bounds(javafx.geometry.Bounds) Paint(javafx.scene.paint.Paint) Point(eu.hansolo.tilesfx.tools.Point) SmoothedChartEvent(eu.hansolo.tilesfx.events.SmoothedChartEvent) PathElement(javafx.scene.shape.PathElement) Point2D(javafx.geometry.Point2D)

Example 10 with PathElement

use of javafx.scene.shape.PathElement in project tilesfx by HanSolo.

the class SmoothedChart method smooth.

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());
}
Also used : ClosePath(javafx.scene.shape.ClosePath) PathElement(javafx.scene.shape.PathElement) MoveTo(javafx.scene.shape.MoveTo) LineTo(javafx.scene.shape.LineTo) Point(eu.hansolo.tilesfx.tools.Point) Paint(javafx.scene.paint.Paint) Point(eu.hansolo.tilesfx.tools.Point)

Aggregations

PathElement (javafx.scene.shape.PathElement)10 LineTo (javafx.scene.shape.LineTo)7 MoveTo (javafx.scene.shape.MoveTo)7 ClosePath (javafx.scene.shape.ClosePath)5 Path (javafx.scene.shape.Path)4 Point (eu.hansolo.tilesfx.tools.Point)3 ArrayList (java.util.ArrayList)3 Point2D (javafx.geometry.Point2D)3 Paint (javafx.scene.paint.Paint)3 CubicCurveTo (javafx.scene.shape.CubicCurveTo)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Bounds (javafx.geometry.Bounds)2 ChartData (eu.hansolo.tilesfx.chart.ChartData)1 SmoothedChartEvent (eu.hansolo.tilesfx.events.SmoothedChartEvent)1 TileEvent (eu.hansolo.tilesfx.events.TileEvent)1 IntStream (java.util.stream.IntStream)1 DoubleProperty (javafx.beans.property.DoubleProperty)1 SimpleDoubleProperty (javafx.beans.property.SimpleDoubleProperty)1 NumberAxis (javafx.scene.chart.NumberAxis)1