Tristan Gibson
Tristan Gibson

Reputation: 123

How do you apply a filter to JavaFX using CSS?

I've been fiddling with JavaFX and the stylesheets incorporation and was trying to figure out a way to manipulate the images a bit. I know there's filter in CSS, but what about JavaFX -fx?

I have the following:

.root {
    -fx-background-image: url("background.jpg");
    -fx-background-size: cover;
    // -fx-filter: contrast(200%)
}

Which is imported with:

scene.getStylesheets().add(Class.class.getResource("style.css").toExternalForm());

Is there a way to do this, or is there a better alternative to Java GUI with more control? Normal filter: contrast(200%) does not work with JavaFX it seems.

Upvotes: 1

Views: 1357

Answers (1)

Tristan Gibson
Tristan Gibson

Reputation: 123

Actually, I found a way to do this without CSS. JavaFX actually has effects built in that you can use. For example, for lighting, you can use

import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;

And then:

    Light.Distant light = new Light.Distant();

    Lighting lighting = new Lighting(light);
    lighting.setSurfaceScale(5.0);

Set up azimuth:

DoubleProperty azimuth = new SimpleDoubleProperty(0);
azimuth.bind(value);

From there, you can apply it on any object:

light.setAzimuth(azimuth.get());
lighting.setLight(light);
sampletext.setEffect(lighting);

Alter the "value" the azimuth is bound to and it should change the lighting.

Referenced: http://zetcode.com/gui/javafx/effects/

Upvotes: 1

Related Questions