简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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