/Users/lyon/j4p/src/graphics/raytracers/tracer/Test.java

1    package graphics.raytracers.tracer; 
2     
3    import graphics.raytracers.tracer.geometry.Vector3d; 
4    import graphics.raytracers.tracer.primatives.Primitive; 
5    import graphics.raytracers.tracer.primatives.Sphere; 
6     
7    import java.applet.Applet; 
8    import java.awt.*; 
9    import java.awt.image.ImageObserver; 
10   import java.io.InputStream; 
11   import java.net.URL; 
12   import java.net.URLConnection; 
13    
14   public class Test extends 
15           Applet implements Runnable, ImageObserver { 
16       Tracer t; 
17       view v; 
18       int painted; 
19       Image img; 
20       boolean loaded; 
21       Thread motor; 
22       Dimension d; 
23       Graphics my_g; 
24    
25       public void init() { 
26           Primitive p; 
27           double diff, spec, shine; 
28           int width, height; 
29           URL hp; 
30           URLConnection hpcon; 
31           InputStream input; 
32           byte scenebytes[]; 
33           String scene; 
34           String sceneURL; 
35           int len; 
36           Canvas s; 
37    
38           scene = ""; 
39           sceneURL = getParameter("scene"); 
40           System.out.println("Scene: " + scene); 
41    
42           my_g = getGraphics(); 
43           loaded = false; 
44    
45           diff = 0.7; 
46           spec = 0.3; 
47           shine = 14.0; 
48    
49           v = new view(); 
50           v.setFrom(new Vector3d(10, 10, -40)); 
51           v.setAt(new Vector3d(10, 10, -15)); 
52           v.setUp(new Vector3d(0, 1, 0)); 
53           v.setAngle(35.0 * 3.14159265 / 180.0); 
54           v.setAspect(1.0); /* 4.0/3.0; */ 
55           v.setDist(1.0); 
56    
57           width = getSize().width; 
58           height = getSize().height; 
59    
60           t = new Tracer(width, height, v); 
61    
62           p = new Sphere(new Vector3d(0, 0, 0), 10); 
63           p.setColor(1, 0, 0); 
64           p.surf.shine = shine; 
65           p.surf.kd = diff; 
66           p.surf.ks = spec; 
67           t.newPrim(p); 
68    
69           p = new Sphere(new Vector3d(0, 20, 0), 10); 
70           p.setColor(0, 1, 0); 
71           p.surf.shine = shine; 
72           p.surf.kd = diff; 
73           p.surf.ks = spec; 
74           t.newPrim(p); 
75    
76           p = new Sphere(new Vector3d(20, 0, 0), 10); 
77           p.setColor(0, 0, 1); 
78           p.surf.shine = shine; 
79           p.surf.kd = diff; 
80           p.surf.ks = spec; 
81           t.newPrim(p); 
82    
83           t.newLight(new Vector3d(100, 100, -20), 1.0); 
84           t.newLight(new Vector3d(-100, 100, -20), 1.0); 
85           t.newLight(new Vector3d(100, -100, -20), 1.0); 
86    
87           img = createImage(t); 
88       } 
89    
90       public boolean imageUpdate(Image img, int info, 
91                                  int x, int y, int width, int height) { 
92           if ((info & ALLBITS) != 1) { 
93               my_g.drawImage(img, 0, 0, this); 
94               return true; 
95           } else { 
96               return false; 
97           } 
98       } 
99    
100      public void paint(Graphics g) { 
101          d = this.getSize(); 
102          loaded = g.drawImage(img, 0, 0, this); 
103      } 
104   
105      public void update(Graphics g) { 
106          paint(g); 
107      } 
108   
109      public void start() { 
110          motor = new Thread(this); 
111          motor.start(); 
112      } 
113   
114      public void stop() { 
115          //motor.stop();   // this has been deprecated, DL 
116      } 
117   
118      public void run() { 
119          motor.setPriority(Thread.MIN_PRIORITY); 
120          while (!loaded) { 
121              repaint(); 
122              try { 
123                  motor.sleep(500); 
124              } catch (InterruptedException e) { 
125              } 
126          } 
127      } 
128  } 
129