- // Klasse Universe
- //
- // (c)2000 Stefan Theess, FH Wedel
-
- import javax.media.j3d.*;
- import javax.vecmath.*;
- import com.sun.j3d.utils.geometry.*;
- import com.sun.j3d.utils.universe.*;
- import com.sun.j3d.utils.image.TextureLoader;
- import com.sun.j3d.utils.behaviors.mouse.*;
- import com.sun.j3d.utils.applet.MainFrame;
- import java.applet.Applet;
- import java.awt.*;
-
- // Universe simuliert die Bewegung der Erde und des Mondes um die Sonne
-
- public class Universe extends Applet {
-
- private static final Color3f BLACK = new Color3f(0f, 0f, 0f);
- private static final Color3f WHITE = new Color3f(1f, 1f, 1f);
- private static final String TEXTURE_FNAME = "earth.jpg";
- private static final float EARTHRADIUS = 0.1f;
- private static final float SUNRADIUS = 0.05f;
- private static final float MOONRADIUS = 0.033f;
- private static final float MOONORBIT = 0.3f; /* Radius Mondumlaufb */
- private static final float EARTHORBIT = 0.7f; /* Radius Erdumlaufb */
- private static final float ECLIPTIC = -0.4f; /* Ekliptikwinkel */
- private static final float EARTHAXIS = 0.25f; /* Laenge Erdachse */
- private static final long MONTH = 3000; /* Zeit Mond um Erde */
- private static final long DAY = 100; /* Zeit Erde um sich selbst */
- private static final long YEAR = 36500; /* Zeit Erde um Sonne */
- private static final int CIRCLECOORDS = 32; /* Anzahl Kreiskoordn */
-
- public Universe() {
- setLayout(new BorderLayout());
-
- GraphicsConfiguration config =
- SimpleUniverse.getPreferredConfiguration();
-
- Canvas3D canvas3D = new Canvas3D(config);
- add("Center", canvas3D);
-
- // SimpleUniverse vereinfacht das Erstellen des Universums
- SimpleUniverse simpleUni = new SimpleUniverse(canvas3D);
-
- BranchGroup sceneGraph = createSceneGraph();
-
- // ViewPlatform wird so eingestellt,
- // dass man die Objekte betrachten kann
- simpleUni.getViewingPlatform().setNominalViewingTransform();
-
- simpleUni.addBranchGraph(sceneGraph);
- } // Ende Konstruktor Universe
-
- // liefert die Koordinaten eines Kreises mit dem Radius r und N Paaren
-
- private Point3f[] createCircleCoords(float r, int N) {
- Point3f[] coords = new Point3f[N];
- int n;
- double a;
- float x, z;
-
- for (a = 0, n = 0; n < N; a = 2.0 * Math.PI / (N-1) * ++n) {
- x = (float) (r * Math.sin(a));
- z = (float) (r * Math.cos(a));
- coords[n] = new Point3f(x, 0f, z);
- }
- return coords;
- } // Ende Methode createCircleCoords
-
- // erzeugt die Geometrie eines Kreises mit Radius r und N Punkten
-
- private Geometry createCircleGeometry(float r, int N) {
- int[] stripCounts = {N};
- LineStripArray lsa =
- new LineStripArray(N, LineStripArray.COORDINATES, stripCounts);
- lsa.setCoordinates(0, createCircleCoords(r, N));
- return lsa;
- } // Ende Methode createCircleGeometry
-
- // erzeugt die Geometrie einer Linie der Laenge l
-
- private Geometry createLineGeometry(float l) {
- LineArray la = new LineArray(2, LineArray.COORDINATES);
- la.setCoordinate(0, new Point3f(0f, -l/2f, 0f));
- la.setCoordinate(1, new Point3f(0f, l/2f, 0f));
- return la;
- } // Ende Methode createLineGeometry
-
- // erzeugt den Content-Branch des Szenengraphen
-
- public BranchGroup createSceneGraph() {
- BranchGroup objRoot = new BranchGroup();
-
- BoundingSphere bounds = new BoundingSphere();
-
- // erzeugt die Erde
-
- Appearance earthAppear = new Appearance();
- earthAppear.setTexture(
- new TextureLoader(TEXTURE_FNAME, this).getTexture());
- Sphere earth = new Sphere(EARTHRADIUS,
- Primitive.GENERATE_TEXTURE_COORDS,
- earthAppear);
-
- // erzeugt die Sonne
-
- Appearance sunAppear = new Appearance();
- sunAppear.setMaterial(new Material(BLACK, WHITE, WHITE, WHITE, 1f));
- Sphere sun = new Sphere(SUNRADIUS, sunAppear);
-
- // erzeugt den Mond
-
- Appearance moonAppear = new Appearance();
- moonAppear.setMaterial(new Material(BLACK, BLACK, WHITE, WHITE, 1f));
- Sphere moon = new Sphere(MOONRADIUS, moonAppear);
-
- // erzeugt die Umlaufbahnen und die Erdachse
-
- Shape3D earthOrbit =
- new Shape3D(createCircleGeometry(EARTHORBIT, CIRCLECOORDS));
- Shape3D moonOrbit =
- new Shape3D(createCircleGeometry(MOONORBIT, CIRCLECOORDS));
- Shape3D earthAxis =
- new Shape3D(createLineGeometry(EARTHAXIS));
-
- // erzeugt das Sonnenlicht
-
- PointLight sunlight = new PointLight();
- sunlight.setInfluencingBounds(bounds);
-
- // versetzt den Mond auf seine Umlaufbahn
-
- Transform3D moonTranslate = new Transform3D();
- moonTranslate.set(new Vector3f(0f, 0f, MOONORBIT));
- TransformGroup objMoonTranslate = new TransformGroup(moonTranslate);
-
- // simuliert einen Monat (Bewegung Mond um Erde)
-
- TransformGroup objMonth = new TransformGroup();
- objMonth.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
- RotationInterpolator rotIntMonth =
- new RotationInterpolator(new Alpha(-1, MONTH), objMonth);
- rotIntMonth.setSchedulingBounds(bounds);
-
- // simuliert einen Tag (Bewegung der Erde um sich selbst)
-
- TransformGroup objDay = new TransformGroup();
- objDay.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
- RotationInterpolator rotIntDay =
- new RotationInterpolator(new Alpha(-1, DAY), objDay);
- rotIntDay.setSchedulingBounds(bounds);
-
- // simuliert die Ekliptik (Neigung der Erdachse)
-
- Transform3D ecliptic = new Transform3D();
- ecliptic.rotZ(ECLIPTIC);
- TransformGroup objEcliptic = new TransformGroup(ecliptic);
-
- float[] knots = new float[CIRCLECOORDS];
- for (int n = 0; n < CIRCLECOORDS; ++n) {
- knots[n] = (float)n * (1 / (float)(CIRCLECOORDS-1));
- }
-
- // simuliert ein Jahr (Bewegung der Erde um die Sonne)
-
- TransformGroup objYear = new TransformGroup();
- objYear.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
- PositionPathInterpolator rotIntYear =
- new PositionPathInterpolator(
- new Alpha(-1, YEAR),
- objYear,
- new Transform3D(),
- knots,
- createCircleCoords(EARTHORBIT, CIRCLECOORDS)
- );
- rotIntYear.setSchedulingBounds(bounds);
-
- // Mouse Rotation Behavior zum Rotieren des Universums mit der Maus
-
- TransformGroup objRotate = new TransformGroup();
- objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
- objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
- MouseRotate mouseRotate = new MouseRotate();
- mouseRotate.setTransformGroup(objRotate);
- mouseRotate.setSchedulingBounds(bounds);
-
- // setzt den Szenengraphen zusammen
-
- objRoot.addChild(objRotate);
-
- objRotate.addChild(mouseRotate);
- objRotate.addChild(sun);
- objRotate.addChild(sunlight);
- objRotate.addChild(objYear);
- objRotate.addChild(earthOrbit);
-
- objYear.addChild(rotIntYear);
- objYear.addChild(objEcliptic);
- objYear.addChild(objMonth);
-
- objEcliptic.addChild(objDay);
-
- objDay.addChild(rotIntDay);
- objDay.addChild(earth);
- objDay.addChild(earthAxis);
-
- objMonth.addChild(rotIntMonth);
- objMonth.addChild(objMoonTranslate);
- objMonth.addChild(moonOrbit);
-
- objMoonTranslate.addChild(moon);
-
- // nimmt Optimierungen am Ast des Szenengraphen vor
- objRoot.compile();
-
- return objRoot;
- } // Ende Methode createSceneGraph
-
- // um Programm sowohl als Applet wie auch als Applikation
- // laufen zu lassen
-
- public static void main(String[] args) {
- // erzeugt Applikationsfenster der Groesse 800x600
- new MainFrame(new Universe(), 800, 600);
- } // Ende Methode main
-
- } // Ende Klasse Universe
... [ Seminar XML und JAVA ] ... [ Thema Java 3D API ] ... [ Beispielprogramm Universe ] ... [ Group Node Objects ] ...