gpt4 book ai didi

Java 渲染 PNG 图像图 block 的时间过长

转载 作者:行者123 更新时间:2023-12-01 21:11:19 25 4
gpt4 key购买 nike

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

 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:

enter image description here

最佳答案

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

更新:

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

关于Java 渲染 PNG 图像图 block 的时间过长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41100868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com