Canvas3D: null GraphicsConfiguration解决办法

本文介绍了一个关于Canvas3D使用空图形配置构造函数导致的NullPointerException问题及其解决方案。通过正确的GraphicsConfiguration实例化,避免了后续程序或版本中出现空指针异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ERRORCanvas3D constructed with a null GraphicsConfiguration 
  This will cause a NullPointerException in a subsequent release
  
  错误Canvas3D使用空图形配置构造函数
  
  这会在随后的程序或版本中产生一个空指针例程或错误
  
  解决办法
  
  加入包import java.awt.GraphicsConfiguration;
  
  在setLayout(new BorderLayout());之后插入
  
  GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
  

  Canvas3D c=new Canvas3D(null);改为Canvas3D c=new Canvas3D(config);



转载:https://blog.youkuaiyun.com/sunbeamact/article/details/6235231

Java 可以通过使用 OpenGL 或 Java3D 库实现 3D 界面。以下是一个使用 Java3D 库实现 3D 界面的示例代码: ```java import java.awt.BorderLayout; import java.awt.GraphicsConfiguration; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.DirectionalLight; import javax.media.j3d.PolygonAttributes; import javax.media.j3d.Shape3D; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.swing.JFrame; import javax.vecmath.Color3f; import javax.vecmath.Point3d; import javax.vecmath.Vector3d; import com.sun.j3d.utils.geometry.Box; import com.sun.j3d.utils.geometry.Cone; import com.sun.j3d.utils.geometry.Cylinder; import com.sun.j3d.utils.geometry.Sphere; import com.sun.j3d.utils.geometry.Torus; import com.sun.j3d.utils.universe.SimpleUniverse; public class Java3DExample extends JFrame { private static final long serialVersionUID = 1L; public Java3DExample() { super("Java3D Example"); // Creating the 3D canvas GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); getContentPane().add(canvas, BorderLayout.CENTER); // Creating the Simple Universe and the Branch Group SimpleUniverse universe = new SimpleUniverse(canvas); BranchGroup group = new BranchGroup(); // Creating the Shapes Box box = new Box(0.3f, 0.3f, 0.3f, Box.GENERATE_TEXTURE_COORDS, null); Cone cone = new Cone(0.3f, 0.5f); Cylinder cylinder = new Cylinder(0.3f, 0.5f); Sphere sphere = new Sphere(0.4f); Torus torus = new Torus(0.2f, 0.4f); // Setting Attributes for the Shapes Color3f color = new Color3f(0.7f, 0.7f, 0.7f); PolygonAttributes polyAttributes = new PolygonAttributes(); polyAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL); polyAttributes.setCullFace(PolygonAttributes.CULL_NONE); Shape3D shape = box.getShape(Box.FRONT); shape.setAppearance(shape.getAppearance()); shape.getAppearance().setPolygonAttributes(polyAttributes); shape.getAppearance().getColoringAttributes().setColor(color); shape = cone.getShape(); shape.setAppearance(shape.getAppearance()); shape.getAppearance().setPolygonAttributes(polyAttributes); shape.getAppearance().getColoringAttributes().setColor(color); shape = cylinder.getShape(Cylinder.BODY); shape.setAppearance(shape.getAppearance()); shape.getAppearance().setPolygonAttributes(polyAttributes); shape.getAppearance().getColoringAttributes().setColor(color); shape = sphere.getShape(); shape.setAppearance(shape.getAppearance()); shape.getAppearance().setPolygonAttributes(polyAttributes); shape.getAppearance().getColoringAttributes().setColor(color); shape = torus.getShape(); shape.setAppearance(shape.getAppearance()); shape.getAppearance().setPolygonAttributes(polyAttributes); shape.getAppearance().getColoringAttributes().setColor(color); // Adding the Shapes to the Branch Group Transform3D transform = new Transform3D(); TransformGroup tg = new TransformGroup(transform); tg.addChild(box); group.addChild(tg); transform = new Transform3D(); transform.setTranslation(new Vector3d(0.0, -0.6, 0.0)); tg = new TransformGroup(transform); tg.addChild(cone); group.addChild(tg); transform = new Transform3D(); transform.setTranslation(new Vector3d(0.0, 0.6, 0.0)); tg = new TransformGroup(transform); tg.addChild(cylinder); group.addChild(tg); transform = new Transform3D(); transform.setTranslation(new Vector3d(-0.6, 0.0, 0.0)); tg = new TransformGroup(transform); tg.addChild(sphere); group.addChild(tg); transform = new Transform3D(); transform.setTranslation(new Vector3d(0.6, 0.0, 0.0)); tg = new TransformGroup(transform); tg.addChild(torus); group.addChild(tg); // Creating and Adding Light to the Branch Group DirectionalLight light = new DirectionalLight(new Color3f(1.0f, 1.0f, 1.0f), new Vector3d(-1.0, -1.0, -1.0)); light.setInfluencingBounds(new javax.vecmath.BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0)); group.addChild(light); group.compile(); // Setting the View Point universe.getViewingPlatform().setNominalViewingTransform(); universe.addBranchGraph(group); // Window listener to close the window addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { dispose(); System.exit(0); } }); // Setting the Size of the Window setSize(400, 400); setVisible(true); } public static void main(String[] args) { Java3DExample example = new Java3DExample(); } } ``` 此示例代码会创建一个包含五个不同形状的 3D 对象,并添加了一个满足所需挑战的光源,最后输出一个 3D 界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值