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

1    package ip.hak; 
2     
3    import java.awt.*; 
4    import java.awt.image.ColorModel; 
5    import java.awt.image.MemoryImageSource; 
6     
7     
8    public class P4 extends Component { 
9        Point center; 
10       Image image, selectedImage, unselectedImage; 
11       Dimension imageDim; 
12       boolean inv; 
13       int idx, idy; 
14    
15       public P4(Point cen, int ix, int iy) { 
16           center = cen; 
17           unselectedImage = byte2Image(unselected); 
18           selectedImage = byte2Image(selected); 
19           image = unselectedImage; 
20           imageDim = new Dimension(unselected.length, unselected[0].length); 
21           inv = false; 
22           idx = ix; 
23           idy = iy; 
24       } 
25    
26       public int getIdx() { 
27           return idx; 
28       } 
29    
30       public int getIdy() { 
31           return idy; 
32       } 
33    
34       public void setCenter(Point p) { 
35           center = p; 
36       } 
37    
38       public boolean contains(Point p) { 
39           int hx = imageDim.width / 2; 
40           int hy = imageDim.height / 2; 
41           int minx = center.x - hx; 
42           int maxx = center.x + hx; 
43           int miny = center.y - hy; 
44           int maxy = center.y + hy; 
45    
46           return (p.x >= minx && p.x <= maxx && p.y >= miny && p.y <= maxy && image != null); 
47       } 
48    
49       public Point getCenter() { 
50           return center; 
51       } 
52    
53       public Dimension getPreferredSize() { 
54           return imageDim; 
55       } 
56    
57       public void paint(Graphics g) { 
58           if (image != null) { 
59               Dimension d = imageDim; 
60               g.drawImage(image, 0, 0, d.width, d.height, this); 
61           } 
62       } 
63    
64       private static Image byte2Image(byte r[][]) { 
65           int w = r.length; 
66           int h = r[0].length; 
67           int v = 0; 
68           Toolkit tk = Toolkit.getDefaultToolkit(); 
69           int pels[] = new int[w * h]; 
70           for (int x = 0; x < w; x++) 
71               for (int y = 0; y < h; y++) { 
72                   if (r[x][y] == 1) 
73                       v = 255; 
74                   else 
75                       v = 0; 
76                   pels[y + x * h] = 0xff000000 | (v << 16) | (v << 8) | v; 
77               } 
78           Image i = tk.createImage(new MemoryImageSource(w, h, ColorModel.getRGBdefault(), pels, 0, w)); 
79           return i; 
80       } 
81    
82       public void invert() { 
83           if (!inv) 
84               image = selectedImage; 
85           else 
86               image = unselectedImage; 
87           inv = !inv; 
88           repaint(); 
89       } 
90    
91       private static byte selected[][] = { 
92           {1, 1, 1, 0, 0, 0, 0, 1, 1, 1}, 
93           {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
94           {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
95           {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
96           {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
97           {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
98           {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
99           {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
100          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
101          {1, 1, 1, 1, 0, 0, 1, 1, 1, 1}}; 
102   
103   
104      private static byte unselected[][] = { 
105          {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
106          {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
107          {1, 1, 1, 0, 0, 0, 0, 1, 1, 1}, 
108          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
109          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
110          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
111          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
112          {1, 1, 1, 0, 0, 0, 0, 1, 1, 1}, 
113          {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
114          {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}; 
115  }