JavaFX - 棕褐色效果



通常情况下,棕褐色效果会将图像从黑白颜色更改为红棕色。您可以将棕褐色效果应用于 JavaFX 中的节点(通常是图像)。

名为 SepiaTone 的类属于 javafx.scene.effect 包,它表示棕褐色效果,此类包含两个属性,它们是:

  • level − 此属性为双精度类型,表示此效果的强度。此属性的范围为 0.0 到 1.0。

  • input − 此属性为 effect 类型,它表示棕褐色效果的输入。

示例

以下程序是一个演示 JavaFX 棕褐色效果的示例。在这里,我们使用 ImageImageView 类将以下图像(tutorialspoint 徽标)嵌入 JavaFX 场景中。这是在位置 100, 70 完成的,配合高度和宽度分别为 200 和 400 的自适应大小。

SepiaTone

对于此图像,我们应用级别值为 0.9 的棕褐色效果。将此代码保存到名为 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("https://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); 
   } 
}      

使用以下命令从命令提示符编译并执行保存的 java 文件。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls SepiaToneEffectExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls SepiaToneEffectExample    

输出

执行上述程序后,将生成如下所示的 JavaFX 窗口。

SepiaTone Effect
广告