[英]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.