jeudi 31 mai 2018

JavaFx - Coloring a number of paths with different random colors

I have a JSON file of pen strokes which I need to draw in JavaFx UI. Everything is working fine, but now I need to color each stroke with a different color, and originally I used the "path" component which takes a color.

I thought of creating a path for each stroke, save them in an ArrayList and generate random colors to give one to each path. Unfortunately, all strokes are taking the last generated color. I added label beside the strokes and colored them with my generated random colors and that worked just fine with the labels but not with the paths.

How can I give a different color for each path?

Here is the code:

private void setupTimeLine() {

            long maxtime = 0;
            int strokeCount = 1;
            for (InkStroke stroke : strokes) {
                    long time = 0;

                    //get the start point of the stroke
                    double x = stroke.getX(strokeCount-1);
                    double y = stroke.getY(strokeCount-1);
                    System.out.println("Stroke number "+strokeCount+" - start x = "+x+", start y = "+y);

                    //create a path for each stroke with a random color
                    path = new Path();
                    path.setStrokeWidth(1);
                    Random rand = new Random();
                    double r = new Double (rand.nextFloat());
                    double g = new Double (rand.nextFloat());
                    double b = new Double (rand.nextFloat()); 
                    Color randomColor = new Color(r, g, b, 1);
                    System.out.println("Color = "+ randomColor.toString());
                    paths.add(path);
                    colors.add(randomColor);

                    //create a label to numerate each path with the same random color
                    Label number = new Label(Integer.toString(strokeCount));
                    number.setTextFill(randomColor);
                    pane.getChildren().add(number);
                    number.relocate(x, y-30);


                    for (int i = 0; i < stroke.getSize(); i++) {
                         final Integer final_i = new Integer(i);
                         long duration = stroke.getTimestamp(i);
                         KeyFrame frame = new 
                         KeyFrame(Duration.millis(duration + maxtime), ae -> addPoint(final_i, stroke, paths.size()));
                         timeline.getKeyFrames().add(frame);
                         time = duration;    
                     }

                    maxtime += time; 
                    strokeCount= strokeCount + 1;
           }
           // adding paths to UI and color them with random colors
           for (int i=0;i<paths.size();i++){
                paths.get(i).setStroke(colors.get(i));
                pane.getChildren().add(paths.get(i));
           }

}

And here is the result I am getting where all paths are taking the last generated color: enter image description here




Aucun commentaire:

Enregistrer un commentaire