简体   繁体   English

为svg路径设置不同的颜色

[英]setting different Colors to svg paths

I've been trying to set different colors to two SVG paths. 我一直在尝试为两个SVG路径设置不同的颜色。 However, it seems that one of the SVG Paths get the same properties as the second one. 但是,似乎其中一个SVG路径具有与第二个相同的属性。 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 我期望第一个路径是黄色,第二个路径是红色,但是我得到了这个结果 在此处输入图片说明

What am I doing wrong? 我究竟做错了什么? Thanks in advance. 提前致谢。

Take a look at what GraphicsContext#appendSVGPath(String svgpath) does: 看一下GraphicsContext#appendSVGPath(String svgpath)作用:

Appends an SVG Path string to the current path. 将SVG路径字符串附加到当前路径。 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(); 您想使用gc.beginPath(); every time you start drawing a new path. 每次您开始绘制新路径时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM