例子
以下程序是演示图像输入效果的示例。在这里,我们在位置 150, 100 创建一个图像输入,并将以下图像(jc2182 徽标)作为此效果的来源。
我们正在创建一个矩形并将此效果应用于它。将此代码保存在名称为的文件中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.cainiaoya.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 文件。
javac ImageInputEffectExample.java
java ImageInputEffectExample