import java.awt.*;
import java.io.*;
import java.awt.image.*;
import java.awt.GraphicsConfiguration;
import javax.imageio.*;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingBox;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.IndexedQuadArray;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.vecmath.*;
import javax.vecmath.Point3d;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class Welt extends JFrame {
public Welt(String title) {
super(title);
// AnzeigeInfos holen (Farbtiefe, usw...
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
// ein neues Canvas mit den AnzeigeInfos erzeugen und dem Frame
// hinzufügen
Canvas3D canvas3d = new Canvas3D(config);
add(canvas3d);
// ein neues Universum im Canvas erzeugen und eine Betrachtungsebene
// erzeugen
SimpleUniverse universe = new SimpleUniverse(canvas3d);
universe.getViewingPlatform().setNominalViewingTransform();
// neue Transform Informationen
Transform3D transform3d = new Transform3D();
// neue Transformgruppe
TransformGroup transroot = new TransformGroup(transform3d);
float[] verts =
{
// front face
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
// back face
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
// right face
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
// left face
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
// top face
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
// bottom face
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
};
QuadArray q = new QuadArray(24, IndexedQuadArray.COORDINATES
| IndexedQuadArray.TEXTURE_COORDINATE_2);
q.setCoordinates(0, verts);
q.setTextureCoordinate ( 0, new Point2f(0.0f,0.0f));
q.setTextureCoordinate ( 1, new Point2f(1.0f,0.0f));
q.setTextureCoordinate ( 2, new Point2f(1.0f,1.0f));
q.setTextureCoordinate ( 3, new Point2f(0.0f,1.0f));
q.setTextureCoordinate ( 4, new Point2f(0.2f,0.0f));
q.setTextureCoordinate ( 5, new Point2f(0.5f,0.0f));
q.setTextureCoordinate ( 6, new Point2f(0.5f,0.3f));
q.setTextureCoordinate ( 7, new Point2f(0.2f,0.3f));
q.setTextureCoordinate ( 8, new Point2f(0.6f,0.3f));
q.setTextureCoordinate ( 9, new Point2f(0.9f,0.3f));
q.setTextureCoordinate (10, new Point2f(0.9f,0.9f));
q.setTextureCoordinate (11, new Point2f(0.6f,0.9f));
Shape3D s = new Shape3D(q, texture("img.jpg"));
transroot.addChild(s);
Background b = new Background();
b.setColor(new Color3f(Color.green));
BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0,
0.0), // Center
1000.0); // Extent
// Set the background color and its application bounds
b.setCapability(Background.ALLOW_COLOR_WRITE);
b.setApplicationBounds(worldBounds);
BoundingBox boundBox = new BoundingBox(new Point3d(-10, -10, -10),
new Point3d(10, 10, 10));
// Muss gesetzt werden um das Betrachten mit der Maus zu erlauben
transroot.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
transroot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// rotieren mit der linken Maustaste
MouseRotate behavior = new MouseRotate(transroot);
behavior.setSchedulingBounds(boundBox);
transroot.addChild(behavior);
// zoomen mit der mitleren Maustaste
MouseZoom mouseBeh2 = new MouseZoom(transroot);
mouseBeh2.setSchedulingBounds(boundBox);
transroot.addChild(mouseBeh2);
// verschieben mit der rechten Maustaste
MouseTranslate mouseBeh3 = new MouseTranslate(transroot);
mouseBeh3.setSchedulingBounds(boundBox);
transroot.addChild(mouseBeh3);
// neue Branchgruppe
BranchGroup branchgroup = new BranchGroup();
// Transformgruppe an Branchgruppe hängen
transroot.addChild(b);
branchgroup.addChild(transroot);
// ... an das Universum hängen
universe.addBranchGraph(branchgroup);
// Bounds inherhalb derer das Behaviour gelten soll
}
public Appearance texture(String s) {
Appearance appear = new Appearance();
appear.setCapability(TextureAttributes.ALLOW_MODE_WRITE);
BufferedImage bi = null;
try
{
bi = ImageIO.read(new File(s));
}
catch (Exception e)
{
e.printStackTrace();
}
TextureLoader loader = new TextureLoader(bi);
ImageComponent2D myImage = loader.getImage();
Texture2D myTex = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
myImage.getWidth(), myImage.getHeight());
myTex.setImage(0, myImage);
myTex.setEnable(true);
myTex.setBoundaryModeS(Texture.WRAP);
myTex.setBoundaryModeT(Texture.WRAP);
appear.setTexture(myTex);
return (appear);
}
public static void main(String args[]) {
Welt beispiel = new Welt("Beispiel");
beispiel.setSize(600, 400);
beispiel.setVisible(true);
beispiel.setDefaultCloseOperation(EXIT_ON_CLOSE);
//beispiel.setExtendedState(MAXIMIZED_BOTH);
}
}