/Users/lyon/j4p/src/ip/hak/ImageComponent.java

1    package ip.hak; 
2     
3    import java.awt.*; 
4     
5    public class ImageComponent extends Component { 
6     
7        MyPanel mp; 
8        Image im[],buffer; 
9        Graphics gContext; 
10       int total,current; 
11       boolean mo = false; 
12    
13    
14       public ImageComponent() { 
15       } 
16    
17       public ImageComponent(int w, int h, MyPanel myp) { 
18           mp = myp; 
19           setSize(w, h); 
20       } 
21    
22       public ImageComponent(int w, int h, MyPanel myp, int id) { 
23           mp = myp; 
24           setSize(w, h); 
25       } 
26    
27       public void switchMessage() { 
28           mo = !mo; 
29           repaint(); 
30       } 
31    
32       public void showImage(Image ia[], Dimension di) { 
33           im = ia; 
34           total = im.length; 
35           buffer = createImage(di.width, di.height); 
36           gContext = buffer.getGraphics(); 
37           Dimension d = getSize(); 
38           gContext.drawImage(im[0], 0, 0, d.width, d.height, this); 
39           current = 1; 
40       } 
41    
42       public void paint(Graphics g) { 
43    
44           super.paint(g); 
45           Dimension d = getSize(); 
46           setForeground(Color.black); 
47    
48           if (im == null) { 
49    
50               g.drawRect(1, 1, d.width - 2, d.height - 2); 
51               if (mo) 
52                   g.drawString("Wait...", d.width / 2 - 10, d.height / 2 - 5); 
53               return; 
54           } 
55    
56           g.drawImage(buffer, 0, 0, d.width, d.height, this); 
57    
58    
59    
60           // draw new image in buffer 
61           gContext.drawImage(im[current], 0, 0, d.width, d.height, this); 
62    
63           // loop 
64           current = (current + 1) % total; 
65    
66           try { 
67               Thread.sleep(100); 
68           } catch (InterruptedException e) { 
69               System.out.println(e); 
70           } 
71    
72           repaint(); // display buffer image 
73       } 
74    
75   }