使用这些方法,可以在单个图像视图中设置多个图像。
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javafx.application.Application; import javafx.geometry.Rectangle2D; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ViewingMultipleImages extends Application { public void start(Stage stage) throws IOException { //创建图像对象 InputStream stream = new FileInputStream("D:\\images\\elephant.jpg"); Image image = new Image(stream); //创建图像视图 ImageView imageView1 = new ImageView(image); //设置图像视图参数 imageView1.setX(10); imageView1.setY(10); imageView1.setFitWidth(200); imageView1.setPreserveRatio(true); ImageView imageView2 = new ImageView(image); //设置图像视图参数 imageView2.setX(230); imageView2.setY(10); imageView2.setFitWidth(350); imageView2.setPreserveRatio(true); //设置视口 Rectangle2D rect2 = new Rectangle2D(45, 30, 250, 250); imageView2.setViewport(rect2); Image image2 = new Image(new FileInputStream("D:\\images\\boy.jpg")); //创建图像视图 ImageView imageView3 = new ImageView(image2); //设置图像视图参数 imageView3.setX(10); imageView3.setY(200); imageView3.setFitWidth(200); imageView3.setPreserveRatio(true); //设置场景对象 Group root = new Group(imageView1, imageView2, imageView3); Scene scene = new Scene(root, 595, 370, Color.BEIGE); stage.setTitle("Multiple Images"); stage.setScene(scene); stage.show(); } public static void main(String args[]) { launch(args); } }
输出结果