import java.applet.*;
import java.awt.BorderLayout;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.media.j3d.PositionPathInterpolator;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.vecmath.Color3f;
import javax.vecmath.Color4f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
public class Homepage3D extends Applet {
/**
*
*/
private static final long serialVersionUID = -2707836382924126259L;
private Canvas3D canvas3D;
SimpleUniverse simpleU;
/**
* Proof of concept for a java3d homepage.
*/
public Homepage3D() {
setLayout(new BorderLayout());
canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add("Center", canvas3D);
// SimpleUniverse is a Convenience Utility class
simpleU = new SimpleUniverse(canvas3D);
canvas3D.getView().setFrontClipPolicy (View.VIRTUAL_EYE);
// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
ViewingPlatform vp = simpleU.getViewingPlatform();
TransformGroup steerTG = vp.getViewPlatformTransform();
Transform3D t3d = new Transform3D();
steerTG.getTransform(t3d);
t3d.lookAt(new Point3d(0,0,2.4),new Point3d(0,0,0),new Vector3d(0,1,0));
t3d.invert();
steerTG.setTransform(t3d);
BranchGroup scene = createSceneGraph();
// create Light
AmbientLight al = new AmbientLight(new Color3f(128f, 10f, 70f));
al.setInfluencingBounds(new BoundingSphere(new Point3d(), 100000));
DirectionalLight dl = new DirectionalLight(new Color3f(1f, 1f, 1f),
new Vector3f(0.5f, 0.5f, -1f));
dl.setInfluencingBounds(new BoundingSphere(new Point3d(), 100000));
scene.addChild(dl);
scene.addChild(al);
OrbitBehavior orbit = new OrbitBehavior(canvas3D, OrbitBehavior.DISABLE_TRANSLATE | OrbitBehavior.DISABLE_ZOOM);
orbit.setSchedulingBounds (new BoundingSphere ());
simpleU.getViewingPlatform().setViewPlatformBehavior (orbit);
scene.compile();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// START MAIN ELEMENT
TransformGroup objSpin = new TransformGroup();
objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
Appearance boxAppearance = new Appearance();
boxAppearance.setMaterial(new Material(new Color3f(0.2f, 0.2f, 0.2f),
new Color3f(0.2f, 0.2f, 0.2f), new Color3f(1.0f, 0.0f, 1.0f),
new Color3f(0.3f, 0.3f, 0.3f), 1f));
boxAppearance.setTexture(new TextureLoader("src/back.png", null)
.getTexture());
boxAppearance.setTextureAttributes(new TextureAttributes(
TextureAttributes.COMBINE, new Transform3D(), new Color4f(),
TextureAttributes.NICEST));
Box box = new Box(0.4f, 0.4f, 0.4f, Primitive.GENERATE_NORMALS
| Primitive.GENERATE_TEXTURE_COORDS, boxAppearance);
objSpin.addChild(box);
// /MOUSE ROTATOR FOR TRANSFORMGROUP
/*MouseRotate myMouseRotate = new MouseRotate();
myMouseRotate.setTransformGroup(objSpin);
myMouseRotate.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMouseRotate);*/
// /END MAIN ELEMENT
// /1. child element
Box box1 = new Box(0.1f, 0.1f, 0.1f, Primitive.GENERATE_NORMALS
| Primitive.GENERATE_TEXTURE_COORDS, boxAppearance);
TransformGroup transformGroup = new TransformGroup();
Transform3D transform3D = new Transform3D();
transform3D.setTranslation(new Vector3f(0f, 0f, box.getZdimension()
+ box1.getZdimension()));
transformGroup.setTransform(transform3D);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
boxAppearance = new Appearance();
boxAppearance.setTexture(new TextureLoader("src/browser.png", null)
.getTexture());
boxAppearance.setTextureAttributes(new TextureAttributes(
TextureAttributes.REPLACE, new Transform3D(), new Color4f(),
TextureAttributes.NICEST));
box1.setAppearance(boxAppearance);
box1.setUserData("chris");
transformGroup.addChild(box1);
// end 1. child element
// /2. child element
Box box2 = new Box(0.1f, 0.1f, 0.1f, Primitive.GENERATE_NORMALS
| Primitive.GENERATE_TEXTURE_COORDS, boxAppearance);
TransformGroup transformGroup2 = new TransformGroup();
Transform3D transform3D2 = new Transform3D();
transform3D2.setTranslation(new Vector3f(0f, 0f,
-(box.getZdimension() + box1.getZdimension())));
transformGroup2.setTransform(transform3D2);
Appearance boxAppearance2 = new Appearance();
boxAppearance2.setTexture(new TextureLoader("src/clock.png", null)
.getTexture());
boxAppearance2.setTextureAttributes(new TextureAttributes(
TextureAttributes.REPLACE, new Transform3D(), new Color4f(),
TextureAttributes.NICEST));
box2.setAppearance(boxAppearance2);
transformGroup2.addChild(box2);
// end 1. child element
// add child elements to spinning transformgroup
objSpin.addChild(transformGroup);
objSpin.addChild(transformGroup2);
Alpha aPos = new Alpha(1,
Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE,
0,
0,
3000,
1500,
0,
3000,
1500,
0);
aPos.setStartTime(0);
// TRANSLATION
float [] knots = new float[2];
knots[0] = 0.0f;
knots[1] = 1.0f;
Point3f [] positions = new Point3f[2];
Vector3f vec = new Vector3f();
transform3D.get(vec);
positions[0] = new Point3f(vec.getX(),vec.getY(),vec.getZ());
positions[1] = new Point3f(vec.getX(),vec.getY(),vec.getZ());
PositionPathInterpolator pi =
new PositionPathInterpolator(aPos, transformGroup, transform3D, knots, positions);
System.out.println();
pi.setSchedulingBounds(new BoundingSphere(new Point3d(), 100000));
pi.setCapability (javax.media.j3d.Node.ALLOW_LOCAL_TO_VWORLD_READ);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transformGroup.addChild(pi);
// ADD PICKTRANSLATEBEHAVIOR
AnimationPickMouseBehaviour pickBehavior = new AnimationPickMouseBehaviour
(canvas3D, objRoot, new BoundingSphere(new Point3d(), 100000), pi, simpleU);
objRoot.addChild(pickBehavior);
objRoot.addChild(objSpin);
return objRoot;
}
public static void main(String[] args) {
new MainFrame(new Homepage3D(), 256, 256);
}
}