A graphics class for viewing scenes using Java3D.
All the
Shape-subclasses specified in the
edu.geom3D
package can be added to a
J3DScene object and are automatically
painted on a
Canvas3D object. For
instance the following code creates a scene with a cylinder and a red
transparent box and adds the canvas to a frame.
J3DScene scene = new J3DScene();
scene.addShape( new Cylinder(new Vector(1,0,0), new Vector(0.5,0.5, 0.3), 0.1f) );
Vector boxCorner = new Vector(-1,0,0);
Vector[] boxBases = {new Vector(1,0,0), new Vector(0,1,0), new Vector(0,0,1)};
float[] boxExtents = {0.8f, 1, 2};
Box box = new Box( boxCorner, boxBases, boxExtents );
scene.addShape( box, new Color(200,0,0,100) );
Canvas3D canvas = scene.getCanvas();
JFrame frame = new JFrame();
frame.setSize(400,400);
frame.getContentPane().add( canvas );
frame.setVisible(true);
Text can be added to the scene as well and will always face the camera.
The
repaint() method must be called every time the position of
shapes has changed and the canvas should be updated. The pointers
to added shapes are stored, so subsequent changes in the
box
object in the above code will be visible on the canvas when
repaint()
is called. The following example shows how to animate a sphere rotating around origo.
J3DScene scene = new J3DScene();
Sphere sphere = new Sphere( new Vector(1,0,0), 0.1f);
scene.addShape(sphere);
float t = 0;
while(true){
t+=0.01f;
sphere.center = new Vector(Math.cos(t), Math.sin(t), 0);
scene.repaint();
try{ Thread.sleep(30); }catch(InterruptedException exc){}
}
A static method is supplied for conveniently creating a frame containing a scene-viewer.
The following example shows how to quickly create a
J3DScene object
that is shown in a frame and ready for use:
J3DScene scene = J3DScene.createJ3DSceneInFrame();
scene.setAxisEnabled(true);
scene.addShape( new Cylinder(new Vector(1,0,0), new Vector0,1,0), 0.1f) );