我在循环创造了多种视觉对象以避免OutOfMemoryError异常:Java3D的:如何同时使用纹理
...
Package packet; // a visual packet
for(int i = 0; i < numberOfSteps; i++){
packet = new Package(packetSize, packetSize, packetSize);
// position, rotate, etc. and add to scene graph
}
...
的包装基本上只是一个简单的立方体纹理。封装的建筑工如下所示:
public Package(float x, float y, float z) {
Appearance appear = new Appearance();
BufferedImage filename = null;
try {
filename = ImageIO.read(getClass().getResource("package.jpg"));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
TextureLoader loader = new TextureLoader(filename);
ImageComponent2D image = loader.getImage();
Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
image.getWidth(), image.getHeight());
texture.setImage(0, image);
appear.setTexture(texture);
textureCube = new Box(x, y, z, Box.GENERATE_TEXTURE_COORDS, appear);
}
所以我一遍又一遍的加载相同的纹理在循环,最终导致OutOfMemoryError异常。无论如何避免/优化?