JavaFX 和 SVG:画一条线 [英] JavaFX and SVG : drawing a line

查看:60
本文介绍了JavaFX 和 SVG:画一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JavaFX 来渲染一些 SVG 内容.我定义了很多方法,返回不同 SVG 形状(省略号、圆形、矩形、线条等)的路径.除了 line 方法之外,所有这些似乎都有效.JavaFX 不会返回错误(意味着路径可能是正确的),但它不会绘制任何内容.这是我的方法.

public static SVGPath line(float startX, float endX, float startY, float endY, PositionType positionType){SVGPath 路径 = new SVGPath();path.setContent(positionType.getMoveto()+startX+","+startY+positionType.getLineto("l")+endX+","+endY);返回路径;}

方法 getMoveto() 返回 Mm,具体取决于 PositionTypegetLineto() 返回 Ll.

这是一个示例方法调用:

SVGPath test2 = SVGPrimitives.line(20f, 30.1f, 23f, 89.21f, PositionType.ABSOLUTE);

这是返回的路径:

M20.0,23.0 L 30.1,89.21

在我看来确实有效,但没有绘制任何内容......

解决方案

An

root.getChildren().addAll(line(32), line(48), line(64));…私有 SVGPath 行(整数大小){SVGPath 路径 = new SVGPath();path.setStroke(Color.BLUE);path.setContent("M0,0L" + size + "," + size + "z");返回路径;}

这同样适用于

import java.util.function.IntFunction;导入 javafx.application.Application;导入 javafx.geometry.Insets;导入 javafx.geometry.Pos;导入 javafx.scene.Scene;导入 javafx.scene.layout.HBox;导入 javafx.scene.layout.VBox;导入 javafx.scene.paint.Color;导入 javafx.scene.shape.SVGPath;导入 javafx.stage.Stage;/*** @see http://www.w3.org/TR/SVG/paths.html* @see http://raphaeljs.com/icons/*/公共类 SVGIcons 扩展应用程序 {私有静态最终 int SIZE = 16;@覆盖公共无效开始(阶段阶段){VBox 根 = 新 VBox(10);root.setAlignment(Pos.CENTER);root.setPadding(new Insets(10));root.getChildren().add(createRow(this::lines));root.getChildren().add(createRow(this::curve));root.getChildren().add(createRow(this::arc));场景场景 = 新场景(根);stage.setTitle("SVGIcons");stage.setScene(场景);舞台表演();}私有 HBox createRow(IntFunction path) {HBox 行 = 新 HBox(10);row.setAlignment(Pos.CENTER);for (int i = 2; i <6; i++) {row.getChildren().add(path.apply(i * SIZE));}返回行;}私有 SVGPath 行(整数大小){SVGPath 路径 = new SVGPath();path.setFill(Color.ALICEBLUE);path.setStroke(Color.BLUE);path.setContent("M0," + 尺寸 + "L" + 尺寸/2 + ",0 "+ size + "," + size + " " + size/2 + "," + 2 * size/3 + "z");返回路径;}私有 SVGPath 曲线(整数大小){SVGPath 路径 = new SVGPath();path.setFill(Color.HONEYDEW);path.setStroke(Color.GREEN);path.setContent("M0,0Q" + 大小 + ",0,"+ 尺寸 + "," + 尺寸 + "L0," + 尺寸 + "z");返回路径;}私有 SVGPath 弧(整数大小){SVGPath 路径 = new SVGPath();path.setFill(Color.MISTYROSE);path.setStroke(Color.RED);path.setContent("M0,0A" + 尺寸/2 + "," + 尺寸+ ",0,1,0," + 大小 + ",0z");返回路径;}公共静态无效主(字符串 [] args){发射(参数);}}

I am using JavaFX to render some SVG stuff. I defined many methods, returning the paths of different SVG shapes (ellipsis, circle, rectangle, lines, etc). All of them seem to work, except the line method. JavaFX doesn't return an error (meaning that the path is probably correct), yet it doesn't draw anything. Here is my method.

public static SVGPath line(float startX, float endX, float startY, float endY, PositionType positionType)
{
    SVGPath path = new SVGPath();
    path.setContent(positionType.getMoveto()+startX+","+startY+positionType.getLineto("l")+endX+","+endY);    

    return path;    
}

The method getMoveto() returns either M or m, depending on the PositionType, and getLineto() returns either L or l.

Here is a sample method call :

SVGPath test2 = SVGPrimitives.line(20f, 30.1f, 23f, 89.21f, PositionType.ABSOLUTE);

And here is the path that's returned :

M20.0,23.0 L 30.1,89.21

It does seem valid to me, yet nothing is drawn...

解决方案

An SVGPath containing a single line encloses no area, so no pixels will be rendered. To see the effect, you can use setStroke() on the path, which "Defines parameters of a stroke that is drawn around the outline of a Shape."

root.getChildren().addAll(line(32), line(48), line(64));
…
private SVGPath line(int size) {
    SVGPath path = new SVGPath();
    path.setStroke(Color.BLUE);
    path.setContent("M0,0L" + size + "," + size + "z");
    return path;
}

The same applies to more complex paths shown here. In the example below, note the following

  • A path can be scaled as a function of the size; a slightly different effect can be achieved by altering the scale of the enclosing Pane, as shown here.

  • As an aid in composition, Java 8 makes it easier to pass a function as a parameter, as suggested here.

  • More complex paths can be constructed using an available SVG editor.

Finally, "Consider a builder when faced with many constructor parameters."

import java.util.function.IntFunction;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.SVGPath;
import javafx.stage.Stage;

/**
 * @see http://www.w3.org/TR/SVG/paths.html
 * @see http://raphaeljs.com/icons/
 */
public class SVGIcons extends Application {

    private static final int SIZE = 16;

    @Override
    public void start(Stage stage) {
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));
        root.getChildren().add(createRow(this::lines));
        root.getChildren().add(createRow(this::curve));
        root.getChildren().add(createRow(this::arc));
        Scene scene = new Scene(root);
        stage.setTitle("SVGIcons");
        stage.setScene(scene);
        stage.show();
    }

    private HBox createRow(IntFunction<SVGPath> path) {
        HBox row = new HBox(10);
        row.setAlignment(Pos.CENTER);
        for (int i = 2; i < 6; i++) {
            row.getChildren().add(path.apply(i * SIZE));
        }
        return row;
    }

    private SVGPath lines(int size) {
        SVGPath path = new SVGPath();
        path.setFill(Color.ALICEBLUE);
        path.setStroke(Color.BLUE);
        path.setContent("M0," + size + "L" + size / 2 + ",0 "
            + size + "," + size + " " + size / 2 + "," + 2 * size / 3 + "z");
        return path;
    }

    private SVGPath curve(int size) {
        SVGPath path = new SVGPath();
        path.setFill(Color.HONEYDEW);
        path.setStroke(Color.GREEN);
        path.setContent("M0,0Q" + size + ",0,"
            + size + "," + size + "L0," + size + "z");
        return path;
    }

    private SVGPath arc(int size) {
        SVGPath path = new SVGPath();
        path.setFill(Color.MISTYROSE);
        path.setStroke(Color.RED);
        path.setContent("M0,0A" + size / 2 + "," + size
            + ",0,1,0," + size + ",0z");
        return path;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于JavaFX 和 SVG:画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆