【问题标题】:JavaFX and SVG : drawing a lineJavaFX 和 SVG:画一条线
【发布时间】:2015-11-13 21:47:24
【问题描述】:

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

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;    
}

方法getMoveto() 返回Mm,具体取决于PositionType,而getLineto() 返回Ll

这是一个示例方法调用:

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

这是返回的路径:

M20.0,23.0 L 30.1,89.21

在我看来确实有效,但没有画出任何东西......

【问题讨论】:

    标签: java svg javafx


    【解决方案1】:

    包含单行的SVGPath 不包含任何区域,因此不会渲染像素。要查看效果,您可以在路径上使用setStroke(),它“定义围绕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;
    }
    

    这同样适用于here 所示的更复杂的路径。在下面的示例中,请注意以下内容

    • 路径可以根据size 进行缩放;通过改变封闭Pane的比例可以实现稍微不同的效果,如图here所示。

    • 正如here 所建议的那样,Java 8 使作为参数传递函数变得更加容易。

    • 可以使用可用的SVG editor 构建更复杂的路径。

    最后,"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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2011-09-30
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      相关资源
      最近更新 更多