Tutorial 1 - The basic framework

All the Java 3D tutorials (and most 3D apps) are based on a simple window and a 3D rendering panel. In this tutorial we'll create the basic classes to support the future turorials.

MainWindow.java

    /**
     * The entry point into out tutorial application. This is called
     * when the application is executed and will simply create our
     * main window.
     *
     * @param argv The arguments passed to the program
     */
    public static void main(String argv[]) {
        new MainWindow();
    }
This small section of code provides our an application with an entry point. When the application is run the main method will be called passing any arguments to the program as elements in the "argv" array. In this case we're just going to create our MainWindow.
    /** 
     * Creates a new instance of MainWindow. This will initialise the
     * window with a title and create our 3D view. 
     */
    public MainWindow() {
        super("NewDawn Tutorial 1 - The basic framework");
        
        // create our view and set it as the only component
        // in this display
        getContentPane().setLayout(new GridLayout(1,1));
        getContentPane().add(new Display3D());
        
        // add a listener to kill the application should the 
        // window be closed
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        // set the size of the display
        setSize(800,600);
        setVisible(true);
    }
Here we're initialising the window. First off a title. Next we setup up the layout out of window, in this case just a single grid cell to support our 3d view (see later). Next we add a window listener to ensure the application exits cleanly if/when the window is closed. Finally the size of the window is setup and display the window with the setVisible(true) call. All of this simply gets a window on the screen with its contents setup.

Display3D.java

    /** The universe that our world will exist within */
    private SimpleUniverse universe;
    /** The basic branch group in which everything will be put */
    private BranchGroup scene;
    
    /** 
     * Creates a new instance of Display3D 
     */
    public Display3D() {
        super(SimpleUniverse.getPreferredConfiguration());
        
        // construct the simple universe supplying it with 
        // the canvas to render onto
        universe = new SimpleUniverse(this);
        
        // create the base branch group in which our world
        // will be put
        scene = new BranchGroup();
    
        // set the base branch group in the universe
        universe.addBranchGraph(scene);
    }
The Java3D object model require two basic objects for any rendering. Firstly the Canvas3D which is the graphics context to which all 3d elements are rendered. In our case the Canvas3D is subclassed by Display3D. The other object required is the Universe. There are a couple of universe objects supplied by Java3D but for most cases, including this one, SimpleUniverse will surfice.

In our display will first intialise the Canvas3D with the graphics configuration (colour depth etc..) we want to use. The utility method on SimpleUniverse provides easy access to the default setting.

Next we create our universe. This universe will contain all the elements that build up our world. It will also control the view of the world displayed. The simple universe is root node of Java3D's scene graph. The final two lines setup an element in the scene, a BranchGroup, that we can add our geometry to.

Compiling and running this example will present you with a black window. Although this doesn't seem like much its the basis for all applications in java3d.

Before we finish this tutorial we should look in more detail at the most important part of Java3D, the Scene Graph. The Java 3D scene graph allows you to organise the elements in your 3d world. The scene graph is a tree structure in which nodes are place. Different types of nodes effect their children in different ways.

In this tutorial we saw the Universe and BranchGroup nodes in the scene graph. The Universe node is the root of the scene graph tree. Branch group nodes allow organisation of the elements of your scene. They also allow the Java 3D engine to optimise the scene rendering. There is alot of theory and detail involved in understanding the scene graph. I'd recommend reading all about it over at java.sun.com

Get the source code
Back to the tutorials page