-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComboBoxDemo.java
101 lines (83 loc) · 3.08 KB
/
ComboBoxDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* ComboBoxDemo.java */
import objectdraw.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* This class demonstrates the use of a Combo box to control the speed of a
* falling ball. Adapted from example in Bruce et al.
*/
public class ComboBoxDemo extends WindowController implements ActionListener {
// location of instructions
private static final Location INSTR_LOCATION = new Location(100,25);
private static final int
SLOW_SPEED = 1, // slow setting
MEDIUM_SPEED = 3, // medium setting
FAST_SPEED = 10; // fast setting
private FallingBall droppedBall; // the falling ball
private JComboBox<String> speedChoice; // Combo box to select ball speed
private int speed; // Current speed setting
// display instructions and combo box
public void begin() {
new Text( "Click to make a falling ball...", INSTR_LOCATION, canvas );
speed = SLOW_SPEED;
String[] options = {"Slow", "Medium", "Fast"};
speedChoice = new JComboBox<String>(options);
speedChoice.setSelectedItem( "Slow" ); // Display "Medium" initially
speedChoice.addActionListener ( this ); // this class is listener
Container contentPane = getContentPane(); // Add combo box to south
contentPane.add( speedChoice, BorderLayout.SOUTH );
contentPane.validate();
}
// make a new ball when the player clicks
public void onMouseClick( Location point ) {
droppedBall = new FallingBall( point, speed, canvas );
}
// reset ball speed from combo box setting
public void actionPerformed( ActionEvent evt ) {
Object newLevel = speedChoice.getSelectedItem ();
if ( newLevel.equals( "Slow" )) {
speed = SLOW_SPEED;
} else if ( newLevel.equals( "Medium" )) {
speed = MEDIUM_SPEED;
} else if ( newLevel.equals( "Fast" )) {
speed = FAST_SPEED;
}
if ( droppedBall != null ) {
droppedBall.setSpeed( speed );
}
}
// Main program for application
public static void main(String [] args) {
ComboBoxDemo theDemo = new ComboBoxDemo();
theDemo.startController(500, 500);
}
/** Animate a falling ball
*/
class FallingBall extends ActiveObject {
private static final int BALLSIZE = 30;
private static final int DELAY_TIME = 33;
private DrawingCanvas canvas; // canvas to draw on
private FilledOval ball; // Image of ball as circle
private int speed; // current speed of ball
// Draw ball at location and w/speed given in parameters
public FallingBall( Location ballLocation, int initSpeed,
DrawingCanvas aCanvas ) {
canvas = aCanvas;
ball = new FilledOval( ballLocation, BALLSIZE, BALLSIZE, canvas );
speed = initSpeed;
start();
}
// Move ball down until off of canvas
public void run() {
while ( ball.getY() < canvas.getHeight() ) {
ball.move( 0, speed );
pause( DELAY_TIME );
}
ball.removeFromCanvas();
}
// reset speed of ball
public void setSpeed( int newSpeed ) {
speed = newSpeed;
}
}
}