/Users/lyon/j4p/src/j3d/AlphaApp.java
|
1 package j3d;
2
3 /*
4 * @(#)AlphaApp.java 1.1 00/09/22 14:37
5 *
6 * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
7 *
8 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
9 * modify and redistribute this software in source and binary code form,
10 * provided that i) this copyright notice and license appear on all copies of
11 * the software; and ii) Licensee does not utilize the software in a manner
12 * which is disparaging to Sun.
13 *
14 * This software is provided "AS IS," without a warranty of any kind. ALL
15 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
16 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
17 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
18 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
19 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
20 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
21 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
22 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
23 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGES.
25 *
26 * This software is not designed or intended for use in on-line control of
27 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
28 * the design, construction, operation or maintenance of any nuclear
29 * facility. Licensee represents and warrants that it will not use or
30 * redistribute the Software for such purposes.
31 */
32
33 import com.sun.j3d.utils.applet.MainFrame;
34 import com.sun.j3d.utils.universe.SimpleUniverse;
35
36 import javax.media.j3d.*;
37 import javax.vecmath.Point3f;
38 import javax.vecmath.Vector3f;
39 import java.applet.Applet;
40 import java.awt.*;
41
42
43 // AlphaApp renders an
44
45 public class AlphaApp extends Applet {
46
47 Shape3D createCar(float xScale, float yScale) {
48 Shape3D car = new Shape3D();
49
50 QuadArray carGeom = new QuadArray(16, GeometryArray.COORDINATES);
51
52 carGeom.setCoordinate(0, new Point3f(xScale * -0.25f, yScale * 0.22f, 0.0f));
53 carGeom.setCoordinate(1, new Point3f(xScale * 0.20f, yScale * 0.22f, 0.0f));
54 carGeom.setCoordinate(2, new Point3f(xScale * 0.10f, yScale * 0.35f, 0.0f));
55 carGeom.setCoordinate(3, new Point3f(xScale * -0.20f, yScale * 0.35f, 0.0f));
56 carGeom.setCoordinate(4, new Point3f(xScale * -0.50f, yScale * 0.10f, 0.0f));
57 carGeom.setCoordinate(5, new Point3f(xScale * 0.50f, yScale * 0.10f, 0.0f));
58 carGeom.setCoordinate(6, new Point3f(xScale * 0.45f, yScale * 0.20f, 0.0f));
59 carGeom.setCoordinate(7, new Point3f(xScale * -0.48f, yScale * 0.20f, 0.0f));
60 carGeom.setCoordinate(8, new Point3f(xScale * -0.26f, yScale * 0.00f, 0.0f));
61 carGeom.setCoordinate(9, new Point3f(xScale * -0.18f, yScale * 0.00f, 0.0f));
62 carGeom.setCoordinate(10, new Point3f(xScale * -0.16f, yScale * 0.12f, 0.0f));
63 carGeom.setCoordinate(11, new Point3f(xScale * -0.28f, yScale * 0.12f, 0.0f));
64 carGeom.setCoordinate(12, new Point3f(xScale * 0.25f, yScale * 0.00f, 0.0f));
65 carGeom.setCoordinate(13, new Point3f(xScale * 0.33f, yScale * 0.00f, 0.0f));
66 carGeom.setCoordinate(14, new Point3f(xScale * 0.35f, yScale * 0.12f, 0.0f));
67 carGeom.setCoordinate(15, new Point3f(xScale * 0.23f, yScale * 0.12f, 0.0f));
68
69 car.setGeometry(carGeom);
70
71 ColoringAttributes colorAttrib =
72 new ColoringAttributes(0.0f, 0.0f, 1.0f, ColoringAttributes.NICEST);
73 Appearance carAppear = new Appearance();
74 carAppear.setColoringAttributes(colorAttrib);
75 car.setAppearance(carAppear);
76
77 return car;
78 }
79
80 public BranchGroup createSceneGraph() {
81 // Create the root of the branch graph
82 BranchGroup objRoot = new BranchGroup();
83
84 // create target TransformGroup with Capabilities
85 TransformGroup objMove1 = new TransformGroup();
86 objMove1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
87
88 Transform3D t3d = new Transform3D();
89 t3d.setTranslation(new Vector3f(0.0f, 0.8f, 0.0f));
90 TransformGroup objPos1 = new TransformGroup(t3d);
91
92 // create Alpha that continuously cycles with a period of 1 sec
93 Alpha alpha1 = new Alpha();
94
95 alpha1.setIncreasingAlphaDuration(2000);
96 alpha1.setAlphaAtOneDuration(1000);
97 alpha1.setDecreasingAlphaDuration(2000);
98 alpha1.setAlphaAtZeroDuration(1000);
99
100 // create position interpolator
101 PositionInterpolator posInt1 = new PositionInterpolator(alpha1, objMove1);
102 posInt1.setSchedulingBounds(new BoundingSphere());
103
104 posInt1.setStartPosition(-1.0f);
105
106 objRoot.addChild(objPos1);
107 objPos1.addChild(objMove1);
108 objMove1.addChild(createCar(0.4f, 0.4f));
109 objRoot.addChild(posInt1);
110
111 // create target TransformGroup with Capabilities
112 TransformGroup objMove2 = new TransformGroup();
113 objMove2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
114
115 t3d.setTranslation(new Vector3f(0.0f, 0.3f, 0.0f));
116 TransformGroup objPos2 = new TransformGroup(t3d);
117
118 Alpha alpha2 = new Alpha();
119
120 alpha2.setIncreasingAlphaDuration(2000);
121 alpha2.setAlphaAtOneDuration(1000);
122 alpha2.setIncreasingAlphaRampDuration(500);
123
124 // create position interpolator
125 PositionInterpolator posInt2 = new PositionInterpolator(alpha2, objMove2);
126 posInt2.setSchedulingBounds(new BoundingSphere());
127
128 posInt2.setStartPosition(-1.0f);
129
130 objRoot.addChild(objPos2);
131 objPos2.addChild(objMove2);
132 objMove2.addChild(createCar(0.4f, 0.4f));
133 objRoot.addChild(posInt2);
134
135 // create target TransformGroup with Capabilities
136 TransformGroup objMove3 = new TransformGroup();
137 objMove3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
138
139 t3d.setTranslation(new Vector3f(0.0f, -0.3f, 0.0f));
140 TransformGroup objPos3 = new TransformGroup(t3d);
141
142 Alpha alpha3 = new Alpha();
143
144 alpha3.setIncreasingAlphaDuration(2000);
145 alpha3.setAlphaAtOneDuration(1000);
146 alpha3.setIncreasingAlphaRampDuration(1000);
147
148 // create position interpolator
149 PositionInterpolator posInt3 = new PositionInterpolator(alpha3, objMove3);
150 posInt3.setSchedulingBounds(new BoundingSphere());
151
152 posInt3.setStartPosition(-1.0f);
153
154 objRoot.addChild(objPos3);
155 objPos3.addChild(objMove3);
156 objMove3.addChild(createCar(0.4f, 0.4f));
157 objRoot.addChild(posInt3);
158
159 Background background = new Background();
160 background.setColor(1.0f, 1.0f, 1.0f);
161 background.setApplicationBounds(new BoundingSphere());
162 objRoot.addChild(background);
163
164 // Let Java 3D perform optimizations on this scene graph.
165 objRoot.compile();
166
167 return objRoot;
168 } // end of CreateSceneGraph method of alpha1App
169
170 // Create a simple scene and attach it to the virtual universe
171
172 public AlphaApp() {
173 setLayout(new BorderLayout());
174 GraphicsConfiguration config =
175 SimpleUniverse.getPreferredConfiguration();
176
177 Canvas3D canvas3D = new Canvas3D(config);
178 add("Center", canvas3D);
179
180 BranchGroup scene = createSceneGraph();
181
182 // SimpleUniverse is a Convenience Utility class
183 SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
184
185 // This will move the ViewPlatform back a bit so the
186 // objects in the scene can be viewed.
187 simpleU.getViewingPlatform().setNominalViewingTransform();
188
189 simpleU.addBranchGraph(scene);
190 } // end of AlphaApp (constructor)
191
192 // The following allows this to be run as an application
193 // as well as an applet
194
195 public static void main(String[] args) {
196 System.out.print("AlphaApp.java \n- a demonstration of using alpha to control ");
197 System.out.println("Interpolators to provide animation in a Java 3D scene.");
198 System.out.println("This is a simple example progam from The Java 3D API Tutorial.");
199 System.out.println("The Java 3D Tutorial is available on the web at:");
200 System.out.println("http://java.sun.com/products/java-media/3D/collateral");
201 Frame frame = new MainFrame(new AlphaApp(), 256, 256);
202 } // end of main (method of AlphaApp)
203
204 } // end of class AlphaApp
205