Write an application that allows the user to play a game, the goal of which is to prevent a bouncing ball from falling off the bottom of the application. When the user presses the S key, the game starts and a blue ball will bounce off the top, left and right sides (the “walls”) of the application. There should be a horizontal bar on the bot- tom of the application, which serves as a paddle, to prevent the ball from hitting the bottom of the application (the ball can bounce off the paddle, but not off the bottom of the applica- tion.) The user can move the paddle using the left and right arrow keys. If the ball hits the paddle, the ball should bounce up, and the game should continue. If the ball hits the bottom of the application, the game should end. Most of the geometr



a) Copying the template to your working directory. Copy the C:Examples Tutorial22ExercisesBouncingBall directory to your C:SimplyJava directory.

b) Opening the template file. Open the BouncingBall.java file in your text editor.

c) Writing code to start the game. In line 131 (in the bouncingBallKeyPressed method), begin an if statement to determine if the S key has been pressed. You will need to use the KeyEvent constant VK_S. Inside the if statement, start ballTimer by calling the start method of ballTimer, which is a Timer declared as an instance variable in the template.

d) Inserting code to move the paddle left. Following the if statement you added in Step c, add an else if statement that tests if the user pressed the left arrow key and if the paddle’s horizontal position, stored in rectX, is greater than ten. You will need to use KeyEvent constant VK_LEFT. If the paddle’s horizontal position equals ten, the left edge of the paddle is touching the left wall and the pad

```
1 // BouncingBall.java
2 // Game whose goal is preventing ball from falling off the bottom.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.Random;
6 import javax.swing.*;
7
8 public class BouncingBall extends JFrame
9 {
10 // Random object to generate random integers
11 private Random randomGenerator = new Random();
12
13 // determine random starting point for the ball
14 private int x = 25 + randomGenerator.nextInt( 201 );
15 private int y = 25 + randomGenerator.nextInt( 201 );
16
17 // position and length of paddle
18 private int rectX = 126;
19 private int rectWidth = 80;
20
21 // distance ball travels each time ball is moved
22 private int deltaX = 2 + randomGenerator.nextInt( 6 );
23 private int deltaY = 2 + randomGenerator.nextInt( 6 );
24
25 // Timer for ball
26 private Timer ballTimer;
27
28 // no-argument constructor
29 public BouncingBall()
30 {
31 createUserInterface();
32 }
33
34 // create and position GUI components; register event handlers
35 private void createUserInterface()
36 {
37 // register KeyListener
38 addKeyListener(
39
40 new KeyAdapter() // anonymous inner class
41 {
42 // event handler called when a key is pressed
43 public void keyPressed( KeyEvent event )
44 {
45 bouncingBallKeyPressed( event );
46 }
47
48 } // end anonymous inner class
49
50 ); // end call to addKeyListener
51
52 // set up ballTimer
53 ballTimer = new Timer( 30,
54
55 new ActionListener() // anonymous inner class
56 {
57 // event handler called every 30 milliseconds
58 public void actionPerformed( ActionEvent event )
59 {
60 ballTimerActionPerformed( event );
61 }
62
63 } // end anonymous inner class
64
65 ); // end Timer constructor
66
67 // set properties of application's window
68 setTitle( "Bouncing Ball" ); // set title bar string
69 setSize( 415, 430 ); // set window size
70 setVisible( true ); // display window
71
72 } // end method createUserInterface
73
74 // draw the ball and the paddle
75 public void paint( Graphics graphics )
76 {
77 super.paint( graphics );
78
79 // draw the ball
80 graphics.setColor( Color.BLUE );
81 graphics.fillOval( x, y, 10, 10 );
82
83 // draw the paddle
84 graphics.setColor( Color.RED );
85 graphics.fillRect( rectX, 410, rectWidth, 10 );
86
87 } // end method paint
88
89 // move the ball; handle bouncing
90 private void ballTimerActionPerformed( ActionEvent event )
91 {
92 // update the position of the ball
93 x += deltaX;
94 y += deltaY;
95
96 if ( y <= 25 )
97 {
98 // bounce the ball off the ceiling
99 deltaY = 2 + randomGenerator.nextInt( 6 );
100 }
101 else if ( y >= 400 && x >= rectX && x <=
102 ( rectX + rectWidth ) )
103 {
104 // bounce the ball off the paddle
105 deltaY = -2 - randomGenerator.nextInt( 6 );
106 }
107 else if ( y >= 430 )
108 {
109 // end the game
110 ballTimer.stop();
111 }
112
113 if ( x <= 5 )
114 {
115 // bounce the ball off the left wall
116 deltaX = 2 + randomGenerator.nextInt( 6 );
117 }
118 else if ( x >= 400 )
119 {
120 // bounce the ball off the right wall
121 deltaX = -2 - randomGenerator.nextInt( 6 );
122 }
123
124 repaint();
125
126 } // end method ballTimerActionPerformed
127
128 // start the game, move paddle left or right
129 private void bouncingBallKeyPressed( KeyEvent event )
130 {
131 if ( event.getKeyCode() == KeyEvent.VK_S )
132 {
133 // start the game ballTimer.start();
134 ballTimer.start();
135 }
136 else if ( event.getKeyCode() == KeyEvent.VK_LEFT
137 && rectX > 10 )
138 }
139 && rectX > 10 )
140 }
141 else if ( event.getKeyCode() == KeyEvent.VK_RIGHT
142 && rectX < 400 - rectWidth )
143 {
144 rectX += 10; // move the paddle right
145 }
146
147 } // end method bouncingBallKeyPressed
148
149

149 // main method
150 public static void main( String[] args )
151 {
152 BouncingBall application = new BouncingBall();
153 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
154
155 } // end method main
156
157 } // end class BouncingBall
```

Computer Science & Information Technology

You might also like to view...

When you copy and paste an object into PowerPoint, it is ________

A) embedded B) cut from the original application and pasted to the clipboard C) removed from the original location and pasted into the new location D) linked

Computer Science & Information Technology

The stream manipulator ____ displays Boolean values as true and false rather than 1 and 0.

a. booltext b. bool c. boolalpha d. showbool

Computer Science & Information Technology