JavaFX | SepiaTone Class
SepiaTone class is a part of JavaFX. SepiaTone class is used to add a sepia tone effect to the image which is similar to the antique photographs. On applying SepiaTone effect a reddish brown tone appears on the image.
Constructors of the class:
- SepiaTone(): Creates a new object of SepiaTone class.
- SepiaTone(double l): Creates a new SepiaTone object with specified level.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getInput() | Returns the value of property input. |
| setInput(Effect v) | Sets the value of property input. |
| setLevel(double v) | Sets the value of level of Sepia Tone object. |
| getLevel() | Returns the value of level of Sepia Tone object. |
Below programs illustrate the use of SepiaTone class:
- Java program to import an image and add SepiaTone effect to it: In this program a FileInputStream is created and an image is taken as input from a file. Image named image is created using the input from file input stream. From the image a image view object is created and it is added to the VBox. The VBox is then added to the scene and the scene is added to the stage. A SepiaTone effect is created with a specified level passed as parameters and the effect is set to the image view using setEffect() function.
// Java program to import an image// and add SepiaTone effect to itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.image.*;importjavafx.scene.effect.*;importjava.io.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;publicclasssepia_tone_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage)throwsException{// set title for the stagestage.setTitle("SepiaTone Example");// create a input streamFileInputStream input =newFileInputStream("D:\\GFG.png");// create a imageImage image =newImage(input);// create a image ViewImageView imageview =newImageView(image);// create a sepia_tone effectSepiaTone sepia_tone =newSepiaTone(0.5);// set effectimageview.setEffect(sepia_tone);// create a VBoxVBox vbox =newVBox(imageview);// create a sceneScene scene =newScene(vbox,200,200);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}} - Java program to import an image and set SepiaTone effect to it. The Level of the SepiaTone effect can be controlled using the button: In this program a FileInputStream is created and an image is taken as input from a file. Image named image is created using the input from the file input stream. From the image, an image view object is created and it is added to the VBox. The VBox is then added to the scene and the scene is added to the stage. A SepiaTone effect is created with a specified level passed as parameters and the effect is set to the image view using setEffect() function. A Button named button is created which is used to increase the SepiaTone of the image. The button is also added to the VBox. The SepiaTone of the image is increased using the setLevel() function.The events related to button is handled using EventHandler.
// Java program to import an image and set// SepiaTone effect to it. The Level of the// SepiaTone effect can be controlled// using the buttonimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.image.*;importjavafx.scene.effect.*;importjava.io.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;publicclasssepia_tone_2extendsApplication {doublelevel =0.1;// launch the applicationpublicvoidstart(Stage stage)throwsException{// set title for the stagestage.setTitle("SepiaTone Example");// create a input streamFileInputStream input =newFileInputStream("D:\\GFG.png");// create a imageImage image =newImage(input);// create a image ViewImageView imageview =newImageView(image);// create a sepia_tone effectSepiaTone sepia_tone =newSepiaTone(level);// create a buttonButton button =newButton("increase");// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// increase the levellevel +=0.1;if(level >1)level =0.0;// set Level for sepia_tonesepia_tone.setLevel(level);}};// set on action of buttonbutton.setOnAction(event);// set effectimageview.setEffect(sepia_tone);// create a VBoxVBox vbox =newVBox(imageview, button);// create a sceneScene scene =newScene(vbox,300,300);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/SepiaTone.html
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.




