ava 3D 与用户界面
Canvas3D
能够绘制三维图形的每块区域都被称为是Canvas3D。它是包含了你环境(universe)中对象视图(view)的长方形。你把画布(canvas)放进帧(frame)中,于是你就创建了能够被显示在画布中的环境。
下面的例子展示了如何在上下端有标签的帧中创建画布。程序能够以applet或者应用的方式运行。
1 import com.sun.j3d.utils.universe.SimpleUniverse; 2 import com.sun.j3d.utils.geometry.ColorCube; 3 import javax.media.j3d.BranchGroup; 4 import javax.media.j3d.Canvas3D; 5 import java.awt.GraphicsConfiguration; 6 import java.awt.BorderLayout; 7 import java.awt.Label; 8 import java.applet.Applet; 9 import com.sun.j3d.utils.applet.MainFrame; 10 public class CanvasDemo extends Applet 11 { 12 public CanvasDemo() 13 { 14 setLayout(new BorderLayout()); 15 GraphicsConfiguration config = 16 SimpleUniverse.getPreferredConfiguration(); 17 Canvas3D canvas = new Canvas3D(config); 18 add("North", new Label("This is the top")); 19 add("Center", canvas); 20 add("South", new Label("This is the bottom")); 21 BranchGroup contents = new BranchGroup(); 22 contents.addChild(new ColorCube(0.3)); 23 SimpleUniverse universe = new SimpleUniverse(canvas); 24 universe.getViewingPlatform().setNominalViewingTransform(); 25 universe.addBranchGraph(contents); 26 } 27 public static void main(String[] args) 28 { 29 CanvasDemo demo = new CanvasDemo(); 30 new MainFrame(demo, 400, 400); 31 } 32 }
Java 3D 和 Swing
Canvas3D利用了你计算机的图形卡以加强性能。不幸的是,这也意味着它不能与Sun的swing用户界面控件很好地配合使用。这些控件被称为是“轻量级”的。这些轻量级控件即使应该是在前端的也会被Canvas3D隐藏掉。
对于这个问题我们有若干解决之道:
● 如果你把轻量级和重量级控件放在不同的容器中,则它们可以在一个屏幕上混用。
● 如果你使用了弹出菜单,那么JPopupMenu的静态方法可以修复这个问题:
setDefaultLightWeightPopupEnabled(false);
你也可以使用旧一些的AWT控件代替swing。