当我从日食开始运行我的程序时, 它运行正常, CPU的负载很少。 JProfiler (观察日食运行的程序) 表示大部分被碰撞探测和绘制。 JPropiler 正在观看编译的罐子运行时, 97% 的 cpu 使用来自绘图。 它的运行速度是日食的两倍 - 三倍 。
为什么?
此加载一次以从图示工作表上获取图像( 所有图像都是缓冲图像)
SpriteMan(Map xMap, Board xBoard)
{
mMap = xMap;
mBoard = xBoard;
try
{
bigImg = ImageIO.read(new File("sprites.PNG"));
background = ImageIO.read(new File("background.PNG"));
}
catch (IOException e)
{
System.err.println("Caught IOException: " + e.getMessage());
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sprites[i][j] = bigImg.getSubimage(i * width,j * height,width,height);
iSprites[i][j] = config.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
//iSprites[i][j] = toImage(sprites[i][j]);
for(int x = 0; x<32; x++)
{
for(int y = 0; y<32; y++)
{
iSprites[i][j].setRGB(x, y, sprites[i][j].getRGB(x,y));
}
}
loadedImage+=1;
}
}
}
After this, the sprites are saved to their corresponding objects (also only called once)
BlockGround(Map xMap, int X, int Y)
{
super(xMap, X, Y);
mSprite = mMap.mBoard.mSpriteMan.sprites[0][0];
mCollidable = true;
mChar = G ;
}
这些区块是用下列方式绘制的:
g2.drawImage(mSprite,null, x, y);
EDIT----- Thank you so much! The program runs normally now (After updating my JRE) although I seem to have messed something up and it took a lot of fiddling to get eclipse running again, but hey, it works!