Java Reference
In-Depth Information
22 add(jsbDelay, BorderLayout.NORTH);
23 add(ball, BorderLayout.CENTER);
24 add(panel, BorderLayout.SOUTH);
25
26
// Register listeners
jbtSuspend.addActionListener(
new
ActionListener() {
27
28 @Override
29
register listener
public void
actionPerformed(ActionEvent e) {
30
31 }
32 });
33
34 @Override
35
ball.suspend();
suspend
jbtResume.addActionListener(
new
ActionListener() {
register listener
public void
actionPerformed(ActionEvent e) {
36
37 }
38 });
39
40 @Override
41
ball.resume();
resume
jsbDelay.addAdjustmentListener(
new
AdjustmentListener() {
register listener
public void
adjustmentValueChanged(AdjustmentEvent e) {
42
43 }
44 });
45 }
46 }
ball.setDelay(jsbDelay.getMaximum() - e.getValue());
new delay
The
BallControl
class extends
JPanel
to display the ball with a scroll bar and two control
buttons. When the
Suspend
button is clicked, the ball's
suspend()
method is invoked to sus-
pend the ball's movement (line 30). When the
Resume
button is clicked, the ball's
resume()
method is invoked to resume the ball's movement (line 36). The bouncing speed can be
changed using the scroll bar.
L
ISTING
18.9
BounceBallApp.java
1
import
java.awt.*;
2
import
javax.swing.*;
3
4
public class
BounceBallApp
extends
JApplet
{
5
public
BounceBallApp() {
6
7 }
8 }
add(
new
BallControl());
add
BallControl
main
method omitted
The
BounceBallApp
class simply places an instance of
BallControl
in the applet. The
main
method is provided in the applet (not displayed in the listing for brevity) so that you can
also run it standalone.
18.15
✓
✓
How does the program make the ball moving?
Check
18.16
How does the code in Listing 18.7 Ball.java change the direction of the ball movement?
Point
18.17
What does the program do when the
Suspend
button is clicked? What does the pro-
gram do when the
Resume
button is clicked?
This section develops an applet for playing tic-tac-toe.
Key
Point
From the many examples in this and earlier chapters you have learned about objects, classes,
arrays, class inheritance, GUI, event-driven programming, and applets. Now it is time to put
VideoNote
TicTacToe