我正在学校做一个大型大富翁游戏项目,但遇到了一个相当大的性能问题。现在我有一个绘制方法,每次调用时都会绘制整个板......这是一个大问题,因为板只需要在开始时绘制一次,并且只有在有人买房子或其他东西时才需要绘制一次。我唯一想要经常绘制的组件是玩家,因为他们是移动最多且需要绘制的组件。
这是我的板子的绘制方法:
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//Draw all the spots
for(int i = 0; i < spots.length; i++){
g2d.setColor(Color.white);
g2d.fill(spots[i].getRect());
if(spots[i] instanceof Property){
if(i < 10){
g2d.setColor(spots[i].getColor());
Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y,spots[i].getRect().width,spots[i].getRect().height/3);
g2d.fill(temp);
g2d.setColor(Color.black);
g2d.drawString(((Property)spots[i]).getName(), spots[i].getRect().x, spots[i].getRect().height);
}else if(i >= 10 && i < 20){
g2d.setColor(spots[i].getColor());
Rectangle temp = new Rectangle(spots[i].getRect().x+spots[i].getRect().width-spots[i].getRect().width/3,spots[i].getRect().y,spots[i].getRect().width/3,spots[i].getRect().height);
g2d.fill(temp);
}else if(i >= 20 && i < 30){
g2d.setColor(spots[i].getColor());
Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y+spots[i].getRect().height-spots[i].getRect().height/3,spots[i].getRect().width,spots[i].getRect().height/3);
g2d.fill(temp);
g2d.setColor(Color.black);
g2d.drawString(((Property)spots[i]).getName(), spots[i].getRect().x, spots[i].getRect().y);
}else if(i >= 30 && i < 40){
g2d.setColor(spots[i].getColor());
Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y,spots[i].getRect().width/3,spots[i].getRect().height);
g2d.fill(temp);
}
}else if(spots[i] instanceof Railroad){
if(i == 5)
g2d.drawImage(imgTrain3, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/4, null);
else if(i == 15)
g2d.drawImage(imgTrain4, spots[i].getRect().x+spots[i].getRect().width/4, spots[i].getRect().y, null);
else if(i == 25)
g2d.drawImage(imgTrain1, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/4, null);
else if(i == 35)
g2d.drawImage(imgTrain2, spots[i].getRect().x+spots[i].getRect().width/4, spots[i].getRect().y, null);
}else if(spots[i] instanceof Chance){
if(i == 7)
g2d.drawImage(imgChance2, spots[i].getRect().x, spots[i].getRect().y, null);
else if(i == 22)
g2d.drawImage(imgChance2, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/3, null);
else if(i == 36)
g2d.drawImage(imgChance3, spots[i].getRect().x, spots[i].getRect().y, null);
}else if(spots[i] instanceof Community){
if(i == 2)
g2d.drawImage(imgComm1, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/3, null);
else if(i == 17)
g2d.drawImage(imgComm2, spots[i].getRect().x, spots[i].getRect().y, null);
else if(i == 33)
g2d.drawImage(imgComm3, spots[i].getRect().x+spots[i].getRect().width/3, spots[i].getRect().y, null);
}else{
g2d.setColor(spots[i].getColor());
g2d.fill(spots[i].getRect());
}
}
//Draw the outline of every spot
g2d.setColor(Color.black);
for(Spot index : spots)
g2d.draw(index.getRect());
//Draw the outline of the whole board
g2d.drawRect(newX, newY, boardSize, boardSize);
//Draw the Players location
for(Player index : players)
g2d.drawImage(index.getImage(), index.getLoc().x, index.getLoc().y, null);
}
基本上有大量的文本来代表棋盘,并且每次棋盘重新绘制时都会执行此操作。有什么建议吗?
额外问题:我也刚刚开始为玩家滚动后的移动动画(目前只是跳转到目的地)。我创建了一个计时器,每掷一次需要 1 秒(例如:如果掷出 5,则需要 5 秒才能移动)。唯一的问题是,我真的不知道如何显示玩家棋子从起始位置到结束位置的缓慢移动。只需要有人给我一个基本的想法,这样我就可以朝着正确的方向前进。
最佳答案
将板绘制到BufferedImage
。在paint方法中,绘制棋盘的图像,然后在顶部绘制棋子。
顺便说一句 - 使用 Swing 时,不要在顶级容器中绘制,而是使用 JComponent
或 JPanel
。对于后两者,重写 paintComponent(Graphics)
而不是 paint(Graphics)
。
关于Java Swing 游戏板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9555436/