首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX:如何使用GraphicsContext方法appendSVGPath(AppendSVGPath)

JavaFX:如何使用GraphicsContext方法appendSVGPath(AppendSVGPath)
EN

Stack Overflow用户
提问于 2015-06-03 19:37:05
回答 1查看 7K关注 0票数 1

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

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

GraphicsContext有一个方法appendSVGPath(AppendSVGPath),我想我可以用它在空腔上绘制SVGs,但是没有办法让它们出现。

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

我修改了该文件,使其包括以下方法:

代码语言:javascript
复制
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());
}

但我不能让这个形状出现在画布上。

这里有完整的测试代码:

代码语言:javascript
复制
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添加到根用户,则svg将显示。

有人成功地使用了appendSVGPath吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-04 07:58:29

画布不像场景图,抚摸和填充路径不是自动发生的。相反,您需要将路径段提供给画布,然后显式调用填充()笔划()来应用这些操作。有关更多信息,请参见GraphicsContext javadoc前面的“路径呈现”部分。

代码语言:javascript
复制
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();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30629122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档