001: /*
002: * $RCSfile: Hello3D.java,v $
003: *
004: * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.1 $
041: * $Date: 2004/09/20 21:16:46 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.apps.hello3d;
046:
047: import java.applet.Applet;
048: import java.awt.BorderLayout;
049: import java.awt.event.*;
050: import com.sun.j3d.utils.applet.MainFrame;
051: import com.sun.j3d.utils.geometry.ColorCube;
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055:
056: /**
057: * Hello3D renders a single, static cube.
058: * The cube is rotated, so it looks more like a 3D object.
059: */
060: public class Hello3D extends Applet {
061:
062: private SimpleUniverse u = null;
063:
064: public BranchGroup createSceneGraph() {
065: // Create the root of the branch graph
066: BranchGroup objRoot = new BranchGroup();
067:
068: // spin object has composited transformation matrix
069: Transform3D spin = new Transform3D();
070: Transform3D tempspin = new Transform3D();
071:
072: spin.rotX(Math.PI / 4.0d);
073: tempspin.rotY(Math.PI / 5.0d);
074: spin.mul(tempspin);
075:
076: TransformGroup objTrans = new TransformGroup(spin);
077: objRoot.addChild(objTrans);
078:
079: // Create a simple shape leaf node, add it to the scene graph.
080: // ColorCube is a Convenience Utility class
081: objTrans.addChild(new ColorCube(0.4));
082:
083: // Let Java 3D perform optimizations on this scene graph.
084: objRoot.compile();
085:
086: return objRoot;
087: }
088:
089: public Hello3D() {
090: }
091:
092: public void init() {
093: setLayout(new BorderLayout());
094:
095: Canvas3D c = new Canvas3D(SimpleUniverse
096: .getPreferredConfiguration());
097: add("Center", c);
098:
099: // Create a simple scene and attach it to the virtual universe
100: BranchGroup scene = createSceneGraph();
101: u = new SimpleUniverse(c);
102:
103: // This will move the ViewPlatform back a bit so the
104: // objects in the scene can be viewed.
105: u.getViewingPlatform().setNominalViewingTransform();
106:
107: u.addBranchGraph(scene);
108: }
109:
110: public void destroy() {
111: u.cleanup();
112: }
113:
114: //
115: // The following allows Hello3D to be run as an application
116: // as well as an applet
117: //
118: public static void main(String[] args) {
119: new MainFrame(new Hello3D(), 256, 256);
120: }
121: }
|