繁体   English   中英

在javafx中用不同的颜色为折线的每个段着色

[英]Coloring every segment of polyline with different color in javafx

我正在寻找一种以不同的颜色绘制多段线的每一段(如果更容易,则绘制路径)的方法(第一行蓝色与红色等,但是在一条多段线中)。 或者,是否可以将折线上的每个顶点视为形状,设置不同的颜色,使它们之间的颜色平滑过渡?

编辑:在一个容器中的多行对我来说还不够好,除非有一种方法可以通过视觉方法将它们与strokeLineJoin之类的方法连接起来。 我只是不明白为什么不可能将折线/路径的每个线段都视为一个形状并将其设置为不同的颜色,这似乎是很自然的事情。

这是您想要的先生吗-这就是它的样子

在此处输入图片说明

package application;

import java.util.Random;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

public class Main extends Application {
  @Override
  public void start(Stage primaryStage) {
      try {
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(getLine(10,30),getLine(10,30),getLine(10,30),
                getLine(10,30),getLine(10,30),getLine(10,30),
                 getLine(10,30),getLine(10,30),getLine(10,30),getLine(10,30),
                getLine(10,30),getLine(10,30),getLine(10,30));
        Scene scene = new Scene(root, 400, 400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private Node getLine(int ponts,int pontLength){
    HBox root = new HBox(-1);
    root.setAlignment(Pos.CENTER);
    Random r = new Random();
    while(ponts !=0){
        Path p1 = new Path();
        p1.getElements().addAll(new MoveTo(), new HLineTo(pontLength));
        p1.setStroke(Color.rgb(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
        root.getChildren().add(p1);
        ponts--;
    }

    return root;
}

@Override
public void stop() throws Exception {
    // TODO Auto-generated method stub
    super.stop();
}

 public static void main(String[] args) {
    launch(args);
 }
}

暂无
暂无

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

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