ProGAL.geom3d.viewer
Class J3DScene

java.lang.Object
  extended by ProGAL.geom3d.viewer.J3DScene

public class J3DScene
extends java.lang.Object

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) );
 

Author:
R. Fonseca

Field Summary
 javax.swing.JFrame frame
           
 
Constructor Summary
J3DScene()
           
 
Method Summary
 void addClickListener(ClickListener cl)
          Add a click-listener that gets called every time an object or the background is clicked
 void addShape(Shape v)
          Add a volume object.
 void addShape(Shape v, java.awt.Color c)
          Add a volume object with a specified color
 void addShape(Shape v, java.awt.Color c, int divisions)
          Add a volume object with a specified color and detail-level
 void addSurface(ParametricSurface surface)
           
 void addSurface(ParametricSurface surface, java.awt.Color col)
           
 void addSurface(ParametricSurface surface, java.awt.Color col, double uMin, double uMax, int uDivs, double vMin, double vMax, int vDivs)
           
 void addText(java.lang.String t, Point pos)
          Add a text-object at the specified position.
 void addText(java.lang.String t, Point pos, double height)
           
 void addText(java.lang.String t, Point pos, double height, java.awt.Color c)
           
 void autoZoom()
          Zooms such that the maximal distance between two objects is within the view
 void centerCamera()
          Sets the location that the camera looks at to the center of all the shapes added to the scene.
 void centerCamera(Point newCenter)
           
static J3DScene createJ3DSceneInFrame()
          Create a frame containing a canvas, display it and return the J3DScene object shown in the frame.
 Camera getCamera()
           
 javax.media.j3d.Canvas3D getCanvas()
          Get the canvas that displays the scene.
 java.util.List<ClickListener> getClickListeners()
           
static void main(java.lang.String[] args)
           
 void removeAllShapes()
          Remove all volumes from the scene.
 void removeShape(Shape v)
          Removes one volume from the scene.
 void repaint()
          Repaint the canvas.
 void repaintRepeatedly(long millisecondDelay)
          Repaint the canvas repeatedly every millisecondDelay milliseconds.
 void setAntialiasing(boolean enabled)
           
 void setAxisEnabled(boolean axisEnabled)
          Enables or disables xyz-axis from the origo
 void setBackgroundColor(java.awt.Color c)
          Set color of background.
 void setParallelProjection(boolean enabled)
          Enables and disables parallel projection (as opposed to perspective projection).
 void toggleRotation()
          Toggles rotation
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

frame

public javax.swing.JFrame frame
Constructor Detail

J3DScene

public J3DScene()
Method Detail

setBackgroundColor

public void setBackgroundColor(java.awt.Color c)
Set color of background.


removeShape

public void removeShape(Shape v)
Removes one volume from the scene.


removeAllShapes

public void removeAllShapes()
Remove all volumes from the scene.


addShape

public void addShape(Shape v)
Add a volume object. The standard color gray will be used


addShape

public void addShape(Shape v,
                     java.awt.Color c)
Add a volume object with a specified color


addShape

public void addShape(Shape v,
                     java.awt.Color c,
                     int divisions)
Add a volume object with a specified color and detail-level


addText

public void addText(java.lang.String t,
                    Point pos)
Add a text-object at the specified position.


addText

public void addText(java.lang.String t,
                    Point pos,
                    double height)

addText

public void addText(java.lang.String t,
                    Point pos,
                    double height,
                    java.awt.Color c)

addSurface

public void addSurface(ParametricSurface surface)

addSurface

public void addSurface(ParametricSurface surface,
                       java.awt.Color col)

addSurface

public void addSurface(ParametricSurface surface,
                       java.awt.Color col,
                       double uMin,
                       double uMax,
                       int uDivs,
                       double vMin,
                       double vMax,
                       int vDivs)

centerCamera

public void centerCamera()
Sets the location that the camera looks at to the center of all the shapes added to the scene.


centerCamera

public void centerCamera(Point newCenter)

autoZoom

public void autoZoom()
Zooms such that the maximal distance between two objects is within the view


setParallelProjection

public void setParallelProjection(boolean enabled)
Enables and disables parallel projection (as opposed to perspective projection).


setAntialiasing

public void setAntialiasing(boolean enabled)

toggleRotation

public void toggleRotation()
Toggles rotation


addClickListener

public void addClickListener(ClickListener cl)
Add a click-listener that gets called every time an object or the background is clicked

Parameters:
cl -

getClickListeners

public java.util.List<ClickListener> getClickListeners()

setAxisEnabled

public void setAxisEnabled(boolean axisEnabled)
Enables or disables xyz-axis from the origo


getCamera

public Camera getCamera()

getCanvas

public javax.media.j3d.Canvas3D getCanvas()
Get the canvas that displays the scene. If this method is called several times the same Canvas3D object will be returned every time.


repaint

public void repaint()
Repaint the canvas. If the scene has been changed in any way the scene displayer will update the view when repaint() is called and no sooner. If the scene is repeatedly changed, and repaint repeatedly called the viewer will show an animation.


repaintRepeatedly

public void repaintRepeatedly(long millisecondDelay)
Repaint the canvas repeatedly every millisecondDelay milliseconds.


createJ3DSceneInFrame

public static J3DScene createJ3DSceneInFrame()
Create a frame containing a canvas, display it and return the J3DScene object shown in the frame. The frame can be retrieved using the J3DScene.frame field.


main

public static void main(java.lang.String[] args)