Tuesday, June 21, 2016

55. ColorAdjust

With ColorAdjust effect, we can change hue, contrast, brightness and saturation.


A Text node with linear gradient fill, has the effect applied. We can compare to the other node with same linear gradient fill, but not the effect.


package ex55;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Ex55 extends Application {

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        
        Stop[] stop = new Stop[] {
            new Stop(0,Color.RED),
            new Stop(0.5,Color.GREEN),
            new Stop(1,Color.BLUE)
        };
        CycleMethod ncyc = CycleMethod.NO_CYCLE;
        
        LinearGradient lg = new LinearGradient(
            0.5, 0, 0.5, 1, true, ncyc, stop);

        ColorAdjust colorAdjust = new ColorAdjust();
        colorAdjust.setHue(0.5);
        colorAdjust.setContrast(0.1);
        colorAdjust.setBrightness(0.1);
        colorAdjust.setSaturation(0.2);

        Text text1 = new Text(50,100, "With ColorAdjust");
        text1.setFont(Font.font(32));
        text1.setFill(lg);
        text1.setEffect(colorAdjust);
        
        Text text2 = new Text(50,150, "Without ColorAdjust");
        text2.setFont(Font.font(32));
        text2.setFill(lg);
        
        root.getChildren().addAll(text1, text2);

        Scene scene = new Scene(root, 350, 200, Color.WHITESMOKE);
        stage.setTitle("Example 55. ColorAdjust");
        stage.setScene(scene);
        stage.show();
    }

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

This is the output:


No comments:

Post a Comment