2
\$\begingroup\$
public final class App extends javafx.application.Application {

    @Override
    public void start(Stage primaryStage) {
        var rectangle = new Rectangle(150, 100, Color.BLACK);
        var text = new Text("START");
        text.setFill(Color.WHITE);
        var vbox2 = new VBox(text);
        vbox2.setAlignment(Pos.CENTER);
        var array = new ArrayList<Circle_color>() {
            {
                add(new Circle_color("RED1", "RED2", 1, 2, Color.RED));
                add(new Circle_color("GREEN1", "GREEN2", 3, 4, Color.GREEN));
                add(new Circle_color("BLUE1", "BLUE2", 5, 6, Color.BLUE));
            }
        };
        var array2 = new ArrayList<Circle_white>() {
            {
                add(new Circle_white("RED1", "RED2", 1));
                add(new Circle_white("GREEN1", "GREEN2", 3));
                add(new Circle_white("BLUE1", "BLUE2", 5));
                add(new Circle_white("CYAN1", "CYAN2", 7));
            }
        };
        var vbox = new VBox();
        vbox.getChildren().addAll(array);
        vbox.getChildren().addAll(array2);
        Stream.concat(array.stream(), array2.stream()).forEach((t) -> {
            set(t, rectangle, vbox2);
        });
        var stack_pane = new StackPane(rectangle, vbox2);
        stack_pane.setLayoutX(250);
        stack_pane.setLayoutY(100);
        primaryStage.setScene(new Scene(new Pane(stack_pane, vbox), 800, 800));
        primaryStage.show();
    }

    public void set(My_circle circle, Rectangle rectangle, VBox vbox) {
        circle.setOnMouseEntered((event) -> {
            event.consume();
            var effect = new BoxBlur(0, 0, 1);
            vbox.getChildren().forEach((t) -> {
                t.setEffect(effect);
            });
            var fill = new FillTransition(Duration.seconds(1), rectangle, (Color) rectangle.getFill(), (Color) circle.getFill());
            var timeline = new Timeline(
                    new KeyFrame(Duration.millis(500),
                            new KeyValue(effect.widthProperty(), 10),
                            new KeyValue(effect.heightProperty(), 10)
                    )
            );
            timeline.setOnFinished((event2) -> {
                vbox.getChildren().clear();
                vbox.getChildren().addAll(circle.get());
                vbox.getChildren().forEach((t) -> {
                    t.setEffect(effect);
                });
                new Timeline(
                        new KeyFrame(Duration.millis(500),
                                new KeyValue(effect.widthProperty(), 0),
                                new KeyValue(effect.heightProperty(), 0)
                        )
                ).play();
            });
            new ParallelTransition(fill, timeline).play();
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
...
public final class Circle_color extends My_circle implements Get {

    public String text, text2;
    public int number, number2;

    public Circle_color(String text, String text2, int number, int number2, Color color) {
        super(50, color);
        this.text = text;
        this.text2 = text2;
        this.number = number;
        this.number2 = number2;
    }

    @Override
    public ArrayList<Text> get() {
        return new ArrayList<>() {
            {
                add(new Text(text));
                add(new Text(text2));
                add(new Text(String.valueOf(number)));
                add(new Text(String.valueOf(number2)));
            }
        };
    }
}
...
public final class Circle_white extends My_circle implements Get {

    String text, text2;
    int number;

    public Circle_white(String text, String text2, int number) {
        super(50, Color.WHITE);
        this.text = text;
        this.text2 = text2;
        this.number = number;
        setStroke(Color.BLACK);
    }

    @Override
    public ArrayList<Text> get() {
        return new ArrayList<>() {
            {
                add(new Text(text));
                add(new Text(text2));
                add(new Text(String.valueOf(number)));
            }
        };
    }
}
...
public abstract class My_circle extends javafx.scene.shape.Circle implements Get {

    public My_circle(int number, Color color) {
        super(number, color);
    }
}
...
public interface Get {

    ArrayList<Text> get();
}

I hastily created an example presenting my problem.

This is the code for five files - 4 classes (App, My_Circle, Circle_white, Circle_color) and Get interface.

The program creates 3 Circle_color objects and 4 Circle_white objects and adds them all to the VBox.

And these objects, when you hover over them, create an animation to change the color of the rectangle and change the text.

Better to try.

The program works, but there are a few problems.

1. The set method was originally in both classes (Circle_white and Circle_color)

Since they only differed in the fields used, otherwise they were identical, so I deleted them and created only one in the App class.

The problem was two things

1.1 The method must accept objects of both classes.

That was easy, both classes are extends from the same class, so I used that class as the parameter type.

1.2 Use of fields. For both classes, I created a get method that returns them in the desired form.

Unfortunately, thanks to the solution in 1.1, these methods cannot be used, so I have to create a Get interface. However, it cannot be implemented in the javafx.scene.shape.Circle class, so I have to create My_circle class that is extends from the javafx.scene.shape.Circle and from it are extends of the Circle_color and Circle_white class.

It's simple and functional, but I still want to ask if this is the ideal solution?

2. Using the VBox.

Although this is only part of the example, I still want to ask.

2.1 There is a way to initialize a VBox while adding ArrayList to it. VBox does not have such a constructor.

2.2 How to replace all elements with ArrayList?

3. Code of the set method or the animation itself.

3.1 I have no idea how an animation should behave if I move to another element without completing the current animation. The color always changes from the current color, but how should the text behave?

3.2 If after the text animation is finished, I first replace all elements and call another animation. Both last half a second, which coincides with a one-second color change. Still, I want to make sure that both animations end at the same time.

Please help.

Thank you

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.