Javafx 简明教程

JavaFX - Image Input Effect

JavaFX 中的图像输入效果仅将图像嵌入到 JavaFX 屏幕中。就像在 Color Input 效果中一样,它用于将指定的彩色矩形区域作为输入传递给另一个效果。图像输入效果用于将指定的图像作为输入传递给另一个效果。

Image input effect in JavaFX just embeds an image to the JavaFX screen. Just like in the Color Input effect, it is used to pass the specified colored rectangular region as an input to another effect. An Image Input effect is used to pass the specified image as an input to another effect.

应用此效果时,指定的图像将不会被修改。此效果应用于任何节点。

On applying this effect, the image specified will not be modified. This effect is applied to any node.

javafx.scene.effect 中名为 ImageInput 的类表示图像输入效果,此类包含三个属性,它们是 −

The class named ImageInput of the package javafx.scene.effect represents the Image Input effect, this class contains three properties, which are −

  1. x − This property is of Double type; it represents the x coordinate of the position of the source image.

  2. y − This property is of Double type; it represents the y coordinate of the position of the source image.

  3. source − his property is of Image type; it represents image that is to be used as a source to this effect. (Passed as input)

Example

以下程序是一个演示图像输入效果的示例。在这里,我们在位置 150, 100 创建一个图像输入,并以以下图像(tutorialspoint 徽标)作为此效果的源。

The following program is an example demonstrating the Image input effect. In here, we are creating an image input at the position 150, 100, and taking the following image (tutorialspoint logo) as a source for this effect.

image input effect

我们创建一个矩形,并对它应用这个效果。使用名称 ImageInputEffectExample.java 将此代码保存在一个文件中。

We are creating a rectangle and applying this effect to it. Save this code in a file with the name ImageInputEffectExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ImageInput;
import javafx.scene.image.Image;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ImageInputEffectExample extends Application {
   @Override
   public void start(Stage stage) {
      //Creating an image
      Image image = new Image("http://www.tutorialspoint.com/green/images/logo.png");

      //Instantiating the Rectangle class
      Rectangle rectangle = new Rectangle();

      //Instantiating the ImageInput class
      ImageInput imageInput = new ImageInput();

      //Setting the position of the image
      imageInput.setX(150);
      imageInput.setY(100);

      //Setting source for image input
      imageInput.setSource(image);

      //Applying image input effect to the rectangle node
      rectangle.setEffect(imageInput);

      //Creating a Group object
      Group root = new Group(rectangle);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

      //Setting title to the Stage
      stage.setTitle("Sample Application");

      //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 ImageInputEffectExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ImageInputEffectExample

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

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

image input effect example