/Users/lyon/j4p/src/graphics/ddd/MainFrame.java

1    package graphics.ddd; 
2     
3    import gui.mouse.MouseRotationController; 
4    import gui.windowUtils.WindowController; 
5     
6    import java.awt.*; 
7    import java.awt.event.KeyEvent; 
8    import java.awt.event.KeyListener; 
9     
10   import graphics.NumImage; 
11    
12    
13    
14   public class MainFrame extends Frame 
15           implements KeyListener { 
16    
17       private RepaintThread repaintThread = 
18               new RepaintThread(this, 20); 
19    
20    
21       private boolean initialized = false; 
22    
23       private Applet3d demo 
24               = new Applet3d(getSize().width, getSize().height); 
25    
26       private MouseRotationController mc 
27               = new MouseRotationController(demo); 
28       private int mode; 
29       /** 
30        * Convert radial images into a solid 
31        * used for 3D image scanning via diffraction. 
32        */ 
33       public void addScan(float radius[][]) { 
34           int scancolor = demo.getIntColor(60, 255, 20); 
35           demo.addObject(mode, scancolor); 
36           int obj = demo.objects; 
37           float twoPi = 2f * (float) Math.PI; 
38           int numberOfImages = radius[0].length; 
39           int h = radius.length; 
40           float deltaTheta = (twoPi / (numberOfImages - 1f)); 
41           double deltaY = 3d / h; 
42           for (int imageNumber = 0; imageNumber < numberOfImages; imageNumber++) 
43               processEachImage(h, radius, imageNumber, deltaTheta, deltaY, obj); 
44           demo.generateScanObject(obj, h, numberOfImages - 1); 
45           demo.shiftObject(obj, (float) -1.2, (float) -0.5, (float) 1.3); 
46           demo.scaleObject(obj, (float) 0.5); 
47           demo.object[obj].texture = obj; 
48       } 
49    
50       private void processEachImage(int h, float[][] radius, 
51                                     int imageNumber, 
52                                     float deltaTheta, 
53                                     double deltaY, int obj) { 
54           float x1; 
55           float y1; 
56           float z1; 
57           for (int y = 0; y < h; y++) { 
58               if (radius[y][imageNumber] > 0) { 
59                   x1 = (float) (radius[y][imageNumber] * Math.sin((imageNumber * deltaTheta))); 
60                   y1 = -1.5f + (float) (y * deltaY); 
61                   z1 = (float) (radius[y][imageNumber] * Math.cos((imageNumber * deltaTheta))); 
62               } else { 
63                   x1 = 0; 
64                   y1 = -1.5f + (float) (y * deltaY); 
65                   z1 = 0; 
66               } 
67               demo.addNode(obj, x1, y1, z1); 
68           } 
69       } 
70    
71       public void initialize(Image i, float r[][]) { 
72    
73           demo = new Applet3d(getSize().width, getSize().height); 
74    
75           demo.addTexture(i); 
76           addScan(r); 
77           addLights(); 
78           demo.reflectivity = 200; 
79           demo.setStatic(); 
80           initialized = true; 
81           addListeners(); 
82       } 
83    
84       public static void image3D(Image img, short i[][]) { 
85           ip.raul.SnowManFrame.image3D(img, i); 
86       } 
87    
88       public static void image3DBad(Image img, short s[][]) { 
89           MainFrame f = new MainFrame(); 
90           f.setSize(200, 200); 
91           f.setVisible(true); 
92           f.initialize(img, s); 
93           f.repaintThread().start(); 
94           for (int i = 1; i <= f.demo.objects; i++) { 
95               f.demo.object[i].mode = 6; 
96           } 
97    
98       } 
99    
100   
101      public static void image3D(Image img, float r[][]) { 
102          MainFrame f = new MainFrame(); 
103          f.setSize(400, 400); 
104          f.setVisible(true); 
105          f.initialize(img, r); 
106          f.repaintThread().start(); 
107          for (int i = 1; i <= f.demo.objects; i++) { 
108              f.demo.object[i].mode = 6; 
109          } 
110      } 
111   
112      public static MainFrame stepImage(Image img, short s[][]) { 
113          MainFrame f = new MainFrame(); 
114          f.setSize(200, 200); 
115          f.setVisible(true); 
116          f.initialize(img, s); 
117          for (int i = 1; i <= f.demo.objects; i++) { 
118              f.demo.object[i].mode = 6; 
119          } 
120          return f; 
121      } 
122   
123   
124      public void keyPressed(KeyEvent e) { 
125      }; 
126      public void keyReleased(KeyEvent e) { 
127      }; 
128   
129      public void addListeners() { 
130          new WindowController(this); 
131          addMouseListener(mc); 
132          addMouseMotionListener(mc); 
133          addKeyListener(this); 
134      } 
135   
136      public void initialize() { 
137          initObjects(); 
138          demo.addTexture(NumImage.getImage()); 
139          addImageField(); 
140          lightsAction(); 
141      } 
142   
143      public void initialize(Image i, short s[][]) { 
144          initObjects(); 
145          demo.addTexture(i); 
146          addImageField(s); 
147          lightsAction(); 
148      } 
149   
150      private void lightsAction() { 
151          addLights(); 
152          demo.reflectivity = 200; 
153          demo.setStatic(); 
154          demo.rotateWorld((float) 90, (float) 0, 0); 
155          initialized = true; 
156      } 
157   
158      private void initObjects() { 
159   
160          addListeners(); 
161      } 
162   
163      public void paint(Graphics gx) { 
164          gx.setColor(Color.black); 
165          gx.fillRect(0, 0, getSize().width, getSize().height); 
166          gx.setColor(new Color(0, 255, 0)); 
167          gx.drawLine(10, 26, 160, 26); 
168      } 
169   
170   
171      public void update(Graphics g) { 
172          if (initialized) { 
173              if (mc.isAutorot()) 
174                  demo.rotateWorld(3, -5, 2); 
175              demo.rotateObject(1, 0, 0, 5); 
176              g.drawImage(demo.renderScene(), 0, 0, this); 
177          } else { 
178              paint(g); 
179              initialize(); 
180          } 
181      } 
182   
183      void addImageField() { 
184          int fieldres = 20; 
185          int fieldcolor = demo.getIntColor(255, 96, 0); 
186          float map[][] = new float[fieldres][fieldres]; 
187          for (int i = 2; i < fieldres; i++) { 
188              for (int j = 2; j < fieldres; j++) { 
189                  int x = NumImage.gray.length * i / fieldres; 
190                  int y = NumImage.gray[0].length * j / fieldres; 
191                  map[i][j] = (float) (NumImage.gray[x][y] / 255.0); 
192              } 
193          } 
194          demo.generateField(map, fieldres, fieldres, mode, fieldcolor); 
195          demo.object[1].texture = 1; 
196          demo.rotateObject(1, 0, (float) 180, (float) 180); 
197          demo.shiftObject(1, 0, (float) 0.5, 0); 
198      } 
199   
200      void addImageField(short s[][]) { 
201          int fieldres = 40; 
202          int fieldcolor = demo.getIntColor(255, 96, 0); 
203          float map[][] = new float[fieldres][fieldres]; 
204          for (int i = 1; i < fieldres - 1; i++) { 
205              for (int j = 1; j < fieldres - 1; j++) { 
206                  int x = s.length * i / fieldres; 
207                  int y = s[0].length * j / fieldres; 
208                  map[i][j] = (float) (s[x][y] / 255.0); 
209              } 
210          } 
211          demo.generateField(map, fieldres, fieldres, mode, fieldcolor); 
212          demo.object[1].texture = 1; 
213          demo.rotateObject(1, 0, (float) 180, (float) 180); 
214          demo.shiftObject(1, 0, (float) 0.5, 0); 
215      } 
216   
217      void addField() 
218              // Adds a 3d field to the scene 
219      { 
220          int fieldres = 10; 
221          int fieldcolor = demo.getIntColor(255, 96, 0); 
222          float map[][] = new float[fieldres][fieldres]; 
223          for (int i = 0; i < fieldres; i++) { 
224              for (int j = 0; j < fieldres; j++) { 
225                  float x = (float) i / (float) fieldres * 2 - 1; 
226                  float y = (float) j / (float) fieldres * 2 - 1; 
227                  //map[i][j]=x*x*x*y-y*y*y*x; 
228                  //map[i][j]=(float)(Math.cos(x*y*8)/10-Math.tan(x*y/2)/2); 
229                  map[i][j] = x * x + y * y - (float) 0.5; 
230                  //map[i][j]=(float)(Math.sin(x*y*8)/6-0.2); 
231              } 
232          } 
233          demo.generateField(map, fieldres, fieldres, mode, fieldcolor); 
234          demo.object[1].texture = 1; 
235          demo.rotateObject(1, 0, (float) 180, (float) 180); 
236          demo.shiftObject(1, 0, (float) 0.5, 0); 
237      } 
238   
239   
240      void addLights() { 
241          demo.ambient = 48; 
242          demo.setPhong(64); 
243          demo.addLight(new Vector3d((float) 0, (float) 0, (float) -1), 1, 164); 
244          demo.addLight(new Vector3d((float) 2, (float) -4, (float) -1), 1, 144); 
245      } 
246   
247      public void keyTyped(KeyEvent e) { 
248          mode = (mode + 1) % 8; 
249          for (int i = 1; i <= demo.objects; i++) 
250              demo.object[i].mode = mode; 
251      } 
252   
253      public RepaintThread repaintThread() { 
254          return repaintThread; 
255      } 
256   
257   
258  } 
259