每当我加载背景图像时,无论图像的大小有多小,我都会遇到此问题。我正在添加一张尺寸为3.81kb的1010x600 png图片。我正在使用来自swing的计时器来重绘它。每当我添加并绘制bg.paintIcon方法时,游戏就会变慢。
public void paintComponent(Graphics g){
super.paintComponent(g);
bg.paintIcon(null, g, 0, 0);
p.img.paintIcon(null, g, p.x, p.y);
g2p = (Graphics2D)g;
g2p.setColor(Color.green);
g2p.fillRect(20, 10, (int)Math.ceil((p.health/1000.0)*960), 20);
life.paintIcon(null, g, 0, 8);
g2p.setColor(Color.black);
g2p.drawRect(19, 34, 960+1, 20+1);
g2p.setColor(Color.blue);
g2p.fillRect(20, 35, (int)Math.ceil((p.mana/500.0)*960), 20);
//System.out.println(df.format(p.health/1000.0));
jl.setLocation(p.x, p.y);
//jl.setText("Current Health: "+p.health);
for (int i = 0 ; i < 20 ; i++){//IMPORTANT
g2[i] = (Graphics2D) g;
e[i].img.paintIcon(null, g, e[i].getx(), e[i].gety());
if (e[i].dead){
gen[i] = new Thread(new Generation(i));
if (letalive){
letalive = false;
gen[i].start();
}
}
}
}
答案 0 :(得分:4)
您的paintComponent代码应该绘制。期。没别了。
以下是paintComponent方法的外观。
public void paintComponent(Graphics g){
super.paintComponent(g);
bg.paintIcon(null, g, 0, 0);
p.img.paintIcon(null, g, p.x, p.y);
g2p = (Graphics2D)g;
g2p.setColor(Color.green);
g2p.fillRect(20, 10, (int)Math.ceil((p.health/1000.0)*960), 20);
life.paintIcon(null, g, 0, 8);
g2p.setColor(Color.black);
g2p.drawRect(19, 34, 960+1, 20+1);
g2p.setColor(Color.blue);
g2p.fillRect(20, 35, (int)Math.ceil((p.mana/500.0)*960), 20);
//System.out.println(df.format(p.health/1000.0));
jl.setLocation(p.x, p.y);
//jl.setText("Current Health: "+p.health);
for (int i = 0 ; i < 20 ; i++){//IMPORTANT
e[i].img.paintIcon(null, g, e[i].getx(), e[i].gety());
}
}
此代码属于您的控制器。
for (int i = 0 ; i < 20 ; i++) {
if (e[i].dead){
gen[i] = new Thread(new Generation(i));
if (letalive){
letalive = false;
gen[i].start();
}
}
}