我在循環創造了多種視覺對象以避免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異常。無論如何避免/優化?