Java 从 PNG 图像渲染图块花费的时间太长

Java taking too long to render tiles from a PNG image

我正在尝试从 PNG 文件在 (600 / 448) 屏幕中渲染关卡 (1600 / 448)。它尝试渲染但没有成功,我认为这样做会停止游戏循环。我认为这种情况正在发生,因为有很多瓷砖,但我真的不知道如何解决它。

 public void loadLevel(BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();

    for (int xx = 0; xx < width; xx++) { //width = 1600
        for (int yy = 0; yy < height; yy++) { //height = 448;
            Color c = new Color(image.getRGB(xx,yy));
            int red = c.getRed();
            int green = c.getGreen();
            int blue = c.getBlue();

            if (red == 225 && blue == 225 & green == 225) {
                objectHandler.add(new GrassRight(xx * 32, yy * 32, ObjectId.GrassRight));
            } else if (red == 129 && blue == 250 & green == 212) {
                objectHandler.add(new Player(xx * 32, yy * 32, objectHandler, ObjectId.Player));
           } else if (red == 0 && blue == 0 && green == 0) {
               objectHandler.add(new Dirt(xx * 32, yy * 32, ObjectId.Dirt));

            } else if (red == 144 && blue == 144 & green == 213) {
                objectHandler.add(new Grass(xx * 32, yy * 32, ObjectId.Grass));

            } else if (red == 38 && blue == 56 & green == 50) {
                objectHandler.add(new GrassLeft(xx * 32, yy * 32, ObjectId.GrassLeft));

            } else if (red == 62 && blue == 35 & green == 39) {
                objectHandler.add(new SlideRight(xx * 32, yy * 32, ObjectId.SlideRight));

            } else if (red == 191 && blue == 12 & green == 54) {
                objectHandler.add(new SlideLeft(xx * 32, yy * 32, ObjectId.SlideLeft));

            } else if (red == 127 && blue == 194 & green == 87) {
                objectHandler.add(new DownSlideLeft(xx * 32, yy * 32, ObjectId.DownSlideLeft));

            } else if (red == 49 && blue == 146 & green == 27) {
                objectHandler.add(new DownSlideRight(xx * 32, yy * 32, ObjectId.DownSlideRight));
            }
        }
    }
}


public void init() {
    System.out.println("Loading...");
    LoadingHandler loadingHandler = new LoadingHandler();
    level = loadingHandler.loadImage("/Level1.png");
    objectHandler = new ObjectHandler();
    cameraHandler = new CameraHandler(0, 0);
    this.addKeyListener(new KeyHandler(objectHandler));
    loadLevel(level);
    System.out.println("Done!");
}

public void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        this.createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    Graphics2D g2d = (Graphics2D) g;
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());
    g2d.translate(cameraHandler.getX(), cameraHandler.getY());
    System.out.println("Rendering!");
    objectHandler.render(g);
    System.out.println("Rendered!");
    g2d.translate(-cameraHandler.getX(), -cameraHandler.getY());
    g.dispose();
    bs.show();

}

public class ObjectHandler {
public LinkedList<ObjectManager> object = new LinkedList<>();

ObjectManager tempObject;

public void tick() {
    for (int i = 0; i < object.size(); i++) {
        tempObject = object.get(i);
        tempObject.tick(object);
    }
}

public void render(Graphics g) {
    for (int i = 0; i < object.size(); i++) {
        tempObject = object.get(i);
        tempObject.render(g);
    }
}

public void add(ObjectManager object) {
    this.object.add(object);

}

它给出了这个输出:

Loading...
Done!
Rendering!

PNG:

不是为每个像素创建一个对象(即 716'800 个对象),而是为每个矩形区域创建一个对象(15 个对象)。

更新:

在 class ObjectManager 中,添加两个 int 字段:width 和 height,然后渲染整个矩形而不是单个像素(或 32x32 正方形)。您仍然可以使用 PNG 来初始化对象,但使用更智能的算法,不会为每个像素重新创建一个新对象。