日期:2014-05-20 浏览次数:20943 次
java.lang.NullPointerException: Canvas3D: null GraphicsConfiguration at javax.media.j3d.Canvas3D.checkForValidGraphicsConfig(Canvas3D.java:982) at javax.media.j3d.Canvas3D.<init>(Canvas3D.java:1025) at wjg.test.MyCone.<init>(MyCone.java:49) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
package wjg.test; import java.applet.Applet; import java.awt.BorderLayout; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.geometry.Cone; //调用生成Cone的Utility包 import com.sun.j3d.utils.geometry.Primitive; import com.sun.j3d.utils.universe.*; import javax.media.j3d.*; import javax.vecmath.*; public class MyCone extends Applet{ public BranchGroup createSceneGraph(){ BranchGroup objRoot = new BranchGroup(); //创建一个分支节点来包含一组对象 TransformGroup objTrans = new TransformGroup(); //创建转换节点(生成坐标系) objRoot.addChild(objTrans); //将转换节点(坐标系)添加到根节点上 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0); //设置场景的有效范围 Color3f bgColor = new Color3f(0.0f,0.0f,0.0f); //定义背景颜色 Background bg = new Background(bgColor); //创建背景颜色 bg.setApplicationBounds(bounds); objRoot.addChild(bg); //将背景添加到场景中 Color3f directionalColor= new Color3f(1.f,1.f,1.f); Vector3f vec = new Vector3f(0.f,0.f,-1.f); DirectionalLight directionalLight = new DirectionalLight(directionalColor,vec); directionalLight.setInfluencingBounds(bounds); objRoot.addChild(directionalLight); //设置外观 Appearance app = new Appearance(); Material material =new Material(); material.setDiffuseColor(new Color3f(1.0f,1.0f,1.0f)); app.setMaterial(material); //生成基本的圆锥体 Cone cone = new Cone(.5f,1.0f,1,app); //将生成的圆锥添加到场景中 objRoot.addChild(cone); return objRoot; } public MyCone(){ setLayout(new BorderLayout()); Canvas3D c = new Canvas3D(null); add("Center",c); BranchGroup scene = createSceneGraph(); scene.compile(); SimpleUniverse u = new SimpleUniverse(c); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public static void main(String[] args) { // TODO Auto-generated method stub new MainFrame(new MyCone(),400,300); } }