CanvasDemo.java Source code

Java tutorial

Introduction

Here is the source code for CanvasDemo.java

Source

/*
The Joy of Java 3D
    
by Greg Hopkins
    
Copyright Copyright 2001
    
    
*/

/*
Canvas3D
    
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. 
    
    
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:
    
* You can mix lightweight and heavyweight components on the same screen if you keep them in separate containers. 
    
* if you use Popup menus, a static function on JPopupMenu fixes the problem:
setDefaultLightWeightPopupEnabled(false);
    
* You can use the older AWT components insteadof swing.
    
    
*/

//

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.Label;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class CanvasDemo extends Applet {

    public CanvasDemo() {

        setLayout(new BorderLayout());
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        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);
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(contents);
    }

    public static void main(String[] args) {
        CanvasDemo demo = new CanvasDemo();
        new MainFrame(demo, 400, 400);
    }
}