好的,我正在制作一个游戏,用户在游戏中的角色后面移动一个图像(在我的代码中称为“地图”),这样看起来好像角色正在移动。Java:我如何更快地绘制这些图像?
程序运行时,性能下降适中,背景图像(“地图”)滞后,并且不总是重新绘制。
这里是我的主类调用来绘制图片类:
public graphics(int z,int s, int f)
{
x = z;
y = s;
d = f;
try {
map = ImageIO.read(new File("background.png"));
//br = ImageIO.read(new File("blackrectangle.jpg"));
s0 = ImageIO.read(new File("spriteshoot0.png"));
s10 = ImageIO.read(new File("spriteshoot10.png"));
s20 = ImageIO.read(new File("spriteshoot20.png"));
s30 = ImageIO.read(new File("spriteshoot30.png"));
s40 = ImageIO.read(new File("spriteshoot40.png"));
s50 = ImageIO.read(new File("spriteshoot50.png"));
s60 = ImageIO.read(new File("spriteshoot60.png"));
s70 = ImageIO.read(new File("spriteshoot70.png"));
s80 = ImageIO.read(new File("spriteshoot80.png"));
s90 = ImageIO.read(new File("spriteshoot90.png"));
s100 = ImageIO.read(new File("spriteshoot100.png"));
s110 = ImageIO.read(new File("spriteshoot110.png"));
s120 = ImageIO.read(new File("spriteshoot120.png"));
s130 = ImageIO.read(new File("spriteshoot130.png"));
} catch (IOException e) {}
}
public void moveZ(int a,int b)
{
xz+=a;
yz+=b;
}
public void move(int a,int b)
{
x+=a;
y+=b;
}
public void angle(int t)
{
q=t;
}
public void paintComponent(Graphics g) {
g.drawImage(map,x,y,this);
if (q==1)
g.drawImage(s0,599,340,null);
if (q==2)
g.drawImage(s10,599,340,null);
if (q==3)
g.drawImage(s20,599,340,null);
if (q==4)
g.drawImage(s30,599,340,null);
if (q==5)
g.drawImage(s40,599,340,null);
if (q==6)
g.drawImage(s50,599,340,null);
if (q==7)
g.drawImage(s60,599,340,null);
if (q==8)
g.drawImage(s70,599,340,null);
if (q==9)
g.drawImage(s80,599,340,null);
if (q==10)
g.drawImage(s90,599,340,null);
if (q==11)
g.drawImage(s100,599,340,null);
if (q==12)
g.drawImage(s110,599,340,null);
if (q==13)
g.drawImage(s120,599,340,null);
if (q==14)
g.drawImage(s130,599,340,null);
}}
所以我想知道怎样才能显示图像“地图”更快或找到解决这个性能问题的替代路线。
注* *此代码是作为我的主类中的一个对象创建的,并且每次执行时都不会从文件中读取BufferedImages(据我所知)。此外,repaint();方法在我的程序中每16毫秒调用一次(是的,我试图增加那个时间,问题仍然存在)。下面是用于重新绘制帧定时器的一个片段:)
TimerTask的任务=新的TimerTask({
public void run() {
if (onU)
g.move(0,3);
if (onL)
g.move(3,0);
if (onD)
g.move(0,-3);
if (onR)
g.move(-3,0);
//System.out.println(ze.getX(650,400,650,0,xZ,yZ));
//System.out.println(ze.getY(650,400,650,0,xZ,yZ));
if(onR||onL||onD||onU)
gameSurface.repaint();
}};
任何帮助感激,谢谢。
永远不要处理由paint或paintComponent方法中的JVM传递给您的Graphics对象。只处理你自己创建的一个。 –
你试过双缓冲吗? – 2011-11-05 02:55:50
不,我以为自动双缓冲 – user1030690