2017-01-28 34 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();

相關問題