import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.Quat4d;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
public class Cube04 extends Frame implements MouseListener
{
public RotationInterpolator rotationInterpolator = null;
public Alpha alpha = null;
public Integer oldPos = null;
public Cube04 (String title) {
// AnzeigeInfos
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
// Canvas mit den AnzeigeInfos
Canvas3D canvas3d = new Canvas3D (config);
add(canvas3d);
// Universum im Canvas
SimpleUniverse simpleUniverse = new SimpleUniverse(canvas3d);
// Kamera
ViewingPlatform viewingPlatform = simpleUniverse.getViewingPlatform();
TransformGroup viewTransformGroup = viewingPlatform.getMultiTransformGroup().getTransformGroup(0);
Transform3D viewTransform3D = new Transform3D();
viewTransformGroup.getTransform(viewTransform3D);
viewTransform3D.rotX(-0.3); // Ausrichtung
viewTransformGroup.setTransform(viewTransform3D);
viewTransform3D.setTranslation(new Vector3f(0.0f, 0.9f, 3.0f)); // Position
viewTransformGroup.setTransform(viewTransform3D);
// Branchgruppe
BranchGroup branchGroup = new BranchGroup();
// Transformgruppen
TransformGroup tgInteraction = new TransformGroup();
tgInteraction.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tgInteraction.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
TransformGroup tgAnimation = new TransformGroup();
tgAnimation.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tgAnimation.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// Appearance
TextureLoader textureLoader = new TextureLoader("test.jpg", this);
Texture texture = textureLoader.getTexture();
Appearance appBox = new Appearance();
appBox.setTexture(texture);
// Würfel
Box box = new Box(0.5f, 0.5f, 0.5f, Box.GENERATE_TEXTURE_COORDS, appBox);
// Animation
Transform3D transform3D = new Transform3D();
alpha = new Alpha(-1, 5000);
rotationInterpolator = new RotationInterpolator(alpha, tgAnimation, transform3D, 0.0f, (float)Math.PI*2);
rotationInterpolator.setSchedulingBounds(new BoundingSphere());
// MausInteraktion
MouseRotate mouseRotate = new MouseRotate();
mouseRotate.setTransformGroup(tgInteraction);
mouseRotate.setSchedulingBounds(new BoundingSphere());
mouseRotate.setFactor(0.005, 0.0);
canvas3d.addMouseListener(this);
branchGroup.addChild(tgInteraction);
tgInteraction.addChild(mouseRotate);
tgInteraction.addChild(tgAnimation);
tgAnimation.addChild(rotationInterpolator);
tgAnimation.addChild(box);
branchGroup.compile();
simpleUniverse.addBranchGraph(branchGroup);
}
public void mousePressed(MouseEvent e)
{
alpha.pause();
oldPos = e.getX();
}
public void mouseReleased(MouseEvent e)
{
if(oldPos != e.getX())
{
if(oldPos > e.getX())
{
rotationInterpolator.setMaximumAngle(-(float)Math.PI*2);
}
else
{
rotationInterpolator.setMaximumAngle((float)Math.PI*2);
}
}
alpha.resume();
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public static void main(String[] args)
{
Cube04 scene = new Cube04("Cube3D");
scene.setSize(800,600);
scene.setVisible(true);
}
}