Rafael
Rafael

Reputation: 575

setting different Colors to svg paths

I've been trying to set different colors to two SVG paths. However, it seems that one of the SVG Paths get the same properties as the second one. Here is the code:

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

I was expecting the first path to be yellow and the second to be red, but I have this result instead enter image description here

What am I doing wrong? Thanks in advance.

Upvotes: 0

Views: 1089

Answers (1)

Dth
Dth

Reputation: 1976

Take a look at what GraphicsContext#appendSVGPath(String svgpath) does:

Appends an SVG Path string to the current path. If there is no current path the string must then start with either type of move command.

Although they look separated, you're in fact using the same path for both shapes. You want to use the gc.beginPath(); every time you start drawing a new path.

Upvotes: 2

Related Questions