/Users/lyon/j4p/src/j3d/cr325/Hello/VariableSpeedCube.java
|
1 /**
2 * Created by IntelliJ IDEA.
3 * User: tchuong
4 * Date: Feb 18, 2004
5 * Time: 2:12:34 PM
6 * To change this template use Options | File Templates.
7 */
8 package j3d.cr325.Hello;
9
10 import com.sun.j3d.utils.geometry.ColorCube;
11 import com.sun.j3d.utils.universe.SimpleUniverse;
12 import com.sun.j3d.utils.universe.ViewingPlatform;
13 import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
14 import gui.ClosableJFrame;
15 import gui.run.RunButton;
16 import j3d.Utils;
17 import j3d.cr325.KeyBehavior;
18 import j3d.cr325.LevitownDoomKeyboard;
19
20 import javax.media.j3d.*;
21 import javax.swing.*;
22 import javax.vecmath.Point3d;
23 import java.awt.*;
24 import java.awt.event.KeyEvent;
25 import java.util.Enumeration;
26
27 public class VariableSpeedCube {
28 public static void main(String[] args) {
29 ClosableJFrame cubeFrame = new ClosableJFrame("3d Cube");
30 Container c = cubeFrame.getContentPane();
31 c.setLayout(new BorderLayout());
32 CubePanel cp = new CubePanel();
33 ButtonControlPanel bcp = new ButtonControlPanel(cp);
34 c.add(cp, BorderLayout.CENTER);
35 c.add(bcp, BorderLayout.SOUTH);
36 cubeFrame.setSize(600, 600);
37 cubeFrame.show();
38 }
39 }//Hello3dFrame
40
41 class CubePanel extends JPanel {
42 public long alphaTime = 4000;
43 public float cubeSize = 0.4f;
44
45 public CubePanel() {
46 setLayout(new BorderLayout());
47
48 Canvas3D canvas3D = Utils.getCanvas3D(this);
49 add("Center", canvas3D);
50
51 BranchGroup bg = createSceneGraph();
52 SimpleUniverse su = new SimpleUniverse(canvas3D);
53 ViewingPlatform vp = su.getViewingPlatform();
54 vp.setNominalViewingTransform();
55 TransformGroup tg = vp.getViewPlatformTransform();
56 //PlatformGeometry pg = new PlatformGeometry();
57 //pg.addChild(Shapes.getHousesBranchGroup());
58 vp.setPlatformGeometry(LevitownDoomKeyboard.buildGunPlatform());
59 TransformGroup steerTg = vp.getViewPlatformTransform();
60
61 Transform3D t3d = new Transform3D();
62 steerTg.getTransform(t3d);
63 System.out.println(t3d);
64
65 KeyNavigatorBehavior keyNavBeh = new GunFiredKeyboardNavigator(tg);
66 //KeyBehavior keyNavBeh = new KeyBehavior(tg);
67 keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(), 10000.0));
68 bg.addChild(keyNavBeh);
69 su.addBranchGraph(bg);
70 }
71
72 public BranchGroup createSceneGraph() {
73 BranchGroup branchGroupRoot
74 = Utils.getBranchGroup();
75 Transform3D rotate
76 = new Transform3D();
77 Transform3D tempRotate
78 = new Transform3D();
79 rotate.rotX(Math.PI / 4.0d);
80 tempRotate.rotY(Math.PI / 5.0d);
81 rotate.mul(tempRotate);
82
83 TransformGroup rotateTransformGroup
84 = Utils.getTransformGroup(rotate);
85 TransformGroup spinTransformGroup
86 = Utils.getTransformGroup();
87
88 branchGroupRoot.addChild(rotateTransformGroup);
89 rotateTransformGroup.addChild(spinTransformGroup);
90
91 // Create a simple shape leaf node, add it to the scene graph.
92 // ColorCube is a Convenience Utility
93 cc = getColorCube();
94 spinTransformGroup.addChild(cc);
95
96 // Create a new Behavior object that will perform the desired
97 // operation on the specified transform object and add it into
98 // the scene graph.
99
100 ri = getRotationInterpolator(spinTransformGroup);
101 si = getScaleInterpolator(spinTransformGroup);
102 BoundingSphere bounds = new BoundingSphere();
103 ri.setSchedulingBounds(bounds);
104 si.setSchedulingBounds(bounds);
105 spinTransformGroup.addChild(ri);
106 spinTransformGroup.addChild(si);
107 return branchGroupRoot;
108 }
109
110 ColorCube cc = new ColorCube();
111
112 private ColorCube getColorCube() {
113 return new ColorCube(getCubeScale());
114 }
115
116 public void setCubeSize(float s) {
117 si.setMinimumScale(s);
118 si.setMaximumScale(s);
119 cubeSize = s;
120 }
121
122 public float getCubeScale() {
123 return cubeSize;
124 }
125
126 RotationInterpolator ri = null;
127 ScaleInterpolator si = null;
128
129
130 private ScaleInterpolator getScaleInterpolator(TransformGroup tg) {
131 return new ScaleInterpolator(new Alpha(-1, getAlphaTime()),
132 tg,
133 new Transform3D(),
134 0.0f,
135 1f);
136 }
137
138 private RotationInterpolator getRotationInterpolator(TransformGroup objSpin) {
139 return new RotationInterpolator(new Alpha(-1, getAlphaTime()),
140 objSpin,
141 new Transform3D(),
142 0.0f,
143 (float) Math.PI * 2.0f);
144 }
145
146 public void setAlphaTime(long num2) {
147 alphaTime = num2;
148 ri.setAlpha(new Alpha(-1, getAlphaTime()));
149 }//setAlphaTime(int)
150
151 public long getAlphaTime() {
152 return alphaTime;
153 }
154
155 private static class GunFiredKeyboardNavigator extends KeyNavigatorBehavior {
156 public GunFiredKeyboardNavigator(TransformGroup tg) {
157 super(tg);
158 }
159
160 public void processStimulus(Enumeration enumeration) {
161 System.out.println(enumeration);
162 super.processStimulus(enumeration);
163 }
164 }
165
166 }//class CubePanel
167
168
169 class ButtonControlPanel extends JPanel {
170 private CubePanel cube;
171 private JLabel alphaLabel = new JLabel("a = " + 4000);
172 private JLabel scaleLabel = new JLabel("scale = " + .4);
173
174 //--------------------------------------------
175 // Sets up the cube control panel
176 //--------------------------------------------
177 public ButtonControlPanel(CubePanel cubePanel) {
178 setLayout(new FlowLayout());
179 cube = cubePanel;
180 add(alphaLabel);
181 add(scaleLabel);
182
183 add(new RunButton("[m") {
184 public void run() {
185 cube.setAlphaTime(cube.getAlphaTime() - 500);
186 alphaLabel.setText("a = " + cube.getAlphaTime());
187 }//run
188 });
189
190 add(new RunButton("[p") {
191 public void run() {
192 cube.setAlphaTime(cube.getAlphaTime() + 500);
193 alphaLabel.setText("a = " + cube.getAlphaTime());
194 }
195 });
196
197 add(new RunButton("[s") {
198 public void run() {
199 cube.setAlphaTime(0);
200 alphaLabel.setText("a = " + cube.getAlphaTime());
201 }
202 });
203
204 add(new RunButton("sm[aller cube") {
205 public void run() {
206 cube.setCubeSize(cube.getCubeScale() - .1f);
207 scaleLabel.setText("scale = " + cube.getCubeScale());
208 }
209 });
210
211 add(new RunButton("bi[gger cube") {
212 public void run() {
213 cube.setCubeSize(cube.getCubeScale() + .1f);
214 scaleLabel.setText("scale = " + cube.getCubeScale());
215 }
216 });
217
218 } //end constructor ButtonControlPanel
219
220 }//class ButtonControlPanel
221