/Users/lyon/j4p/src/ip/gui/frames/DrawFrame.java

1    package ip.gui.frames; 
2     
3    import java.awt.*; 
4    import java.awt.event.FocusEvent; 
5    import java.awt.event.FocusListener; 
6     
7    public class DrawFrame extends WaveletFrame 
8            implements FocusListener { 
9        int xsize,ysize,xcenter,ycenter; 
10       int delay = 10; 
11       int numberOfPoints = 1000; 
12       Graphics backGC; 
13       Image backBuffer; 
14       int[] pointX,pointY,pointZ; 
15       Color[] grayScale; 
16    
17       boolean inFocus = true; 
18    
19       public void focusGained(FocusEvent e) { 
20           inFocus = true; 
21           repaint(2000, 0, 0, 256, 256); 
22       } 
23    
24       public void focusLost(FocusEvent e) { 
25           inFocus = false; 
26       } 
27    
28       public DrawFrame(String title) { 
29           super(title); 
30       } 
31    
32       public static void main(String args[]) { 
33           DrawFrame f = new DrawFrame("DrawFrame"); 
34           f.setSize(256, 256); 
35           f.show(); 
36           f.setUpFrame(); 
37           f.setPoints(); 
38           f.addFocusListener(f); 
39           f.repaint(); 
40       } 
41    
42       public static void drawPoints( 
43               float x[], 
44               float y[], 
45               float z[]) { 
46           DrawFrame f = new DrawFrame("DrawFrame"); 
47           f.setSize(256, 256); 
48           f.show(); 
49           f.setUpFrame(); 
50           f.setPoints(x, y, z); 
51           f.addFocusListener(f); 
52           f.repaint(); 
53       } 
54    
55    
56       public void setUpFrame() { 
57           //Get screen dimensions 
58           xsize = getSize().width; 
59           ysize = getSize().height; 
60           xcenter = xsize / 2; 
61           ycenter = ysize / 2; 
62    
63           //Init double-buffering 
64           backBuffer = createImage(xsize, ysize); 
65           backGC = backBuffer.getGraphics(); 
66    
67           //Create grayscale tones 
68           grayScale = new Color[256]; 
69           for (int n = 0; n < 256; n++) 
70               grayScale[255 - n] = new Color(n, n, n); 
71       } 
72    
73       public void setPoints() { 
74           int X[] = new int[numberOfPoints]; 
75           int Y[] = new int[numberOfPoints]; 
76           int Z[] = new int[numberOfPoints]; 
77           double theta = 0; 
78           double eps = (1.0 / numberOfPoints) * 2 * Math.PI; 
79           for (int n = 0; n < numberOfPoints; n++) { 
80               X[n] = (int) 
81                       ((Math.sin(theta) * xsize * 2.0) - xsize / 2); 
82               Y[n] = (int) 
83                       ((Math.cos(theta) * ysize * 2.0) - ysize / 2); 
84               Z[n] = (int) 
85                       (Math.random() * 4095); 
86               theta = theta + eps; 
87           } 
88           setPoints(X, Y, Z); 
89       } 
90    
91       public void setPoints(int x[], int y[], int z[]) { 
92           numberOfPoints = x.length; 
93           pointX = x; 
94           pointY = y; 
95           pointZ = z; 
96           int xmax = -1000; 
97           int ymax = -1000; 
98           int zmax = -1000; 
99           int xmin = 1000; 
100          int ymin = 1000; 
101          int zmin = 1000; 
102   
103          for (int i = 0; i < x.length; i++) { 
104              if (x[i] > xmax) xmax = x[i]; 
105              if (y[i] > ymax) ymax = y[i]; 
106              if (z[i] > zmax) zmax = z[i]; 
107   
108              if (x[i] < xmin) xmin = x[i]; 
109              if (y[i] < ymin) ymin = y[i]; 
110              if (z[i] < zmin) zmin = z[i]; 
111          } 
112          System.out.println("x,y,z max =" + xmax + " " + ymax + " " + zmax); 
113          System.out.println("x,y,z min =" + xmin + " " + ymin + " " + zmin); 
114      } 
115   
116      public void setPoints(float x[], float y[], float z[]) { 
117          numberOfPoints = x.length; 
118          pointX = new int[numberOfPoints]; 
119          pointY = new int[numberOfPoints]; 
120          pointZ = new int[numberOfPoints]; 
121          int xmax = -1000; 
122          int ymax = -1000; 
123          int zmax = -1000; 
124          int xmin = 1000; 
125          int ymin = 1000; 
126          int zmin = 1000; 
127   
128          for (int i = 0; i < x.length; i++) { 
129              pointX[i] = (int) (x[i] * 384 + (1 - x[i]) * (-640)); 
130              pointY[i] = (int) (y[i] * 384 + (1 - y[i]) * (-640)); 
131              pointZ[i] = (int) (z[i] * 4095 + (1 - z[i]) * (0)); 
132          } 
133          setPoints(pointX, pointY, pointZ); 
134      } 
135   
136      public void paint(Graphics g) { 
137          //Clear screen 
138          backGC.setColor(Color.black); 
139          backGC.fillRect(0, 0, xsize, ysize); 
140   
141          for (int n = 0; n < numberOfPoints; n++) { 
142              //Move stars and clip against viewplane 
143              //The back clipping plane and the 
144              // view plane are hard coded 
145              //for optimizing reasons. 
146              // Viewplane=512, backplane=4095 
147              pointZ[n] -= 50; 
148              if (pointZ[n] < 512) pointZ[n] = 4095; 
149              int z = pointZ[n]; 
150   
151              //Apply perspective projection 
152              int x = (pointX[n] << 9) / z + xcenter; 
153              int y = (pointY[n] << 9) / z + ycenter; 
154   
155              //Apply dept shading 
156              backGC.setColor(grayScale[z >> 4]); 
157   
158              //Draw star 
159              backGC.drawLine(x, y, x, y); 
160          } 
161   
162          //Draw buffer to screen 
163          g.drawImage(backBuffer, 0, 0, this); 
164          repaint(2000, 0, 0, 256, 256); 
165   
166      } 
167   
168   
169      public void update(Graphics g) { 
170          if (inFocus) 
171              paint(g); 
172      } 
173  }