NottmTony
NottmTony

Reputation: 476

JavaFX in Java 1.7 : setEffect cannot undo a Lighting effect for a child in the group

I have a parent group, which has multiple children.

I apply a Lighting effect on the parent group, and this gets applied to all the children, but I want to undo this on the children, and apply a different Lighting effect using a different colour.

Options? I've tried setting the effect to null on the children - no joy. I've tried creating children groups below the parent group.

Is there a way of applying a lighting effect to the child group that effectively undoes the lighting effect on the child?

I know it is meant to work like this, but is there a way to stop this behaviour somehow ?

Minimum reproducible example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.effect.Effect;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

// Trying to override an effect applied at group level on a child in the group
public class TestHarnessMain extends Application
{
    public static Effect createFilteredEffect(final Color colour)
    {
        double LIGHT_ANGLE = 90.0;
        ColorAdjust GREY_EFFECT = new ColorAdjust(0.0, -1.0, 0.0, 0.0);

        final Light.Distant light = new Light.Distant();
        light.setElevation(LIGHT_ANGLE);
        light.setAzimuth(LIGHT_ANGLE);
        light.setColor(colour);
        final Lighting lighting = new Lighting();
        lighting.setLight(light);
        lighting.setSurfaceScale(0);
        lighting.setContentInput(GREY_EFFECT);
        return lighting;
    }

    @Override
    public void start(final Stage stage)
    {
        double ON_COLOUR_COMPONENT = 1.0;
        double OFF_COLOUR_COMPONENT = 0.1;
        double COLOUR_OPACITY = 1.0;

        Effect BLUE_EFFECT = createFilteredEffect(new Color(OFF_COLOUR_COMPONENT, OFF_COLOUR_COMPONENT, ON_COLOUR_COMPONENT, COLOUR_OPACITY));
        Effect RED_EFFECT = createFilteredEffect(new Color(ON_COLOUR_COMPONENT, OFF_COLOUR_COMPONENT, OFF_COLOUR_COMPONENT, COLOUR_OPACITY));

        stage.setTitle("Test Set Effect on Children");
        stage.setWidth(1200);
        stage.setHeight(800);

        // 2 green circles initially
        Circle circle1 = new Circle(50,50, 30);
        circle1.setFill(Color.GREEN);

        Circle circle2 = new Circle(100,100, 20);
        circle2.setFill(Color.GREEN);

        // Add circles to group
        Group group = new Group();
        group.getChildren().add(circle1);
        group.getChildren().add(circle2);

        // Set effect on group - sets both circles blue.
        group.setEffect(BLUE_EFFECT);    // Comment this line to see that circle2 will go to red

        // Try to override group effect on circle 2 (does not work)
        circle2.setEffect(null);
        circle2.setEffect(RED_EFFECT);

        Scene scene = new Scene(group, 500, 300);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(final String... args)
    {
        Application.launch(args);
    }

}

Upvotes: 2

Views: 47

Answers (0)

Related Questions