Open In App

JavaFX | SepiaTone Class

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. SepiaTone(): Creates a new object of SepiaTone class.
  2. 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:

  1. 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 it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.image.*;
    import javafx.scene.effect.*;
    import java.io.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
      
    public class sepia_tone_1 extends Application {
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("SepiaTone Example");
      
            // create a input stream
            FileInputStream input = new FileInputStream("D:\\GFG.png");
      
            // create a image
            Image image = new Image(input);
      
            // create a image View
            ImageView imageview = new ImageView(image);
      
            // create a sepia_tone effect
            SepiaTone sepia_tone = new SepiaTone(0.5);
      
            // set effect
            imageview.setEffect(sepia_tone);
      
            // create a VBox
            VBox vbox = new VBox(imageview);
      
            // create a scene
            Scene scene = new Scene(vbox, 200, 200);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Input Image:

    Output:

  2. 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 button
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.image.*;
    import javafx.scene.effect.*;
    import java.io.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
      
    public class sepia_tone_2 extends Application {
      
        double level = 0.1;
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("SepiaTone Example");
      
            // create a input stream
            FileInputStream input = new FileInputStream("D:\\GFG.png");
      
            // create a image
            Image image = new Image(input);
      
            // create a image View
            ImageView imageview = new ImageView(image);
      
            // create a sepia_tone effect
            SepiaTone sepia_tone = new SepiaTone(level);
      
            // create a button
            Button button = new Button("increase");
      
            // action event
            EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
                public void handle(ActionEvent e)
                {
      
                    // increase the level
                    level += 0.1;
                    if (level > 1)
                        level = 0.0;
      
                    // set Level for sepia_tone
                    sepia_tone.setLevel(level);
                }
            };
      
            // set on action of button
            button.setOnAction(event);
      
            // set effect
            imageview.setEffect(sepia_tone);
      
            // create a VBox
            VBox vbox = new VBox(imageview, button);
      
            // create a scene
            Scene scene = new Scene(vbox, 300, 300);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Input Image:

    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



Last Updated : 22 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads