// From: http://www.micg.et.fh-stralsund.de/Java3D/java3D.htm#Bild1
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class Erscheinungsbild extends Applet {
/**
* init Methoden fur die Darstellung als Applet
*/
public void init() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup szene = macheSzene();
szene.compile();
universe = new SimpleUniverse(canvas3D);
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(szene);
}
/**
* Erstellt den Szenegraphen
*
* @return BranchGroup
*/
public BranchGroup macheSzene() {
BranchGroup objWurzel = new BranchGroup();
// Transformation, 2 Rotationen:
Transform3D drehung = new Transform3D();
Transform3D drehung2 = new Transform3D();
drehung.rotX(Math.PI / 4.0d);
drehung2.rotY(Math.PI / 5.0d);
drehung.mul(drehung2);
TransformGroup objDreh = new TransformGroup(drehung);
objDreh.addChild(new Box(0.5f, 0.5f, 0.5f, makeAppearance()));
objWurzel.addChild(objDreh);
return objWurzel;
}
/**
* Wurfeldarstellung
*
* @return Appearance
*/
private Appearance makeAppearance() {
Appearance a = new Appearance();
ColoringAttributes c = new ColoringAttributes();
c.setColor(new Color3f(0.0f, 1.0f, 0.0f));
a.setColoringAttributes(c);
return a;
}
/**
* gibt speicher frei
*/
public void destroy() {
universe.removeAllLocales();
}
public static void main(String[] args) {
frame = new MainFrame(new Erscheinungsbild(), 500, 500);
frame.setTitle("Appearance");
}
//---- Attribute -----------------------
private SimpleUniverse universe;
private Canvas3D canvas3D;
private static Frame frame;
}
|