The following code produces a ball that bounces off the bottom of the panel. Note that I used an anonymous panel class for convenience. I could have defined this as a separate class, as we've done in the previous exercises. Other things to note are the use of javax.swing.Timer and the separation of movement (in the actionPerformed() method) and drawing (in the paintComponent() method) # Exercise 1. Make the ball bounce off all four walls 2. Add a second bouncing ball 3. Add a mouse controlled paddle to bounce the balls off (use a mouseMotionListener) 4. Make one of the balls bounce off the paddle. Hint: see if the rectangles surrounding the ball and the paddle intersect. There are lots of examples on line on how to do this. # Extension Make a tennis game like pong: <https://www.ponggame.org/>. * Use the mouse to control the right hand paddle and keys to control the left one. You'll need to add a KeyListener. * Add collision detection to bounce the ball back from the paddles * Add a scoring system for when the ball goes off the edge of the panel. ![[GUIBall.java]] ```java package guiball; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; /** * * @author ajb */ public class GUIBall extends JFrame implements ActionListener { int x = 10; int y = 10; int xVect = 1; int yVect = 1; int ballDiameter = 30; /** * @param args the command line arguments */ GUIBall() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(640, 480); this.setLocationRelativeTo(null); this.getContentPane().add(panel); this.setVisible(true); Timer timer = new Timer(10, this); timer.start(); } JPanel panel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.fillOval(x, y, ballDiameter, ballDiameter); } }; public static void main(String[] args) { // TODO code application logic here new GUIBall(); } @Override public void actionPerformed(ActionEvent ae) { if (y > this.getContentPane().getHeight() - ballDiameter) { yVect = -yVect; } x += xVect; y += yVect; panel.repaint(); } } ```