forked from ZhiqingXiao/java-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter21.java
537 lines (408 loc) · 13.2 KB
/
chapter21.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// An improved version of the StopWatch class from Try This 17-1.
// This version uses a separate thread to display the elapsed
// time when the stopwatch is running.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
class ThreadStopWatch {
JLabel jlab; // display the elapsed time
JButton jbtnStart; // start the stopwatch
JButton jbtnStop; // stop the stopwatch
long start; // holds the start time in milliseconds
boolean running=false; // true when stopwatch is running
Thread thrd; // reference to the timing thread
ThreadStopWatch() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Thread-based Stopwatch");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(230, 90);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the elapsed-time label.
jlab = new JLabel("Press Start to begin timing.");
// Make the Start and Stop buttons.
jbtnStart = new JButton("Start");
jbtnStop = new JButton("Stop");
// Initially disable the Stop button.
jbtnStop.setEnabled(false);
// Create the Runnable instance that will become the second thread.
Runnable myThread = new Runnable() {
// This method will run in a separate thread.
public void run() {
try {
// Report elapsed time every tenth of a second.
for(; ; ) {
// Pause for a tenth of a second.
Thread.sleep(100);
// Invoke updateTime() on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
updateTime();
}
});
}
} catch(InterruptedException exc) {
System.out.println("Call to sleep was interrupted.");
System.exit(1);
}
}
};
// Create a new thread.
thrd = new Thread(myThread);
// Start the thread.
thrd.start();
// Add the action listeners for the Start and
// Stop buttons.
jbtnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// Store start time.
start = Calendar.getInstance().getTimeInMillis();
// Reverse the state of the buttons.
jbtnStop.setEnabled(true);
jbtnStart.setEnabled(false);
// Start the stopwatch.
running = true;
}
});
jbtnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
long stop = Calendar.getInstance().getTimeInMillis();
// Compute the elapsed time.
jlab.setText("Elapsed time is "
+ (double) (stop - start)/1000);
// Reverse the state of the buttons.
jbtnStart.setEnabled(true);
jbtnStop.setEnabled(false);
// Stop the stopwatch.
running = false;
}
});
// Add the buttons and label to the content pane.
jfrm.add(jbtnStart);
jfrm.add(jbtnStop);
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
// Update the elapsed time display.
void updateTime() {
if(!running) return;
long temp = Calendar.getInstance().getTimeInMillis();
jlab.setText("Elapsed time is " +
(double) (temp - start)/1000);
}
public static void main(String[] args) {
// Create the frame on the event-dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ThreadStopWatch();
}
});
}
}
// -----------------------------------------
// This version of the stopwatch uses the Timer class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
class TimerStopWatch {
JLabel jlab; // display the elapsed time
JButton jbtnStart; // start the stopwatch
JButton jbtnStop; // stop the stopwatch
long start; // holds the start time in milliseconds
Timer swTimer; // the timer for the stopwatch
TimerStopWatch() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Timer-based Stopwatch");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(230, 90);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the elapsed-time label.
jlab = new JLabel("Press Start to begin timing.");
// Make the Start and Stop buttons.
jbtnStart = new JButton("Start");
jbtnStop = new JButton("Stop");
jbtnStop.setEnabled(false);
// This action listener is called when the timer
// goes off.
ActionListener timerAL = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
updateTime();
}
};
// Create a timer that goes off every tenth of a second.
swTimer = new Timer(100, timerAL);
// Add the action listeners for the start and
// stop buttons.
jbtnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// Store start time.
start = Calendar.getInstance().getTimeInMillis();
// Reverse the state of the buttons.
jbtnStop.setEnabled(true);
jbtnStart.setEnabled(false);
// Start the stopwatch.
swTimer.start();
}
});
jbtnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
long stop = Calendar.getInstance().getTimeInMillis();
// Compute the elapsed time.
jlab.setText("Elapsed time is "
+ (double) (stop - start)/1000);
// Reverse the state of the buttons.
jbtnStart.setEnabled(true);
jbtnStop.setEnabled(false);
// Stop the stopwatch.
swTimer.stop();
}
});
// Add the buttons and label to the content pane.
jfrm.add(jbtnStart);
jfrm.add(jbtnStop);
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
// Update the elapsed time display. Notice
// that the running variable is no longer needed.
void updateTime() {
long temp = Calendar.getInstance().getTimeInMillis();
jlab.setText("Elapsed time is " +
(double) (temp - start)/1000);
}
public static void main(String[] args) {
// Create the frame on the event-dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TimerStopWatch();
}
});
}
}
// -----------------------------------------
// A simple Swing-based applet
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
This HTML can be used to launch the applet:
<object code="MyApplet" width=240 height=100>
</object>
*/
public class MyApplet extends JApplet {
JButton jbtnOne;
JButton jbtnTwo;
JLabel jlab;
// Called first.
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
guiInit(); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of " + exc);
}
}
// Set up and initialize the GUI.
private void guiInit() {
// Set the applet to use flow layout.
setLayout(new FlowLayout());
// Create two buttons and a label.
jbtnOne = new JButton("One");
jbtnTwo = new JButton("Two");
jlab = new JLabel("Press a button.");
// Add action listeners for the buttons.
jbtnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button One pressed.");
}
});
jbtnTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button Two pressed.");
}
});
// Add the components to the applet's content pane.
add(jbtnOne);
add(jbtnTwo);
add(jlab);
}
}
// -----------------------------------------
// Try This 21-1: A Swing-based applet that scrolls
// text through a label.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
This HTML can be used to launch the applet:
<object code="ScrollText" width=240 height=100>
</object>
*/
public class ScrollText extends JApplet {
JLabel jlab;
String msg = " Swing makes the GUI move! ";
ActionListener scroller;
Timer stTimer; // this timer controls the scroll rate
// Initialize the applet.
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
guiInit();
}
});
} catch(Exception exc) {
System.out.println("Can't create because of " + exc);
}
}
// Start the timer when the applet is started.
public void start() {
stTimer.start();
}
// Stop the timer when the applet is stopped.
public void stop() {
stTimer.stop();
}
// Stop the timer when the applet is destroyed.
public void destroy() {
stTimer.stop();
}
// Initialize the timer GUI.
private void guiInit() {
// Create the label that will scroll the message.
jlab = new JLabel(msg);
jlab.setHorizontalAlignment(SwingConstants.CENTER);
// Create the action listener for the timer.
scroller = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// Left-scroll the message one character.
char ch = msg.charAt(0);
msg = msg.substring(1, msg.length());
msg += ch;
jlab.setText(msg);
}
};
// Create the timer.
stTimer = new Timer(200, scroller);
// Add the label to the applet content pane.
add(jlab);
}
}
// -----------------------------------------
// Paint lines to a panel.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
// This class extends JPanel. It overrides
// the paintComponent() method so that random
// data is plotted in the panel.
class PaintPanel extends JPanel {
Insets ins; // holds the panel's insets
Random rand; // used to generate random numbers
PaintPanel(int w, int h) {
// Use a red line border.
setBorder(BorderFactory.createLineBorder(Color.RED, 1));
// Set the preferred dimension as specified.
setPreferredSize(new Dimension(w, h));
rand = new Random();
}
// Override the paintComponent() method.
protected void paintComponent(Graphics g) {
// Always call the superclass method first.
super.paintComponent(g);
// Get the height and width of the component.
int height = getHeight();
int width = getHeight();
// Get the insets.
ins = getInsets();
// Fill the panel by plotting random data
// in the form of a bar graph.
for(int i=ins.left+5; i <= width-ins.right-5; i += 4) {
// Obtain a random number between 0 and
// the maximum height of the drawing area.
int h = rand.nextInt(height-ins.bottom);
// If a generated value is in or too close to the
// border, change it to just inside the border.
if(h <= ins.top) h = ins.top+1;
// Draw a line that represents the data.
g.drawLine(i, height-ins.bottom, i, h);
}
}
// Change the border size.
public void changeBorderSize(int size) {
setBorder(
BorderFactory.createLineBorder(Color.RED, size));
}
}
// Demonstrate painting directly onto a panel.
class PaintDemo {
JButton jbtnMore;
JButton jbtnSize;
JLabel jlab;
PaintPanel pp;
boolean big; // use to toggle size of panel
PaintDemo() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Painting Demo");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(240, 260);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the panel that will be painted.
pp = new PaintPanel(100, 100);
// Make the buttons.
jbtnMore = new JButton("Show More Data");
jbtnSize = new JButton("Change Border Size");
// Describe the graph.
jlab = new JLabel("Bar Graph of Random Data");
// Repaint the panel when the Show More Data button
// is clicked.
jbtnMore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
pp.repaint();
}
});
// Set the border size of the panel when the
// Change Border Size button is clicked.
// Changing the border size automatically
// results in a repaint.
jbtnSize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(!big) pp.changeBorderSize(5);
else pp.changeBorderSize(1);
big = !big;
}
});
// Add the buttons, label, and panel to the content pane.
jfrm.add(jlab);
jfrm.add(pp);
jfrm.add(jbtnMore);
jfrm.add(jbtnSize);
big = false;
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String[] args) {
// Create the frame on the event-dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PaintDemo();
}
});
}
}