/Users/lyon/j4p/src/ip/hak/Point4.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 Point4 extends Component { 
9        Point center; 
10       Image image, selectedImage, unselectedImage; 
11       Dimension imageDim; 
12       boolean inv; 
13    
14       public Point4(Point cen) { 
15           center = cen; 
16           unselectedImage = byte2Image(unselected); 
17           selectedImage = byte2Image(selected); 
18           image = unselectedImage; 
19           imageDim = new Dimension(unselected.length, unselected[0].length); 
20           inv = false; 
21       } 
22    
23       public void setCenter(Point p) { 
24           center = p; 
25       } 
26    
27       public Point getCenter() { 
28           return center; 
29       } 
30    
31       public boolean contain(Point p) { 
32           int hx = imageDim.width / 2; 
33           int hy = imageDim.height / 2; 
34           int minx = center.x - hx; 
35           int maxx = center.x + hx; 
36           int miny = center.y - hy; 
37           int maxy = center.y + hy; 
38    
39           System.out.println("X=" + p.x + " max=" + maxx + " min=" + minx); 
40           System.out.println("Y=" + p.y + " max=" + maxy + " min=" + miny); 
41           return (p.x >= minx && p.x <= maxx && p.y >= miny && p.y <= maxy && image != null); 
42       } 
43    
44       public boolean contains(int x, int y) { 
45    
46           return (x >= 0 && x < imageDim.width && y >= 0 && y < imageDim.height && image != null); 
47       } 
48    
49       public void reshape(Point cp, Dimension dim) { 
50           setBounds( 
51                   cp.x - dim.width / 2, 
52                   cp.y - dim.height / 2, 
53                   dim.width, 
54                   dim.height); 
55       } 
56    
57       public void reshape() { 
58           setBounds( 
59                   center.x - imageDim.width / 2, 
60                   center.y - imageDim.height / 2, 
61                   imageDim.width, imageDim.height); 
62       } 
63    
64       public Dimension getPreferredSize() { 
65           return imageDim; 
66       } 
67    
68       public void update(Graphics g) { 
69           paint(g); 
70       } 
71    
72       public void paint(Graphics g) { 
73           if (image != null) { 
74               //int xg=imageDim.width/2; 
75               //int yg=imageDim.height/2; 
76               //g.drawImage(image,center.x-xg,center.y-yg,imageDim.width, imageDim.height,this); 
77               g.drawImage(image, 0, 0, imageDim.width, imageDim.height, this); 
78               //g.drawString("X : "+center.x+" Y : "+center.y,center.x+10,center.y); 
79           } 
80       } 
81    
82       private static Image byte2Image(byte r[][]) { 
83           int w = r.length; 
84           int h = r[0].length; 
85           int v = 0; 
86           Toolkit tk = Toolkit.getDefaultToolkit(); 
87           int pels[] = new int[w * h]; 
88           for (int x = 0; x < w; x++) 
89               for (int y = 0; y < h; y++) { 
90                   if (r[x][y] == 1) 
91                       v = 255; 
92                   else 
93                       v = 0; 
94                   pels[y + x * h] = 0xff000000 | (v << 16) | (v << 8) | v; 
95               } 
96           Image i = tk.createImage(new MemoryImageSource(w, h, ColorModel.getRGBdefault(), pels, 0, w)); 
97           return i; 
98       } 
99    
100      public void invert() { 
101          if (!inv) 
102              image = selectedImage; 
103          else 
104              image = unselectedImage; 
105          inv = !inv; 
106          repaint(); 
107      } 
108   
109      private static byte selected[][] = { 
110          {1, 1, 1, 0, 0, 0, 0, 1, 1, 1}, 
111          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
112          {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
113          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
114          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
115          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
116          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
117          {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
118          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
119          {1, 1, 1, 1, 0, 0, 1, 1, 1, 1}}; 
120   
121   
122      private static byte unselected[][] = { 
123          {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
124          {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
125          {1, 1, 1, 0, 0, 0, 0, 1, 1, 1}, 
126          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
127          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
128          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
129          {1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, 
130          {1, 1, 1, 0, 0, 0, 0, 1, 1, 1}, 
131          {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
132          {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}; 
133   
134   
135  }