Java3D JFrame Source Code

This class is the basic JFrame class for Java3D applications, feel free to use this code however you wish.

/*
 * Basic Java3D JFrame Class
 * Developed for JustJava3d.wordpress.com
 */

//Imports
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ToolTipManager;

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

//JFrame class inherits the code from the Swing JPanel class.
public class Main extends JPanel {

	//The constructor method
	public Main() {

		//Set the layout, this is just a standard thing.
		setLayout(new BorderLayout());

		//Get the preferred graphics configuration for the default screen
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

		//Create a Canvas3D using the preferred configuration
		Canvas3D canvas = new Canvas3D(config);

		//Create a Simple Universe with view branch
		SimpleUniverse universe = new SimpleUniverse(canvas);

		//Create the root of the branch graph
		BranchGroup group = new BranchGroup();

		//Add the canvas into the center of the screen
		add("Center", canvas);

		//This will move the ViewPlatform back so the objects in the scene can be viewed
		universe.getViewingPlatform().setNominalViewingTransform();

		//Set the render distance of objects
		universe.getViewer().getView().setBackClipDistance(100.0);

		//Add the branch into the Universe
		universe.addBranchGraph(group);

	}

	//Main method
	public static void main(String[] args) {
		//Set the No Erase Background property to true
		System.setProperty("sun.awt.noerasebackground", "true");

		//Disable the JPopupMenu
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);

		//Create an instance for the ToolTipManager
		ToolTipManager ttm = ToolTipManager.sharedInstance();

		//Disable the ToolTipManager
		ttm.setLightWeightPopupEnabled(false);

		//Create object instance
		Main main = new Main();

		//Create new JFrame instance
		JFrame frame = new JFrame("This is a Default JFrame Created by Just Java 3D");

		//Set the size of the JFrame
		frame.setSize(750, 500);

		//The frame will now hold the main canvas
		frame.getContentPane().add(main);

		//Make sure the frame is actually visisble
		frame.setVisible(true);
	}
}

Advertisements