无法在paintComponent中实现draw(矩形)

时间:2016-12-04 17:38:02

标签: java arraylist paintcomponent repaint

所以我试图实现一个游戏,在这个游戏中玩家被小行星击中加速,他们会失去生命。我想做的就是创建一个方形数组,删除玩家被击中的一个方格。出于某种原因,当我尝试在paintComponent方法中x.draw矩形时,我不断得到“找不到符号错误”,我不明白为什么。谁能解释一下呢?

package lightrunner;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.Random;
import java.awt.geom.Area;
import java.util.ArrayList;
import javax.swing.JMenuBar;
import javax.swing.Timer;
import java.util.Iterator;
import java.awt.geom.Rectangle2D;

   class LightRunner
{
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600,520);
        frame.setTitle("Light Runner");

    frame.add(new GamePanel());
    frame.setVisible(true);
     }

    public static class GamePanel extends JPanel 
        {

    public java.util.List<Rectangle> lives;
    Ship ship;
            Asteroid asteroid;
            Asteroid asteroid2;
            Timer timer;
           Graphics g;
           //  private final int MILLESECONDS_BETWEEN_FRAMES = 10;                
            Asteroid[] ast_array;
            boolean isStar;

    public GamePanel() 
            {         
        super();
                    setBackground(Color.black);
                 //   menu();
        initializeGameObjects();
        addKeyListener(new ShipMover());

        setFocusable(true);     
    }


    public void initializeGameObjects()
            {
                    ast_array = new Asteroid[8];
        //asteroid = new Asteroid(250,250, "/Users/nicholascerillo/Desktop/asteroid.png");
                   // asteroid2 = new Asteroid(300,300, "/Users/nicholascerillo/Desktop/asteroid.png");
        ship = new Ship(10, 164, "/Users/nicholascerillo/Desktop/ship.png");
        timer = new Timer(10, new GameMotion());
                   // 

                     for (int i = 0; i <= 7; i++)
                    {
                        Random rand = new Random();
                        int Astx = WIDTH + (i*200) + 200;
                        int Asty = rand.nextInt((600-0)) + 0; 
                       // int star = rand.nextInt((10-0)) + 0;
                        ast_array[i] = new Asteroid(Astx,Asty);

                    }
                       lives = new ArrayList<Rectangle>();
                       lives.add(new Rectangle( 10, 10, 10, 10));
                       lives.add(new Rectangle( 10, 25, 10, 10));
                       lives.add(new Rectangle( 10, 30, 10, 10));                
                   // GameMotion(); 

                  timer.start();  

    }
    @Override
    public void paintComponent(Graphics x) 
            {
        //super();//paintComponent(x);

        Graphics2D g2 = (Graphics2D)x;          
        ship.paint(g2);
                    g2.drawImage(ship.getShip(),ship.getX1(), ship.getY1(),this);

                     for(int r = 0; r < 7; r++)
                     {
                      g2.drawImage(ast_array[r].getImage(), ast_array[r].getX(), ast_array[r].getY(),null); 
                     }
        x.setColor(Color.RED);
        for (Rectangle life : lives) 
        {
            x.draw(life);
        }

        //asteroid.paint(g2);       

    }


    private class GameMotion implements ActionListener 
    {

        public GameMotion() 
                    {
                         // light = new Timer(20,this);
        }
        public void actionPerformed(ActionEvent evt)
                    {   
                       // light.start();


                            //checkBounds();
                       Iterator<Rectangle> it = lives.iterator();

                           // ship.BoundedY(); 
                           ship.move();
            ast_array[0].moveAsteroid();
                            ast_array[1].moveAsteroid();
                            ast_array[2].moveAsteroid();
                            ast_array[3].moveAsteroid();
                            ast_array[4].moveAsteroid();
                            ast_array[5].moveAsteroid();
                            ast_array[6].moveAsteroid();
                            ast_array[7].moveAsteroid();

                            while(it.hasNext())
                            {
                            Rectangle life = it.next();
                            for(int p = 0; p < 7; p++)
                            {
                            ship.isHit(ast_array[p]);
                            if(ship.getHit())
                            {
                               it.remove();  
                            }

                            }
                            }
            repaint();


        }


    }



    private class ShipMover implements KeyListener
       {
        public void keyPressed(KeyEvent evt) {

            int key = evt.getKeyCode();

            if (key == KeyEvent.VK_RIGHT ) 
                            {

                ship.setSpeedX(5);

                            }

            else if (key == KeyEvent.VK_LEFT) 
                            {

                ship.setSpeedX(-5);

            }
                            else if(key == KeyEvent.VK_UP /*&& (ship.BoundedY())*/){
                                ship.setSpeedY(-5);
                            }
                            else if(key == KeyEvent.VK_DOWN /*&& (ship.BoundedY())*/){
                                ship.setSpeedY(5);
                            }
        }
        public void keyReleased(KeyEvent evt) {
            int key = evt.getKeyCode();
            if ((key == KeyEvent.VK_LEFT) || (key == KeyEvent.VK_RIGHT) )  {
                ship.setSpeedX(0);
            }

                            else if((key == KeyEvent.VK_UP) || (key == KeyEvent.VK_DOWN))
                            {
                                ship.setSpeedY(0);
                            }

        }
        public void keyTyped(KeyEvent evt) {

        }
    }
        }
    }

0 个答案:

没有答案