重写paintComponent();绘制多个缓冲图像

时间:2013-02-04 17:12:51

标签: java swing graphics

我正在尝试制作塔防游戏。到目前为止,我已经制作了JFrame(称为GameFrame),其中显示了所有内容,添加了背景图像(.PNG)并且我正在尝试创建怪物并让它们“移动“在我创造的道路上。所以,我创建了一个GlassPane类,我在glassPane中设置为GameFrame

以下是GlassPane的代码:

public class GlassPane extends JComponent implements ActionListener{

    private ArrayList<MyPoint> path; //list of Points-pixel positions- (x,y pair) that monsters will follow
    private ArrayList<Monster> wave; //list of monsters that will try to reach the rift
    private Timer timer; //javax.swing.Timer
    private boolean waveEnd;//signing that the wave has ended(ignore it)

    public GlassPane(){

        super();
        createPath();//method is below
        wave = new ArrayList<Monster>();//monsters added in nextWave()
        timer = new Timer(4,this);//timer in order to slow down and smooth the movement of monsters
        waveEnd = false;
    }

    @Override
    protected void paintComponent(Graphics g){
            super.paintComponent(g);
            for(Monster m:wave){
               if(m.isVisible())//visible is a private variable in Monster class(defines whether i want to paint that monster or not)
                {
               g.drawImage(m.getImage(), m.getX(), m.getY(), this);
                }
             }
    }

    public void nextWave(){

        waveEnd = false;
                for(int i =0;i<20;i++)
                    wave.add(new Monster());
                wave.get(0).setVisible(true);//sets the first monster to be visible(paintable)
                int visibles = 1;//index 0 is visible,so index 1 is next to become visible
        while(!waveEnd){
                        if(visibles<19 && wave.get(0).getPathIndex()%50 == 0){
                        //meaning until all 20 monsters are visible(indexes 0-19),"every 50 steps(pixels) the first monster moves"
                           wave.get(visibles).setVisible(true);
                           //make another monster visible and start moving it
                           visibles++;//visible(moving) monsters increased by 1
            timer.start();//"move all visible monsters one step forward with a small delay until the next movement to make it soft"
        }
        JOptionPane.showMessageDialog(new JFrame(), "Finished!","All monsters reached the rift!",JOptionPane.INFORMATION_MESSAGE);//let me know then wave is finished
    }

    private void createPath(){
                //just making a lsit of pixels that I want monsters to follow
        path = new ArrayList<MyPoint>();
        for(int x=0;x<175;x++){
            path.add(new MyPoint(x,0));
        }

        for(int y=0;y<175;y++){
            path.add(new MyPoint(174,y));
        }

        for(int x=175;x<325;x++){
            path.add(new MyPoint(x,174));
        }

        for(int y=175;y<425;y++){
            path.add(new MyPoint(299,y));
        }

        for(int x=325;x<575;x++){
            path.add(new MyPoint(x,424));
        }

        for(int y=424;y>175;y--){
            path.add(new MyPoint(574,y));
        }

        for(int x=575;x<1001;x++){
            path.add(new MyPoint(x,174));
        }   
    }

    @Override

    public void actionPerformed(ActionEvent arg0) {
             for(Monster m:wave)
                if(m.isVisible())
            m.move(path);
                //"move every visible monster by one step"
        if(m.get(19).getPathIndex() == 1674)//1674 is the path's last index
            waveEnd = true;//if the last monster reaches the end,then all have
                //meaning the wave is finished
        this.repaint();//refresh any changes to the monsters' positions
        }
}

问题是只有第一个怪物在路径中移动,这意味着当我调用repaint()时只绘制了一个怪物。我在p aintComponent(Graphics g)做错了什么?因为我相信这就是我的错误所在...我怎样才能一次又一次地绘制所有“可用”的图像呢?

注意:如果重要的话,它们是.PNG格式的缓冲图像。

我虽然添加了Monster类代码,但我相信解释方法的功能足以了解正在发生的事情。如果有人需要更多细节,请告诉我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为你必须在循环中加上括号。 简写只在循环后的第一行循环

//Output: 
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//BBBBBBBBBBBBBBBBBB
for(Monster m:wave)
System.out.println("AAAAAAAAAAAAAA");
System.out.println("BBBBBBBBBBBBBBB");


//Output: 
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//BBBBBBBBBBBBBBBBBB
//BBBBBBBBBBBBBBBBBB
//BBBBBBBBBBBBBBBBBB
//BBBBBBBBBBBBBBBBBB  
for(Monster m:wave)
{
System.out.println("AAAAAAAAAAAAAA");
System.out.println("BBBBBBBBBBBBBBB");
}