1

I am trying to change the brightness of the whole scene in javafx. This is what my code looks like at the moment:

public void start(Stage primaryStage) {
  StackPane root = new StackPane();

  Rectangle rec1 = new Rectangle();
  rec1.setWidth(300);
  rec1.setHeight(300);
  rec1.setFill(javafx.scene.paint.Color.RED);

  ColorAdjust colorAdjust = new ColorAdjust();
  colorAdjust.setBrightness(-0.8);

  root.setEffect(colorAdjust);

  Scene scene = new Scene(root, 1920, 1080);
  root.getChildren().add(rec1);

  primaryStage.setFullScreen(true);
  primaryStage.setScene(scene);
  primaryStage.show();
}

The problem is, like this only the brightness of the rectangle changes, but not the brightness of the whole scene. I also need to change the brightness of the "background". Is there any way to do that?

1 Answers1

0

Strangely this seems to be fixed by adding a node to the StackPane in order for it to adjust the color to everything, not just shapes. When shapes are the only thing visible, that is all that's ColorAdjusted. At least one Node must be present. Changing the one line to the following will do what you want:

root.getChildren().addAll(rec1, new Label());

However, this could have consequences to your project by shift something slightly even though it's empty. We can get around this by making it invisible and not-managed so that it isn't considered in layout calculations.

Label fix = new Label("Fix colorAdjust whole scene.");
fix.setVisible(false);
fix.setManaged(false);

Scene scene = new Scene(root, 500, 500);
root.getChildren().addAll(rec1, fix);
Community
  • 1
  • 1
Matthew Wright
  • 1,485
  • 1
  • 14
  • 38