Java Swing游戏板

时间:2012-03-04 13:54:44

标签: java swing paint

我正在研究一个大型的垄断游戏作为学校的项目,并遇到了一个非常大的性能问题。现在我有一个绘制方法,每次调用它时都会绘制整个板子......这是一个很大的问题,因为只需要在开始时只需要一个人购买房子或其他东西就可以绘制一次。我想要画的很多组件都是玩家,因为他们是最能移动并且需要画画的人。

这是我董事会的绘画方法:

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秒才能移动)。唯一的问题是我对于如何显示播放器片段从起始位置到结束的缓慢移动并不是一个好主意。只需要有人给我一个基本的想法,这样我就能朝着正确的方向前进。

2 个答案:

答案 0 :(得分:4)

将棋盘画到BufferedImage。在绘画方法中,绘制棋盘的图像,然后在顶部绘制棋子。

顺便说一句 - 使用Swing时,不要在顶级容器中绘画,而是使用JComponentJPanel。对于后两者,请覆盖paintComponent(Graphics)而不是paint(Graphics)

答案 1 :(得分:1)

您可以使用:jComponent.repaint(someRectangle)避免重绘整个曲面。另请参阅此example

但是,您必须自己想象每次移动之间需要更新的矩形(或矩形)。你不仅需要出现新事物的矩形,还需要消失的东西。

对于Swing中的动画,请查看此tutorial。您会注意到我在这篇文章中链接的两个示例都来自官方Swing tutorial