Ava 3D and user interface
Canvas3d
Each area that can draw a 3D image is called canvas3d. It contains the rectangle of the object view in your environment (universe. You put the canvas into a frame, so you create an environment that can be displayed in the canvas.
The following example shows how to create a canvas in a frame with labels at the upper and lower ends. The program can run as an applet or application.
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 Applet11 {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 and swing
Canvas3d leverages your computer's graphics card to enhance performance. Unfortunately, this also means that it cannot work well with Sun's swing user interface controls. These controls are called "lightweight. These lightweight controls are hidden by canvas3d even at the front end.
We have several solutions to this problem:
● If you place lightweight and heavyweight controls in different containers, they can be mixed on one screen.
● If you use the pop-up menu, the static jpopupmenu method can solve this problem:
Setdefalightlightweightpopupenabled (false );
You can also use the old AWT controls to replace swing.