JavaFX Color Adjust
In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation, brightness, input, and contrast. All methods needed for this purpose are present in the javafx.scene.effect.ColorAdjust class.
Properties and corresponding methods to create Color Adjust Effect :
1. hue
This property is of double type. It is used to represent the various adjustment user had made in the hue of the color and the setHue( double huevalue ) method is used to set it.
2. contrast
This property is of double type. It is used to represent the various adjustment user had made in the contrast of the color and the setContrast( double contrastvalue) method is used to set it.
3. brightness
This property is of double type. It is used to represent the various adjustment user had made in the brightness of the color and the setBrightness( double brightnessvalue ) method is used to set it.
4. saturation
This property is of double type. It is used to represent the various adjustment user had made in the saturation of the color and the setSaturation( double saturationvalue ) method is used to set it.
5. input
This property is of double type. It is used to represent the value which is the input for the user and the setInput( double inputvalue ) method is used to set it.
JavaFX Effect – Color Adjust
Example:
import javafx.application.Application;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class EffectUI extends Application {
@Override
public void start(Stage stage) throws Exception
{
Image newimage = new Image("https://icons.iconarchive.com/icons/custom-icon-design/pretty-office-4/256/home-icon.png");
ImageView newimageView = new ImageView(newimage);
newimageView.setX(140);
newimageView.setY(90);
newimageView.setFitHeight(200);
newimageView.setFitWidth(400);
newimageView.setPreserveRatio(true);
ColorAdjust newAdjust = new ColorAdjust();
newAdjust.setContrast(0.3);
newAdjust.setHue(-0.05);
newAdjust.setBrightness(0.9);
newAdjust.setSaturation(0.8);
newimageView.setEffect(newAdjust);
Group root = new Group(newimageView);
Scene scene = new Scene(root,700,450);
stage.setScene(scene);
stage.setTitle("JavaFx Color Adjust Effect example");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output:
In order to create the Color Adjust Effect in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.paint.Color, javafx.scene.shape.Rectangle, javafx.scene.text.Font, javafx.scene.text.FontPosture, javafx.scene.text.FontWeight, javafx.scene.text.Text, javafx.stage.Stage, javafx.scene.Group, javafx.scene.Scene, javafx.scene.effect.ColorAdjust, javafx.scene.image.Image, javafx.scene.image.ImageView.
Then we have created one class named EffectUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold an icon with a color adjust effect, a Group object is created which is then passed to the Scene class object.
The Home icon image is imported and the color adjust constructor is created and various properties are set using setters and using setEffect() method, the color adjust effect is applied on the Home icon.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX Color Adjust Effect example”. Also, it displays a home icon with specified color adjust the effect on it.
JavaFX Effect – Images with and without Color Adjust Effect
Example:
import javafx.application.Application;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class EffectUI extends Application {
@Override
public void start(Stage stage) throws Exception
{
Image newimage = new Image("https://icons.iconarchive.com/icons/custom-icon-design/pretty-office-4/256/home-icon.png");
ImageView newimageView = new ImageView(newimage);
Image newimage2 = new Image("https://icons.iconarchive.com/icons/custom-icon-design/pretty-office-4/256/home-icon.png");
ImageView newimageView2 = new ImageView(newimage);
newimageView.setX(140);
newimageView.setY(90);
newimageView2.setX(440);
newimageView2.setY(90);
newimageView.setFitHeight(200);
newimageView.setFitWidth(400);
newimageView2.setFitHeight(200);
newimageView2.setFitWidth(400);
newimageView.setPreserveRatio(true);
newimageView2.setPreserveRatio(true);
ColorAdjust newAdjust = new ColorAdjust();
newAdjust.setContrast(0.3);
newAdjust.setHue(-0.05);
newAdjust.setBrightness(0.9);
newAdjust.setSaturation(0.8);
newimageView.setEffect(newAdjust);
Group root = new Group(newimageView,newimageView2);
Scene scene = new Scene(root,700,450);
stage.setScene(scene);
stage.setTitle("JavaFx Color Adjust Effect example");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output:
In order to create the Image with and without Color Adjust Effect in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.paint.Color, javafx.scene.shape.Rectangle, javafx.scene.text.Font, javafx.scene.text.FontPosture, javafx.scene.text.FontWeight, javafx.scene.text.Text, javafx.stage.Stage, javafx.scene.Group, javafx.scene.Scene, javafx.scene.effect.ColorAdjust, javafx.scene.image.Image, javafx.scene.image.ImageView.
Then we have created one class named EffectUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold an icon with and without color adjust effect, a Group object is created which is then passed to the Scene class object.
The Home icon image is imported and the color adjust constructor is created and various properties are set using setters and using setEffect() method, the color adjust effect is applied on one Home icon and the other is kept as it is for identifying the difference.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX Color Adjust Effect example”. Also, it displays a one home icon with color adjust effect and the other without any color adjust effect.