League Invaders Pt.3 Drawing Shapes

1. Go to your GamePanel class.

2. To draw custom things we need to use a method from the JPanel class called paintComponent. Copy the method below exactly as written and put it in your GamePanel class.

        @Override

public void paintComponent(Graphics g){

                

        }

Make sure to import the java.awt.Graphics class.

3. We can use the Graphics object in the parameters of our method to draw objects. Inside the paintComponent method, use the Graphics object (g) to draw a rectangle like this:

        g.fillRect(10, 10, 100, 100);

The parameters for the rectangle are the exact same as Processing (x, y, width, height). So this will draw a rectangle that is located at (10, 10) and is 100 by 100 pixels big.

4. In the ActionPerformed method delete your syso command. Call repaint(); This will refresh our image.

5. Run the program. Do you see a square appear in your window?