1

我正在开发一个使用 SVG 的项目。目前,该程序将 SVG 作为 SVGPath 对象存储在 FXML 文件中。然后将该文件加载到一个组中,然后将其添加到屏幕上。在 FXML 文件中,大约有 300 个这样的 SVGPath。我相信这最终意味着场景图上有 300 个节点。

我最终将不得不扩大 SVGPath 的数量,并且担心在场景中放置更多节点,因此我开始考虑使用 Cavas/GraphicsContext 来代替。

GraphicsContext 有一个方法 appendSVGPath(String svgpath),我想我可以用它在 cavas 上绘制我的 SVG,但我没有任何运气让它们出现。

我使用 Oracle 的 CanvasTest.java 文件作为起点: http ://docs.oracle.com/javafx/2/canvas/jfxpub-canvas.htm

我修改了文件以包含以下方法:

private void appendSVG(GraphicsContext gc) {
     SVGPath svg = new SVGPath();
     svg.setContent("M 100 100 L 300 100 L 200 300 z");
     svg.setFill(Color.RED);
     svg.setStroke(Color.BLUE);
     gc.appendSVGPath(svg.getContent());
}

但我无法让形状出现在画布上。

完整的测试代码在这里:

package canvastest;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.SVGPath;
import javafx.stage.Stage;

public class CanvasTest extends Application {

private Canvas canvas = new Canvas(200, 200);
private GraphicsContext gc = canvas.getGraphicsContext2D();
private Group root = new Group();

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

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Canvas Test");

    appendSVG(gc);

//        SVGPath svg = new SVGPath();
//        svg.setContent("M 100 100 L 300 100 L 200 300 z");
//        svg.setFill(Color.RED);
//        svg.setStroke(Color.BLUE);

    root.getChildren().add(root);
    primaryStage.setScene(new Scene(root, 400, 400));
    primaryStage.show();
}

private void appendSVG(GraphicsContext gc) {
    SVGPath svg = new SVGPath();
    svg.setContent("M 100 100 L 300 100 L 200 300 z");
    svg.setFill(Color.RED);
    svg.setStroke(Color.BLUE);
    gc.appendSVGPath(svg.getContent());
}
}

如果我从一开始就取消注释 SVG 部分,只需将 svg 添加到 root,就会显示 svg。

有没有人使用 appendSVGPath 取得任何成功?

4

1 回答 1

2

画布不像场景图,描边和填充路径不会自动发生。相反,您需要将路径段提供给画布,然后显式调用fill()stroke()以应用这些操作。有关详细信息,请参阅GraphicsContext javadoc前面的“路径渲染”部分。

SVG路径

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class CanvasTest extends Application {

    private Canvas canvas = new Canvas(200, 200);
    private GraphicsContext gc = canvas.getGraphicsContext2D();

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

    @Override
    public void start(Stage stage) {
        appendSVG(gc);

        stage.setScene(new Scene(new Group(canvas)));
        stage.show();
    }

    private void appendSVG(GraphicsContext gc) {
        gc.setFill(Color.RED);
        gc.setStroke(Color.BLUE);
        gc.appendSVGPath("M 50 50 L 150 50 L 100 150 z");
        gc.fill();
        gc.stroke();
    }
}
于 2015-06-04T07:58:29.213 回答