drawing java3d on JPanel
hi,
i want to draw my java3D object or wireframes on JPanel, so that i can use netbeans to position a JPanel any were in the frame i want then just assin that to my Java3D JPanel..
can some one help provide me with simple code that will create a 3D univers like the canvas 3D but on JPanel?
my aim is port this code to show on JPanel , no applet .. just JPanel.
import java.awt.Frame;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class lesson03 extends Applet {
SimpleUniverse simpleU;
public Appearance getWireframeAppearance() {
Appearance app = new Appearance();
Color awtColor = new Color(255, 255, 0);// use any Color you like
Color3f color = new Color3f(awtColor);
ColoringAttributes ca = new ColoringAttributes();
ca.setColor(color);
app.setColoringAttributes(ca);
PolygonAttributes pa = new PolygonAttributes();
pa.setPolygonMode(pa.POLYGON_LINE);
pa.setCullFace(pa.CULL_NONE);
app.setPolygonAttributes(pa);
return app;
}
public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
Appearance polygon1Appearance = getWireframeAppearance();
QuadArray polygon1 = new QuadArray (4, QuadArray.COORDINATES);
polygon1.setCoordinate (0, new Point3f (0f, 0f, 0f));
polygon1.setCoordinate (1, new Point3f (2f, 2f, -1f));
//polygon1.setCoordinate (2, new Point3f (2f, 3f, 0f));
//polygon1.setCoordinate (3, new Point3f (0f, 3f, 0f));
objRoot.addChild(new Shape3D(polygon1,polygon1Appearance));
return objRoot;
}
public lesson03 (){
}
public void init() {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add("Center", c);
BranchGroup scene = createSceneGraph();
simpleU = new SimpleUniverse(c);
TransformGroup tg = simpleU.getViewingPlatform().getViewPlatformTransform();
Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3f(0f,0f,10f));
tg.setTransform(t3d);
scene.compile();
simpleU.addBranchGraph(scene);
}
public void destroy(){
simpleU.removeAllLocales();
}
public static void main(String[] args) {
Frame frame = new MainFrame(new lesson03(), 500, 500);
}
}
im still expermineting and learning java3D and i want to use this code and minimize it "strip it of all applet stuff" and just have it show on JPanel so that i can place it in swing JFrame and some controls "buttons and textfields" in netbeans to experiment with coordinate drawing and so on..
peace
add the Canvas3D to a JFrame.
//returns a JFrame centered onscreen
public static JFrame getGuiInstance(String TITLE, int GUI_WIDTH,
int GUI_HEIGHT) {
JFrame frame = new JFrame(TITLE);
ImageIcon icon = new ImageIcon("the path to your imageIcon");
frame.setIconImage(icon.getImage()); //new
frame.setTitle(TITLE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width / 2) - (GUI_WIDTH / 2); //- GUI_WIDTH;
int y = ( d.height / 2 ) - (GUI_HEIGHT / 2);
frame.setSize(new Dimension(GUI_WIDTH, GUI_HEIGHT));
frame.setLocation(x, y);
return frame;
}
JFrame frame = getGuiInstance("my title", intWIDTH, intHEIGHT);
frame.setLayout(new BorderLayout());
frame.add( myCanvas3D, BorderLayout.CENTER);//add the canvas#D to the Frame
regards
import java.awt.*;
import com.sun.j3d.utils.geometry.Text2D;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.vecmath.*;
public class Text2DTest extends JApplet{
public void init(){
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
Text2DTest1 text = new Text2DTest1();
text.init();
panel2.add(text);
JButton button = new JButton("Testing");
panel3.add(button);
panel1.add(panel2);
panel1.add(panel3);
add(panel1);
}
}
class Text2DTest1 extends JPanel{
private SimpleUniverse u = null;
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create a Transformgroup to scale all objects so they
// appear in the scene.
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.4);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
TransformGroup textTranslationGroup;
Transform3D textTranslation;
float yPos = -.5f;
Shape3D textObject = new Text2D("Rotating Yellow Text",
new Color3f(1f, 1f, 0f),
"Serif",
60,
Font.BOLD);
Appearance app = textObject.getAppearance();
PolygonAttributes pa = app.getPolygonAttributes();
if (pa == null)
pa = new PolygonAttributes();
pa.setCullFace(PolygonAttributes.CULL_NONE);
if (app.getPolygonAttributes() == null)
app.setPolygonAttributes(pa);
objTrans.addChild(textObject);
textTranslation = new Transform3D();
textTranslation.setTranslation(new Vector3f(0f, yPos, 0f));
textTranslationGroup = new TransformGroup(textTranslation);
textTranslationGroup.addChild(objTrans);
objScale.addChild(textTranslationGroup);
return objRoot;
}
public Text2DTest1() {
}
public void init() {
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph();
u = new SimpleUniverse(c);
// Have Java 3D perform optimizations on this scene graph.
scene.compile();
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
}
Hello, i am working on a program using Java3D and Swing.
the one i want to ask is how to add canvas3D into JApplet that have more than one JPanels.
Thanks