Java Reference
In-Depth Information
Display 19.3
The Runnable Interface (part 2 of 2)
39 public void actionPerformed(ActionEvent e)
40 {
41 startThread();
42 }
43 public void run()
44 {
45 Graphics g = box.getGraphics();
46 for ( int y = 0; y < FILL_HEIGHT; y = y + CIRCLE_SIZE)
47 for ( int x = 0; x < FILL_WIDTH; x = x + CIRCLE_SIZE)
48 {
49 g.fillOval(x, y, CIRCLE_SIZE, CIRCLE_SIZE);
50 doNothing(PAUSE);
51 }
52 }
53 public void startThread()
54 {
55 Thread theThread = new Thread( this );
56 theThread.start();
57 }
58 public void doNothing( int milliseconds)
59 {
60 try
61 {
62 Thread.sleep(milliseconds);
63 }
64 catch (InterruptedException e)
65 {
66 System.out.println("Unexpected interrupt");
67 System.exit(0);
68 }
69 }
70 }
Self-Test Exercises
1. Because sleep is a static method, how can it possibly know what thread it needs
to pause?
2. Where was polymorphism used in the program in Display 19.2 ? ( Hint : We are
looking for an answer involving the class Packer .)
 
 
Search WWH ::




Custom Search