import java.applet.Applet;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.geometry.*;public class shpere extends Applet {
public void init(){
}    BranchGroup createSceneGraph(){
     BranchGroup objRoot = new BranchGroup();
     BoundingSphere bounds = new BoundingSphere();
     //环境光
     AmbientLight lightA = new AmbientLight();
     lightA.setInfluencingBounds(bounds);
     objRoot.addChild(lightA);
     //定向光
     Color3f color1 = new Color3f(1f,1f,1f);
     Vector3f vector = new Vector3f(-.5f,0f,-2f);
     DirectionalLight lightB = new DirectionalLight(color1,vector);
     lightB.setInfluencingBounds(new BoundingSphere());
     objRoot.addChild(lightB);
     //定义坐标系
     Transform3D transform = new Transform3D();
     TransformGroup trans = new TransformGroup(transform);
     trans.setCapability(trans.ALLOW_TRANSFORM_WRITE);
     trans.setCapability(trans.ALLOW_TRANSFORM_READ);
     objRoot.addChild(trans);
     //定义球体外观
     Appearance app = new Appearance();
     //定义球体材质
     Material mat = new Material();
     mat.setDiffuseColor(new Color3f(0f,0f,1f));
     mat.setSpecularColor(new Color3f(1f,1f,1f));
     mat.setShininess(512f);
     app.setMaterial(mat);
     //生成球体
     Sphere s = new Sphere(.3f,app);
     trans.addChild(s);
     objRoot.compile();
     //定义背景
     Background bg = new Background();
     bg.setColor(new Color3f(1f,1f,1f));
     bg.setApplicationBounds(new BoundingSphere());
     objRoot.addChild(bg);
     return objRoot;
    }
    public shpere() throws Exception
{
setLayout(new BorderLayout());
GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
Canvas3D c=new Canvas3D(config);
add("Center",c);
BranchGroup scene=createSceneGraph();
SimpleUniverse u=new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args)
{

    try{
     new MainFrame(new shpere(),200,200);
    }catch(Exception e){
     e.printStackTrace();
    }

}