Java 3D and the User Interface


Each area where three-dimensional graphics can be painted is called a Canvas3D. This is a rectangle that contains a view of the objects in your universe. You place the canvas inside a frame, then you create a universe to be displayed in the canvas.

The following example shows how to create a canvas in a frame with labels at the top and bottom. The program can be run as either an applet or an application.

import com.sun.j3d.utils.universe.SimpleUniverse;

import com.sun.j3d.utils.geometry.ColorCube;



import java.awt.GraphicsConfiguration;

import java.awt.BorderLayout;

import java.awt.Label;

import java.applet.Applet;

import com.sun.j3d.utils.applet.MainFrame;

public class CanvasDemo extends Applet {

public CanvasDemo() {

   setLayout(new BorderLayout());

   GraphicsConfiguration config =


   Canvas3D canvas = new Canvas3D(config);

   add("North",new Label("This is the top"));

   add("Center", canvas);

   add("South",new Label("This is the bottom"));

   BranchGroup contents = new BranchGroup();

   contents.addChild(new ColorCube(0.3));

   SimpleUniverse universe = new SimpleUniverse(canvas);




public static void main( String[] args ) {

   System.setProperty("sun.awt.noerasebackground", "true");

   CanvasDemo demo = new CanvasDemo();

   new MainFrame(demo,400,400);



Java 3D and Swing

The Canvas3D takes advantage of your computer.s graphics card to increase performance. Unfortunately, this means that it does not mix very well with Sun.s swing user interface components. These components are called "lightweight" Lightweight components can be hidden by a Canvas3D even if they are supposed to be at the front.

There are several solutions to this problem:

Appearance Is Everything <<< Table of Contents >>> Animation and Interaction