我正在用打砖块之类的Java语言编写一个简单的游戏,但是GUI管理器存在问题。我有一个面板,可以绘制所有图像。图像可分为两类:像球这样的图像,它需要非常快的重新粉刷;而像块一样的图像,只有在发生碰撞时才需要重新粉刷。游戏开始时,它变得非常缓慢,因为this.repaint()
命令重新绘制了所有图像。我在网站上的问题之间进行搜索,如何使重新绘制的速度更慢,但我没有找到答案。
这是代码
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
this.paintVaus(g2d);
this.paintBall(g2d);
this.paintBlock(g2d);
}
private void paintBall(Graphics2D g2d){
g2d.drawImage(Ball.getInstance().getImage(), Ball.getInstance().getX(), Ball.getInstance().getY(), this);
if(DoubleBall.getInstance().getVisible()){
g2d.drawImage(Ball.getInstance().getImage(), DoubleBall.getInstance().getX(), Ball.getInstance().getY(), this);
}
}
private void paintBlock(Graphics2D g2d) {
try {
for(int i = 0; i < Model.getInstance().getnumberOfColumns(); i++){
for(int j = 0; j < Model.getInstance().getnumberOfRows(); j++){
if(Model.getInstance().getBlockArray(i, j) >= 1){
g2d.drawImage(Block.getInstance().getImage(i, j), widthBlock * i, heightBlock * j, this);
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void paintVaus(Graphics2D g2d){
g2d.drawImage(Vaus.getInstance().getImage(),
Vaus.getInstance().getX(), Vaus.getInstance().getY(), this);
}
@Override
public void actionPerformed(ActionEvent e) {
this.repaint();
}