Javafx 简明教程

JavaFX - SepiaTone Effect

褐色调效果通常将图像从黑白颜色更改为红褐色。您可以在 JavaFX(通常是图像)中将褐色调效果应用到节点。

Sepia tone effect in general changes the image from the black and white color to a reddish brown color. You can apply the Sepia Tone Effect to a node in JavaFX (image in general).

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

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

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

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

Example

以下程序是一个演示 JavaFX 怀旧效果的示例。在此示例中,我们使用 ImageImageView 类将以下图像(tutorialspoint 徽标)嵌入 JavaFX 场景中。我们将其置于位置 100, 70,并设置其高度和宽度分别为 200 和 400。

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

对此图像,我们使用级别值为 0.9 应用怀旧效果。将此代码保存在名为 SepiaToneEffectExample.java 的文件中。

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);
   }
}

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

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

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

Output

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

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

sepiatone effect