/Users/lyon/j4p/src/j3d/cr325/MandleBox.java
|
1 package j3d.cr325;
2
3 import com.sun.j3d.utils.applet.MainFrame;
4 import com.sun.j3d.utils.geometry.Box;
5 import com.sun.j3d.utils.universe.SimpleUniverse;
6 import j3d.Utils;
7
8 import javax.media.j3d.*;
9 import java.applet.Applet;
10 import java.awt.*;
11 import java.awt.event.KeyEvent;
12 import java.util.Enumeration;
13
14
15 /**
16 * Create a mandlebrot set for a box the user rotates
17 * with a keystroke.
18 */
19 public final class MandleBox extends Applet {
20 /**
21 * a class that lets the user
22 * rotate the object with the keyboard.
23 */
24 public static final class SimpleBehavior extends Behavior {
25
26 private final TransformGroup targetTG;
27 private final Transform3D t3d = new Transform3D();
28 final Transform3D rotz = new Transform3D();
29 private double angle = 0.0;
30
31
32 SimpleBehavior(final TransformGroup targetTG) {
33 this.targetTG = targetTG;
34 }
35
36 public void initialize() {
37
38 this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
39 }
40
41 public void processStimulus(final Enumeration criteria) {
42 angle += 0.1;
43 t3d.rotY(angle);
44 rotz.rotZ(angle * .1);
45 t3d.mul(rotz);
46 targetTG.setTransform(t3d);
47 this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
48 }
49
50 }
51
52 private static Appearance getAppearance() {
53 final Appearance app = new Appearance();
54 app.setTexture(Utils.getMandleTexture(400, 400));
55 return app;
56 }
57
58 private static BranchGroup createSceneGraph() {
59
60 final BranchGroup objRoot = new BranchGroup();
61
62 final TransformGroup objRotate = new TransformGroup();
63 objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
64
65 objRoot.addChild(objRotate);
66 final Box b = new Box(.4f, .4f, .4f,
67 Box.GENERATE_TEXTURE_COORDS, getAppearance());
68 objRotate.addChild(b);
69
70 final SimpleBehavior myRotationBehavior = new SimpleBehavior(objRotate);
71 myRotationBehavior.setSchedulingBounds(new BoundingSphere());
72 objRoot.addChild(myRotationBehavior);
73 objRoot.compile();
74
75 return objRoot;
76 }
77
78
79 /**
80 * create a simple scene and attach it to the
81 * virtual universe.
82 */
83 public MandleBox() {
84 setLayout(new BorderLayout());
85
86 final Canvas3D canvas3D
87 = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
88 add("Center", canvas3D);
89 final SimpleUniverse su = new SimpleUniverse(canvas3D);
90 final BranchGroup scene = createSceneGraph();
91
92 su.getViewingPlatform().setNominalViewingTransform();
93
94 su.addBranchGraph(scene);
95 }
96
97 /**
98 * ignore the args.
99 *
100 * @param args args unused.
101 */
102 public static void main(final String[] args) {
103 new MainFrame(new MandleBox(), 256, 256);
104 } // end of main (method of SimpleBehaviorApp)
105
106 } // end of class SimpleBehaviorApp
107