/Users/lyon/j4p/src/ip/gui/IconComponent.java

1    package ip.gui; 
2     
3    import java.awt.*; 
4    import java.awt.event.ActionEvent; 
5    import java.awt.event.ActionListener; 
6    import java.awt.event.MouseAdapter; 
7    import java.awt.event.MouseEvent; 
8    import java.awt.image.ColorModel; 
9    import java.awt.image.MemoryImageSource; 
10    
11   public class IconComponent extends Component { 
12       Dimension imageDim; 
13       Image image; 
14       Image invertImage; 
15       Image nonInvertImage; 
16    
17       public boolean contains(int x, int y) { 
18           return 
19                   ( 
20                   x >= 0 && x < imageDim.width && 
21                   y >= 0 && y < imageDim.height && 
22                   image != null); 
23       } 
24    
25       public IconComponent(byte r[][]) { 
26           nonInvertImage = byte2Image(r); 
27           image = nonInvertImage; 
28           invertImage = byte2InvertImage(r); 
29           imageDim = new Dimension(r.length, r[0].length); 
30           addMouseListener(new MouseEventHandler()); 
31    
32           setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 
33       } 
34    
35       public void invert(boolean t) { 
36           if (t) 
37               image = invertImage; 
38           else 
39               image = nonInvertImage; 
40           repaint(); 
41       } 
42    
43    
44       public Dimension getPreferredSize() { 
45           return imageDim; 
46       } 
47    
48       public void paint(Graphics g) { 
49           if (image != null) { 
50               g.drawImage(image, 0, 0, imageDim.width, imageDim.height, 
51                       this); 
52               g.drawRect(0, 0, imageDim.width + 1, imageDim.height + 1); 
53           } 
54       } 
55    
56       private static Image byte2Image(byte r[][]) { 
57    
58           int w = r.length; 
59           int h = r[0].length; 
60           int v = 0; 
61           Toolkit tk = Toolkit.getDefaultToolkit(); 
62           int pels[] = new int[w * h]; 
63           for (int x = 0; x < w; x++) 
64               for (int y = 0; y < h; y++) { 
65                   if (r[x][y] == 1) 
66                       v = 255; 
67                   else 
68                       v = 0; 
69                   pels[y + x * h] = 
70                           0xff000000 
71                           | (v << 16) 
72                           | (v << 8) 
73                           | v; 
74               } 
75           Image i = tk.createImage( 
76                   new MemoryImageSource( 
77                           w, 
78                           h, 
79                           ColorModel.getRGBdefault(), 
80                           pels, 0, 
81                           w)); 
82           return i; 
83       } 
84    
85       private static Image byte2InvertImage(byte r[][]) { 
86    
87           int w = r.length; 
88           int h = r[0].length; 
89           int v = 0; 
90           Toolkit tk = Toolkit.getDefaultToolkit(); 
91           int pels[] = new int[w * h]; 
92           for (int x = 0; x < w; x++) 
93               for (int y = 0; y < h; y++) { 
94                   if (r[x][y] == 1) 
95                       v = 0; 
96                   else 
97                       v = 255; 
98                   pels[y + x * h] = 
99                           0xff000000 
100                          | (v << 16) 
101                          | (v << 8) 
102                          | v; 
103              } 
104          Image i = tk.createImage( 
105                  new MemoryImageSource( 
106                          w, 
107                          h, 
108                          ColorModel.getRGBdefault(), 
109                          pels, 0, 
110                          w)); 
111          return i; 
112      } 
113   
114      transient ActionListener actionListener; 
115   
116      public synchronized void addActionListener(ActionListener l) { 
117          actionListener = AWTEventMulticaster.add(actionListener, l); 
118      } 
119   
120      public synchronized void removeActionListener(ActionListener l) { 
121          actionListener = AWTEventMulticaster.remove(actionListener, l); 
122      } 
123   
124      class MouseEventHandler extends MouseAdapter { 
125          public void mousePressed(MouseEvent e) { 
126              if (actionListener != null) { 
127                  actionListener.actionPerformed( 
128                          new ActionEvent(IconComponent.this, 
129                                  ActionEvent.ACTION_PERFORMED, null)); 
130              } 
131          } 
132   
133      } 
134   
135  }