Patrick
Patrick

Reputation: 3

Javafx darken background

I have FXML application with 10 circles in AnchorPane. I want to hover mouse on one circle and make other 9 and background to darken.

The best I could do was some basic FadeTransition, which only made them disappear, not darken, plus I cant figure out how to select all children of node except one that I have mouse on. Selecting all children except one manually seems not really efficient for more objects.

I tried to google it up, but I just cant find anything. Please, post a link to thread related to similar problem or sample code. Any help would be really appreciated.

Upvotes: 0

Views: 1931

Answers (1)

AlmasB
AlmasB

Reputation: 3407

You can use the following sample. Please note that there are some assumptions made, such as every node in the scene graph is a Shape object and that every shape has a Color object associated with the fill. The sample code is sufficient to derive other solutions related specifically to your use case.

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;

public class SelectionApp extends Application {

    private Pane root = new Pane();

    private Parent createContent() {

        root.setPrefSize(800, 600);

        root.getChildren().add(new Rectangle(800, 600, Color.AQUA));

        for (int i = 0; i < 10; i++) {
            Circle circle = new Circle(25, 25, 25, Color.GREEN);

            // just place them randomly
            circle.setTranslateX(Math.random() * 700);
            circle.setTranslateY(Math.random() * 500);

            circle.setOnMouseEntered(e -> select(circle));
            circle.setOnMouseExited(e -> deselect(circle));

            root.getChildren().add(circle);
        }

        return root;
    }

    private void select(Shape node) {
        root.getChildren()
                .stream()
                .filter(n -> n != node)
                .map(n -> (Shape) n)
                .forEach(n -> n.setFill(darker(n.getFill())));
    }

    private void deselect(Shape node) {
        root.getChildren()
                .stream()
                .filter(n -> n != node)
                .map(n -> (Shape) n)
                .forEach(n -> n.setFill(brighter(n.getFill())));
    }

    private Color darker(Paint c) {
        return ((Color) c).darker().darker();
    }

    private Color brighter(Paint c) {
        return ((Color) c).brighter().brighter();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(createContent());
        primaryStage.setTitle("Darken");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 2

Related Questions