League Invaders Pt. 5

Game Objects

1. Create a new class called GameObject.

2. Add the following member variables to your GameObject class.

        int x;

        int y;

        int width;

        int height;

3. Add parameters to your constructor that match the member variables and use these to initialize the member variables.

4. Create a void method called update.

5. Create another void method called draw. Make this method take a parameter of the Graphics class. If you need an example, look at the paintComponent method in the GamePanel class.

6. Cut the line of code that draws a square from your GamePanel's paintComponent method and paste it into your draw method of the GameObject class.

7. In the GamePanel class, create an object of the GameObject class. Initialize it in the GamePanel constructor.

8. In the paintComponent method, use your GameObject object to call the draw method. Make sure to pass the Graphics object.

9. Run your program. Does your square still show up?

10. In the actionPerformed method of your GamePanel class, use your GameObject object to call the update method. Also call repaint(); This method belongs to the JPanel class and will redraw the panel.

11. Go to your GameObject class. Replace the x and y position of the square to your x and y member variables.

12. Add code to your update method to move the object around. Do not move on until this has been completed.