Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing游戏板_Java_Swing_Paint - Fatal编程技术网

Java Swing游戏板

Java Swing游戏板,java,swing,paint,Java,Swing,Paint,我在学校做一个大型垄断游戏项目,遇到了一个相当大的性能问题。现在我有一个画法,每次调用它时都会画出整个电路板。。。这是一个大问题,因为董事会只需要在行乞时画一次,而且只有在有人买房子或其他东西时才需要画。我最想画的是球员,因为他们是最需要画的球员 以下是我的画板的绘画方法: public void paint(Graphics g){ super.paint(g); Graphics2D g2d = (Graphics2D)g; g2d.setRe

我在学校做一个大型垄断游戏项目,遇到了一个相当大的性能问题。现在我有一个画法,每次调用它时都会画出整个电路板。。。这是一个大问题,因为董事会只需要在行乞时画一次,而且只有在有人买房子或其他东西时才需要画。我最想画的是球员,因为他们是最需要画的球员

以下是我的画板的绘画方法:

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);
    }
public void绘制(图形g){
超级油漆(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(renderingHits.KEY\u抗锯齿,renderingHits.VALUE\u抗锯齿\u开启);
//画出所有的斑点
对于(int i=0;i=10&&i<20){
setColor(spots[i].getColor());
矩形温度=新矩形(点[i].getRect().x+点[i].getRect().width点[i].getRect().width/3,点[i].getRect().y,点[i].getRect().width/3,点[i].getRect().height);
g2d.填充(温度);
}否则,如果(i>=20&&i<30){
setColor(spots[i].getColor());
矩形温度=新矩形(点[i].getRect().x,点[i].getRect().y+spots[i].getRect().height spots[i].getRect().height spots[i].getRect().width,spots[i].getRect().height/3);
g2d.填充(温度);
g2d.setColor(Color.black);
g2d.drawString(((属性)spots[i]).getName(),spots[i].getRect().x,spots[i].getRect().y);
}否则,如果(i>=30&&i<40){
setColor(spots[i].getColor());
矩形温度=新矩形(点[i].getRect().x,点[i].getRect().y,点[i].getRect().width/3,点[i].getRect().height);
g2d.填充(温度);
}
}else if(点[i]铁路实例){
如果(i==5)
g2d.drawImage(imgTrain3,spots[i].getRect().x,spots[i].getRect().y+spots[i].getRect().height/4,null);
否则如果(i==15)
g2d.drawImage(imgTrain4,spots[i].getRect().x+spots[i].getRect().width/4,spots[i].getRect().y,null);
否则如果(i==25)
g2d.drawImage(imgTrain1,spots[i].getRect().x,spots[i].getRect().y+spots[i].getRect().height/4,null);
否则如果(i==35)
g2d.drawImage(imgTrain2,spots[i].getRect().x+spots[i].getRect().width/4,spots[i].getRect().y,null);
}else if(点[i]偶然事件){
如果(i==7)
g2d.drawImage(imgChance2,spots[i].getRect().x,spots[i].getRect().y,null);
否则如果(i==22)
g2d.drawImage(imgChance2,spots[i].getRect().x,spots[i].getRect().y+spots[i].getRect().height/3,null);
否则如果(i==36)
g2d.drawImage(imgChance3,spots[i].getRect().x,spots[i].getRect().y,null);
}else if(点[i]社区实例){
如果(i==2)
g2d.drawImage(imgComm1,spots[i].getRect().x,spots[i].getRect().y+spots[i].getRect().height/3,null);
否则如果(i==17)
g2d.drawImage(imgComm2,spots[i].getRect().x,spots[i].getRect().y,null);
否则如果(i==33)
g2d.drawImage(imgComm3,spots[i].getRect().x+spots[i].getRect().width/3,spots[i].getRect().y,null);
}否则{
setColor(spots[i].getColor());
填充(spots[i].getRect());
}
}
//画出每个点的轮廓
g2d.setColor(Color.black);
用于(现货指数:现货)
g2d.draw(index.getRect());
//画出整个棋盘的轮廓
g2d.drawRect(newX、newY、boardSize、boardSize);
//画出玩家的位置
for(玩家索引:玩家)
g2d.drawImage(index.getImage(),index.getLoc().x,index.getLoc().y,null);
}
基本上是一吨的文字来代表董事会,这是正在做的每一次董事会重新油漆。有什么建议吗


附加问题:我还刚刚开始为玩家在翻滚后制作移动动画(目前只是跳到目的地)。我创建了一个计时器,每次滚动需要1秒(例如:如果你滚动5次,移动需要5秒)。唯一的问题是,我真的没有一个好主意,如何显示缓慢移动的球员作品从开始的位置到结束。只需要有人给我一个基本的想法,这样我就可以朝着正确的方向前进。

你可以避免用:。另见此

但是,您必须为自己绘制在每次移动之间需要更新的矩形。你不仅需要新事物出现的矩形,还需要事物消失的矩形


有关Swing中的动画,请参见此。你会注意到我在这篇文章中链接的两个例子都来自官方。

将电路板绘制为
缓冲图像。在“绘制”方法中,绘制电路板的图像,然后在顶部绘制各部分

顺便说一句-使用Swing时,不要在顶级容器中绘制,而是使用
JComponent
JPanel
。对于后两种,覆盖
paintCom