/Users/lyon/j4p/src/bookExamples/ch18Swing/awt/BlinkFrame.java

1    package bookExamples.ch18Swing.awt; 
2     
3    import java.awt.*; 
4    import java.awt.event.MouseEvent; 
5    import java.awt.event.MouseListener; 
6     
7     
8    public class BlinkFrame extends Frame implements 
9            MouseListener, Runnable { 
10    
11       Thread thread; 
12       Color[] colors = 
13               {Color.red, Color.green, Color.blue}; 
14       int index = 0; 
15    
16       public void init() { 
17    
18           thread = new Thread(this); 
19           thread.start(); 
20           addMouseListener(this); 
21       } 
22    
23       public void run() { 
24           repaint(); 
25       } 
26    
27       public static void main(String args[]) { 
28           BlinkFrame b = new BlinkFrame(); 
29           b.setVisible(true); 
30           b.setSize(200, 200); 
31           b.init(); 
32       } 
33    
34       public void paint(Graphics g) { 
35           while (true) { 
36               try { 
37                   g.setColor(colors[index]); 
38                   g.fillOval(30, 30, 20, 20); 
39                   thread.sleep(500); 
40                   g.setColor(Color.white); 
41                   g.fillOval(30, 30, 20, 20); 
42                   thread.sleep(250); 
43               } catch (InterruptedException e) { 
44               } 
45           } 
46       } 
47    
48       public void mousePressed(MouseEvent e) { 
49           index = (index + 1) % colors.length; 
50       } 
51    
52       public void mouseClicked(MouseEvent e) { 
53       } 
54    
55       public void mouseEntered(MouseEvent e) { 
56       } 
57    
58       public void mouseExited(MouseEvent e) { 
59       } 
60    
61       public void mouseReleased(MouseEvent e) { 
62       } 
63    
64   }