| 1 | import java.lang.*; |
|---|
| 2 | import java.awt.*; |
|---|
| 3 | import java.applet.Applet; |
|---|
| 4 | import javax.media.j3d.*; |
|---|
| 5 | import javax.vecmath.*; |
|---|
| 6 | import com.sun.j3d.utils.universe.*; |
|---|
| 7 | import com.sun.j3d.utils.geometry.*; |
|---|
| 8 | import com.sun.j3d.utils.applet.MainFrame; |
|---|
| 9 | |
|---|
| 10 | class test extends Applet |
|---|
| 11 | { |
|---|
| 12 | static VirtualUniverse vu; |
|---|
| 13 | |
|---|
| 14 | public test () |
|---|
| 15 | { |
|---|
| 16 | GraphicsConfiguration gc; |
|---|
| 17 | Canvas3D c3d; |
|---|
| 18 | System.out.println("Hello Java3D"); |
|---|
| 19 | gc = this.getGC(); |
|---|
| 20 | System.out.println("gc = " + gc); |
|---|
| 21 | vu = new VirtualUniverse(); |
|---|
| 22 | try |
|---|
| 23 | { |
|---|
| 24 | c3d = new Canvas3D(gc); |
|---|
| 25 | System.out.println("c3d = " + c3d); |
|---|
| 26 | setLayout(new BorderLayout()); |
|---|
| 27 | add("Center", c3d); |
|---|
| 28 | Locale loc = new Locale(vu); |
|---|
| 29 | loc.addBranchGraph(view(c3d)); |
|---|
| 30 | loc.addBranchGraph(scene()); |
|---|
| 31 | } |
|---|
| 32 | catch (NullPointerException e) |
|---|
| 33 | { |
|---|
| 34 | System.out.println("Exception instantiating Canvas3D object"); |
|---|
| 35 | System.out.println("e = " + e.getMessage()); |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | public GraphicsConfiguration getGC () |
|---|
| 40 | { |
|---|
| 41 | GraphicsConfigTemplate3D gct3D = new GraphicsConfigTemplate3D(); |
|---|
| 42 | GraphicsEnvironment ge = GraphicsEnvironment |
|---|
| 43 | .getLocalGraphicsEnvironment(); |
|---|
| 44 | GraphicsDevice gd[] = ge.getScreenDevices(); |
|---|
| 45 | |
|---|
| 46 | System.out.println("gct3D = " + gct3D); |
|---|
| 47 | System.out.println("ge = " + ge); |
|---|
| 48 | System.out.println("gd[0] = " + gd[0]); |
|---|
| 49 | |
|---|
| 50 | return gd[0].getBestConfiguration( gct3D ); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | public BranchGroup scene() |
|---|
| 54 | { |
|---|
| 55 | BranchGroup bg = new BranchGroup(); |
|---|
| 56 | TransformGroup tg = new TransformGroup(); |
|---|
| 57 | Transform3D tf = new Transform3D(); |
|---|
| 58 | Matrix3d m1 = new Matrix3d(); |
|---|
| 59 | Matrix3d m2 = new Matrix3d(); |
|---|
| 60 | m2.rotY(Math.toRadians(45.0)); |
|---|
| 61 | m1.rotX(Math.toRadians(30.0)); |
|---|
| 62 | m1.mul(m2); |
|---|
| 63 | tf.setRotation(m1); |
|---|
| 64 | tg.setTransform(tf); |
|---|
| 65 | tg.addChild(new ColorCube()); |
|---|
| 66 | bg.addChild(tg); |
|---|
| 67 | bg.compile(); |
|---|
| 68 | return bg; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | public BranchGroup view( Canvas3D c ) |
|---|
| 72 | { |
|---|
| 73 | BranchGroup bg = new BranchGroup(); |
|---|
| 74 | TransformGroup tg = new TransformGroup(); |
|---|
| 75 | Transform3D tf = new Transform3D(); |
|---|
| 76 | ViewPlatform vp = new ViewPlatform(); |
|---|
| 77 | View v = new View(); |
|---|
| 78 | PhysicalBody pb = new PhysicalBody(); |
|---|
| 79 | PhysicalEnvironment pe = new PhysicalEnvironment(); |
|---|
| 80 | v.setPhysicalBody(pb); |
|---|
| 81 | v.setPhysicalEnvironment(pe); |
|---|
| 82 | v.addCanvas3D(c); |
|---|
| 83 | v.attachViewPlatform(vp); |
|---|
| 84 | v.setFrontClipDistance(1); |
|---|
| 85 | v.setBackClipDistance(50); |
|---|
| 86 | vp.setViewAttachPolicy(View.RELATIVE_TO_FIELD_OF_VIEW); |
|---|
| 87 | tf.setTranslation(new Vector3d(0.0, 0.0, 5.0)); |
|---|
| 88 | tg.setTransform(tf); |
|---|
| 89 | tg.addChild(vp); |
|---|
| 90 | bg.addChild(tg); |
|---|
| 91 | return bg; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | public static void main ( String[] args ) |
|---|
| 95 | { |
|---|
| 96 | new MainFrame( new test(), 512, 512 ); |
|---|
| 97 | System.out.println("System Properties: " + System.getProperties()); |
|---|
| 98 | System.out.println("Java3D Properties: " + vu.getProperties()); |
|---|
| 99 | try |
|---|
| 100 | { |
|---|
| 101 | Thread.sleep(5000); |
|---|
| 102 | } |
|---|
| 103 | catch (Exception e) |
|---|
| 104 | { |
|---|
| 105 | System.out.println("main: exception: " + e.getMessage()); |
|---|
| 106 | } |
|---|
| 107 | System.exit(0); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|