JavaFX Effects - SepiaTone


Advertisements

On applying the Sepia Tone Effect to a node in JavaFX (image in general), it is toned with a reddish brown color.

The class named SepiaTone of the package javafx.scene.effect represents the sepia tone effect, this class contains two properties, which are −

  • level − This property is of double type representing the intensity of this effect. The range of this property is 0.0 to 1.0.

  • input − This property is of the type effect and it represents an input to the sepia tone effect.

Example

The following program is an example demonstrating the Sepia Tone Effect of JavaFX. In here, we are embedding the following image (tutorialspoint logo) in JavaFX scene using Image and ImageView classes. This is done at the position 100, 70 along with fit height and fit width 200 and 400 respectively.

SepiaTone

To this image, we are applying the Sepia Tone Effect with the level value 0.9. Save this code in a file with name SepiaToneEffectExample.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.SepiaTone; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.stage.Stage;  

public class SepiaToneEffectExample extends Application { 
   @Override 
   public void start(Stage stage) {       
      //Creating an image 
      Image image = new Image("http://www.tutorialspoint.com/images/tp-logo.gif"); 
       
      //Setting the image view 
      ImageView imageView = new ImageView(image); 
      
      //Setting the position of the image  
      imageView.setX(150); 
      imageView.setY(0);
      
      //setting the fit height and width of the image view 
      imageView.setFitHeight(300); 
      imageView.setFitWidth(400); 
      
      //Setting the preserve ratio of the image view 
      imageView.setPreserveRatio(true);    
       
      //Instanting the SepiaTone class 
      SepiaTone sepiaTone = new SepiaTone(); 
      
      //Setting the level of the effect 
      sepiaTone.setLevel(0.8); 
      
      //Applying SepiaTone effect to the image 
      imageView.setEffect(sepiaTone);      
         
      //Creating a Group object  
      Group root = new Group(imageView);   
               
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Sepia tone effect example"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show();         
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}      

Compile and execute the saved java file from the command prompt using the following commands.

javac SepiaToneEffectExample.java 
java SepiaToneEffectExample    

On executing, the above program generates a JavaFX window as shown below.

SepiaTone Effect
javafx_effects.htm
Advertisements