|
This Java tip creates a Sphere using Sun's utility classes in Java3D.
import java.applet.Applet;
import java.awt.BorderLayout;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
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.vecmath.Color3f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class LitSphereApp extends Applet {
Appearance createAppearance() {
Appearance appear = new Appearance();
Material material = new Material();
// material.setDiffuseColor(0.0f, 0.0f, 1.0f);
material.setShininess(50.0f);
// make modifications to default material properties
appear.setMaterial(material);
// ColoringAttributes colorAtt = new ColoringAttributes();
// colorAtt.setShadeModel(ColoringAttributes.SHADE_FLAT);
// appear.setColoringAttributes(colorAtt);
return appear;
}
BranchGroup createScene() {
BranchGroup scene = new BranchGroup();
scene.addChild(new Sphere(0.9f, Sphere.GENERATE_NORMALS,
createAppearance()));
AmbientLight lightA = new AmbientLight();
lightA.setInfluencingBounds(new BoundingSphere());
scene.addChild(lightA);
DirectionalLight lightD1 = new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere());
Vector3f direction1 = new Vector3f(-1.0f, -1.0f, -0.5f);
direction1.normalize();
lightD1.setDirection(direction1);
lightD1.setColor(new Color3f(0.0f, 0.0f, 1.0f));
scene.addChild(lightD1);
DirectionalLight lightD2 = new DirectionalLight();
lightD2.setInfluencingBounds(new BoundingSphere());
Vector3f direction2 = new Vector3f(1.0f, -1.0f, -0.5f);
direction2.normalize();
lightD2.setDirection(direction2);
lightD2.setColor(new Color3f(1.0f, 0.0f, 0.0f));
scene.addChild(lightD2);
Background bg = new Background();
bg.setColor(1.0f, 1.0f, 1.0f);
bg.setApplicationBounds(new BoundingSphere());
scene.addChild(bg);
return scene;
}
public LitSphereApp() {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
BranchGroup scene = createScene();
scene.compile();
SimpleUniverse u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String argv[]) {
new MainFrame(new LitSphereApp(), 256, 256);
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.