注意:这不是你确切问题的答案。
我不知道为什么要为每个图像创建ImageView。相反,我会为每个nodeChunk绘制一个图像(160000个图像),并为这个nodeChunk创建一个ImageView。
在功能上,我看不出这两种方法之间有什么区别(您可以告诉我为什么要使用每个imageview)。但从性能上看,这种方法是非常快速和顺利的。
请找到下面的演示我所说的。
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
/**
* Combining 4,000,000 images
*/
public class ImageLoadingDemo extends Application {
SecureRandom rnd = new SecureRandom();
// I created 5 images of dimension 1 x 1 each with new color
String[] images = {"1.png", "2.png", "3.png", "4.png", "5.png"};
Map<Integer, BufferedImage> buffImages = new HashMap<>();
Map<Integer, Image> fxImages = new HashMap<>();
@Override
public void start(Stage primaryStage) throws IOException {
ScrollPane root = new ScrollPane();
root.setPannable(true);
Scene scene = new Scene(root, 800, 800);
primaryStage.setScene(scene);
primaryStage.show();
root.setContent(approach1()); // Fast & smooth rendering
// Your approach of creating ImageViews
// root.setContent(approach2()); // Very very very very very slow rendering
}
private Node approach1(){
// Creating 5 x 5 nodeChunk grid
GridPane grid = new GridPane();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
ImageView nodeChunk = new ImageView(SwingFXUtils.toFXImage(createChunk(), null));
grid.add(nodeChunk, i, j);
}
}
return grid;
}
private BufferedImage createChunk() {
// Combining 160000 1px images into a single image of 400 x 400
BufferedImage chunck = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);
Graphics2D paint;
paint = chunck.createGraphics();
paint.setPaint(Color.WHITE);
paint.fillRect(0, 0, chunck.getWidth(), chunck.getHeight());
paint.setBackground(Color.WHITE);
for (int i = 0; i < 400; i++) {
for (int j = 0; j < 400; j++) {
int index = rnd.nextInt(5); // Picking a random image
BufferedImage buffImage = buffImages.get(index);
if (buffImage == null) {
Image image = new Image(ImageLoadingDemo.class.getResourceAsStream(images[index]));
buffImages.put(index, SwingFXUtils.fromFXImage(image, null));
}
paint.drawImage(buffImage, i, j, null);
}
}
return chunck;
}
private Node approach2(){
GridPane grid = new GridPane();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
grid.add(createGroup(), i, j);
}
}
return grid;
}
private Group createGroup() {
GridPane grid = new GridPane();
for (int i = 0; i < 400; i++) {
for (int j = 0; j < 400; j++) {
int index = rnd.nextInt(5);
Image fxImage = fxImages.get(index);
if (fxImage == null) {
fxImage = new Image(ImageLoadingDemo.class.getResourceAsStream(images[index]));
fxImages.put(index, fxImage);
}
grid.add(new ImageView(fxImage), i, j);
}
}
return new Group(grid);
}
public static void main(String[] args) {
Application.launch(args);
}
}