首先,我是一名初学者,希望将动画添加到我制作的简单游戏中。我制作了一个单独的类来测试我的动画,显示的每个图像(除了两个以外)均如此。
我当前的代码是:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import javax.swing.JApplet;
public class GooAnimation extends JApplet implements Runnable {
Image[] eastbarf = new Image[22];
Image[] east2barf = new Image[10];
boolean enter = false;
int counter = 0;
Image img1;
URL url;
public void init() {
eastbarf[0] = getImage(getDocumentBase(), "eastbarf1.png");
eastbarf[1] = getImage(getDocumentBase(), "eastbarf2.png");
eastbarf[2] = getImage(getDocumentBase(), "eastbarf3.png");
eastbarf[3] = getImage(getDocumentBase(), "eastbarf4.png");
eastbarf[4] = getImage(getDocumentBase(), "eastbarf5.png");
eastbarf[5] = getImage(getDocumentBase(), "eastbarf6.png");
eastbarf[6] = getImage(getDocumentBase(), "eastbarf7.png");
eastbarf[7] = getImage(getDocumentBase(), "eastbarf8.png");
eastbarf[8] = getImage(getDocumentBase(), "eastbarf9.png");
eastbarf[9] = getImage(getDocumentBase(), "eastbarf10.png");
eastbarf[10] = getImage(getDocumentBase(), "eastbarf11.png");
eastbarf[11] = getImage(getDocumentBase(), "eastbarf12.png");
eastbarf[12] = getImage(getDocumentBase(), "eastbarf13.png");
eastbarf[13] = getImage(getDocumentBase(), "eastbarf14.png");
eastbarf[14] = getImage(getDocumentBase(), "eastbarf15.png");
eastbarf[15] = getImage(getDocumentBase(), "eastbarf16.png");
eastbarf[16] = getImage(getDocumentBase(), "eastbarf17.png");
eastbarf[17] = getImage(getDocumentBase(), "eastbarf18.png");
eastbarf[18] = getImage(getDocumentBase(), "eastbarf19.png");
eastbarf[19] = getImage(getDocumentBase(), "eastbarf20.png");
eastbarf[20] = getImage(getDocumentBase(), "eastbarf21.png");
eastbarf[21] = getImage(getDocumentBase(), "eastbarf22.png");
east2barf[0] = getImage(getDocumentBase(), "east2barf1.png");
east2barf[1] = getImage(getDocumentBase(), "east2barf2.png");
east2barf[2] = getImage(getDocumentBase(), "east2barf3.png");
east2barf[3] = getImage(getDocumentBase(), "east2barf4.png");
east2barf[4] = getImage(getDocumentBase(), "east2barf5.png");
east2barf[5] = getImage(getDocumentBase(), "east2barf6.png");
east2barf[6] = getImage(getDocumentBase(), "east2barf7.png");
east2barf[7] = getImage(getDocumentBase(), "east2barf8.png");
east2barf[8] = getImage(getDocumentBase(), "east2barf9.png");
east2barf[9] = getImage(getDocumentBase(), "east2barf10.png");
}
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
@Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}
@Override
public void paint(Graphics g) {
// for (int i = 0; i < eastbarf.length; i++) {
if (enter) {
if (counter < 10) {
g.setColor(Color.WHITE);
g.drawRect(0, 0, 10000, 10000);
g.fillRect(0,0,10000,10000);
g.drawImage(east2barf[counter], 300, 0, 1000, 1000, this);
System.out.println(eastbarf[counter].getHeight(null));
}
if (counter == 10) {
counter = 0;
enter = false;
}
}
if (!enter) {
if (counter < 22) {
// if ((i - 1)%2 == 1) {
//
// }
System.out.println(counter);
if (eastbarf[counter] == null) {
System.out.println("Its null!");
}
// if (counter % 2 == 1) {
// g.setColor(Color.YELLOW);
// }
// else {
// g.setColor(Color.MAGENTA);
// }
g.setColor(Color.WHITE);
g.drawRect(0, 0, 10000, 10000);
g.fillRect(0,0,10000,10000);
g.drawImage(eastbarf[counter], 0, 0, 1000, 1000, this);
System.out.println(eastbarf[counter].getHeight(null));
// }
}
// }
if (counter > 21) {
enter = true;
counter = 0;
}
}
counter += 1;
// g.drawImage(img1, 0, 0, null);
}
}
我收到两个错误消息:
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 2" java.lang.OutOfMemoryError: Java heap space
但是,我查找了如何增加Eclipse堆空间并将其增加一倍。我不确定这是否足够。理想情况下,我将拥有4个相同长度的barf列表和4个2barf列表。如您所见,为了便于概念化,我将所有图片名称都分开了。我很新,并且不了解图像明智的最佳实践或JApplet / Swing的东西。如果有人可以帮我弄清楚为什么我在两张照片上都输了,我将不胜感激。缺少的图像是eastbarf2中的第六张和第八张图像。
我非常感谢您的帮助。
谢谢
编辑:
每个图像大约80-150 kb。在Eclipse中,这些是我放入运行配置的参数部分中的命令。 -Xms1024M -Xmx2048M。他们似乎没有改变任何东西。
最佳答案
g.drawRect(0, 0, 10000, 10000);
g.fillRect(0,0,10000,10000);
g.drawImage(east2barf[counter], 300, 0, 1000, 1000, this);
您将在屏幕上填充10k x 10k像素,然后绘制大小为1000 x 1000的图像,这肯定会消耗您的内存。如果您想为自己的游戏制作一张大地图,则必须使用正交摄像头实现一个贴图地图,该摄像头将仅渲染玩家所在的屏幕,例如将所有Image更改为BufferedImage 并尝试以下操作:
[...]
g.drawRect(0, 0, 800, 600);
g.fillRect(0,0,800,600);
g.drawImage(east2barf[counter], 300, 0, east2barf[counter].getWidth(), east2barf[counter].getHeight(), this);
[...]
g.setColor(Color.WHITE);
g.drawRect(0, 0, 800, 600);
g.fillRect(0,0,800,600);
g.drawImage(eastbarf[counter], 0, 0, east2barf[counter].getWidth(), east2barf[counter].getHeight(), this);
[...]