How to view Canvas3D in a JPanel
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Saturday, November 17, 2001 07:35 AM

I want to view a simple scene in a JPanel but I get only the frame without scene. So what is wrong withe the following code. The structure of the code is very simple. 1) Model3DFrame is a very simple user interface built with JBuilder 5. 2) My3D make a scene to view in the frame of type Model3DFrame. Here is the code: package mytest; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.image.*; import javax.media.j3d.*; import java.awt.*; import java.awt.event.*; import java.awt.GraphicsConfiguration; import javax.vecmath.*; imp   More>>

I want to view a simple scene in a JPanel but I get only the frame without scene. So what is wrong withe the following code. The structure of the code is very simple.
1) Model3DFrame is a very simple user interface built with JBuilder 5.

2) My3D make a scene to view in the frame of type Model3DFrame.

Here is the code:


package mytest;



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

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

import com.sun.j3d.utils.image.*;

import javax.media.j3d.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.GraphicsConfiguration;

import javax.vecmath.*;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;





public class My3D {



public My3D() {

boolean packFrame = false;



Model3DFrame frame = new Model3DFrame();



//Center the window

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = frame.getSize();

if (frameSize.height > screenSize.height) {

frameSize.height = screenSize.height;

}

if (frameSize.width > screenSize.width) {

frameSize.width = screenSize.width;

}

frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);



//Validate frames that have preset sizes

//Pack frames that have useful preferred size info, e.g. from their layout



if (packFrame) {

frame.pack();

}

else {

frame.validate();

}

frame.setVisible(true);



// Graphics

GraphicsConfiguration graphicsConfiguration = SimpleUniverse.getPreferredConfiguration();



Canvas3D canvas = new Canvas3D(graphicsConfiguration);



// Attach canvas to jPanelContentPanne of the frame

frame.jPanelContentPanne.add("center", canvas);



// Create the universe

SimpleUniverse universe = new SimpleUniverse(canvas);



// Create the scene

BranchGroup group = createSceneGraph();



// View point

universe.getViewingPlatform().setNominalViewingTransform();



// add the group of objects to the Universe

universe.addBranchGraph(group);

}

private BranchGroup createSceneGraph() {

// Create a structure to contain objects

BranchGroup group = new BranchGroup();

// Add a color cube

group.addChild(new ColorCube(0.3));

return group;

} // end of createSceneGraph



public static void main( String[] args){

new My3D();

}





} // end of  My3D class


package mytest;



import java.applet.Applet;

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

import java.lang.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import com.borland.dbswing.*;

import java.io.*;

import javax.swing.text.*;

import javax.swing.event.*;

import java.awt.print.*;

import com.borland.jbcl.layout.*;



public class Model3DFrame extends JFrame {

IntlSwingSupport intlSwingSupport1 = new IntlSwingSupport();

JPanel contentPane;



JPanel jPanelCenter = new JPanel();

JPanel jPanelFileViewTab = new JPanel();

JPanel jPanelContentPanne = new JPanel();

JPanel jPanelFileTab = new JPanel();

VerticalFlowLayout verticalFlowLayoutCenter = new VerticalFlowLayout();

JPanel jPanelWest = new JPanel();

VerticalFlowLayout verticalFlowLayoutWest = new VerticalFlowLayout();

JPanel jPanelEast = new JPanel();

VerticalFlowLayout verticalFlowLayoutSouth = new VerticalFlowLayout();

JPanel jPanelSouth = new JPanel();

VerticalFlowLayout verticalFlowLayoutNorth = new VerticalFlowLayout();

JPanel jPanelNorth = new JPanel();

VerticalFlowLayout verticalFlowLayoutEast = new VerticalFlowLayout();

JPanel jPanelMessagePanne = new JPanel();

JPanel jPanelDataEntryPanne = new JPanel();

JPanel jPanelStatusBar = new JPanel();

JPanel jPanelMainMenu = new JPanel();

JPanel jPanelMainToolBar = new JPanel();

JPanel jPanelProjectToolBar = new JPanel();

BorderLayout borderLayout = new BorderLayout();



// Built with JBuilder 5




public Model3DFrame() {



enableEvents(AWTEvent.WINDOW_EVENT_MASK); // protected final void

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}



private void jbInit() throws Exception {



// Class

this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

this.setSize(new Dimension(400, 300));

this.setTitle("Model 3D");

// Content panne

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(borderLayout);

jPanelWest.setLayout(verticalFlowLayoutWest);

jPanelEast.setLayout(verticalFlowLayoutSouth);

jPanelSouth.setLayout(verticalFlowLayoutNorth);

jPanelNorth.setLayout(verticalFlowLayoutEast);

jPanelSouth.add(jPanelStatusBar, null);

jPanelSouth.add(jPanelDataEntryPanne, null);

jPanelSouth.add(jPanelMessagePanne, null);

contentPane.add(jPanelCenter, BorderLayout.CENTER);

jPanelCenter.setLayout(verticalFlowLayoutCenter);

jPanelCenter.add(jPanelFileTab, null);

jPanelCenter.add(jPanelContentPanne, null);

jPanelCenter.add(jPanelFileViewTab, null);

contentPane.add(jPanelWest, BorderLayout.WEST);

contentPane.add(jPanelEast, BorderLayout.EAST);

contentPane.add(jPanelSouth, BorderLayout.SOUTH);

contentPane.add(jPanelNorth, BorderLayout.NORTH);

jPanelNorth.add(jPanelProjectToolBar, null);

jPanelNorth.add(jPanelMainToolBar, null);

jPanelNorth.add(jPanelMainMenu, null);

}



} // End of Model3DFrame


 

   <<Less
About | Sitemap | Contact