2017-01-28 33 views
0

我一直在尝试将不同的颜色设置为两个SVG路径。但是,看起来SVG Paths中的一个获得了与第二个相同的属性。下面是代码:将不同的颜色设置为svg路径

public class MainApp extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
    primaryStage.setTitle("Drawing Operations Test"); 
    Group root = new Group(); 
    Canvas canvas = new Canvas(400, 400); 
    GraphicsContext gc = canvas.getGraphicsContext2D(); 

    gc.setFill(Color.YELLOW); 
    gc.setStroke(Color.YELLOW); 
    gc.appendSVGPath("M 50 50 L 150 50 L 100 150 z"); 
    //gc.fill(); //If I uncomment these two lines, the second 
    //gc.stroke(); //path won't appear 

    gc.setFill(Color.RED); 
    gc.setStroke(Color.BLUE); 
    gc.appendSVGPath("M 200 50 L 300 50 L 250 150 z"); 
    gc.fill(); 
    gc.stroke(); 

    root.getChildren().add(canvas); 
    primaryStage.setScene(new Scene(root)); 
    primaryStage.show(); 
    } 
    public static void main(String[] args) { 
    launch(args); 
    } 
} 

我期待第一个路径是黄色,第二是红色的,但我有这样的结果,而不是 enter image description here

我在做什么错? 在此先感谢。

回答

2

看看什么GraphicsContext#appendSVGPath(String svgpath)做:

追加的SVG路径字符串转换为当前路径。如果没有当前的路径,则该字符串必须以任一类型的移动命令开始。

尽管它们看上去是分开的,但实际上两个形状都使用相同的路径。每次开始绘制新路径时,都想使用gc.beginPath();

相关问题