[英]Coloring every segment of polyline with different color in javafx
I am looking for a way to draw every segment of polyline (or path if it is easier) with different color (first line blue conected to red etc. but in one polyline). 我正在寻找一种以不同的颜色绘制多段线的每一段(如果更容易,则绘制路径)的方法(第一行蓝色与红色等,但是在一条多段线中)。 Alternatively is there a way to treat every vertex on polyline as shape, set it different color, making smooth transition of color between them?
或者,是否可以将折线上的每个顶点视为形状,设置不同的颜色,使它们之间的颜色平滑过渡?
Edit: Multiple lines in one container is not good enough for me unless there exist a way to connect them visually with methods like strokeLineJoin. 编辑:在一个容器中的多行对我来说还不够好,除非有一种方法可以通过视觉方法将它们与strokeLineJoin之类的方法连接起来。 I just don't get why it is impossible to treat every segment of polyline/path as a shape and set it with different color, it seems like so natural thing to do.
我只是不明白为什么不可能将折线/路径的每个线段都视为一个形状并将其设置为不同的颜色,这似乎是很自然的事情。
Is this what you want sir - this is how it looks 这是您想要的先生吗-这就是它的样子
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.