ProGAL.geom2d.viewer
Class J2DScene

java.lang.Object
  extended by ProGAL.geom2d.viewer.J2DScene

public class J2DScene
extends java.lang.Object

A graphics class for viewing 2D scenes. Many of the Shape-subclasses specified in the ProGAL.geom2d package can be added to a J2DScene object and are automatically painted. For instance the following code creates a scene with 3 circles, two lines indicating unit axis, and a text label.

 J2DScene scene = new J2DScene();
 scene.addShape(new LineSegment(new Point(0,0), new Point(1,0)), Color.BLACK);
 scene.addShape(new LineSegment(new Point(0,0), new Point(0,1)), Color.BLACK);
 scene.addShape(new Circle(new Point(0,0), 1), new Color(250,0,0,100), 0.05, false);
 scene.addShape(new Circle(new Point(1,0), 0.2), Color.GREEN);
 scene.addShape(new Circle(new Point(0,1), 0.2), Color.BLUE);
 scene.addShape(new TextShape("(1,0)", new Point(1,0), 0.2), Color.BLACK);
 
 JPanel canvas = scene.getCanvas();
 
 JFrame frame = new JFrame();
 frame.setSize(400,400);
 frame.getContentPane().add( canvas );
 frame.setVisible(true);
 
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 will be visible on the canvas when repaint() is called. The following example shows how to animate a circle rotating around origo.
 J2DScene scene = J2DScene.createJ2DSceneInFrame();
 scene.addShape(new LineSegment(new Point(0,0), new Point(1,0)), Color.BLACK);
 scene.addShape(new LineSegment(new Point(0,0), new Point(0,1)), Color.BLACK);
 Circle c = new Circle(new Point(0,1), 0.2);
 scene.addShape(c, Color.BLUE);
 
 double t = 0;
 while(true){
        c.center().setCoord(0, Math.cos(t));
        c.center().setCoord(1, Math.sin(t));
        scene.repaint();
        t+=0.01;
        try {Thread.sleep(50);} catch (InterruptedException e) {}
 }
 
As shown in this example, a static method is supplied for conveniently creating a frame containing a scene-viewer. Shapes are painted in the order they are inserted. The addShape method has support for border width and filling out a shape.

Author:
R. Fonseca

Field Summary
 javax.swing.JFrame frame
           
 
Constructor Summary
J2DScene()
          Construct a representation of a 2D scene.
 
Method Summary
 void addShape(Shape s)
          Add a shape to this scene.
 void addShape(Shape s, java.awt.Color c)
          Add a shape to this scene with the specified color.
 void addShape(Shape s, java.awt.Color c, double border)
           
 void addShape(Shape s, java.awt.Color c, double border, boolean fill)
           
 void autoZoom()
          Zoom the view to enclose all the objects in the scene.
 void centerCamera()
          Center the view on the objects in the scene
static J2DScene createJ2DSceneInFrame()
          Create a frame containing a canvas, display it and return the J2DScene object shown in the frame.
 javax.swing.JPanel getCanvas()
          Return the panel that this scene is painted on.
static void main(java.lang.String[] args)
           
 void removeAllShapes()
          Remove all shapes from the scene
 void removeShape(Shape s)
          Remove the specified shape from the scene
 void repaint()
          Repaint the scene
 
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

J2DScene

public J2DScene()
Construct a representation of a 2D scene.

Method Detail

getCanvas

public javax.swing.JPanel getCanvas()
Return the panel that this scene is painted on.


centerCamera

public void centerCamera()
Center the view on the objects in the scene


autoZoom

public void autoZoom()
Zoom the view to enclose all the objects in the scene.


addShape

public void addShape(Shape s)
Add a shape to this scene. Currently, are supported.


addShape

public void addShape(Shape s,
                     java.awt.Color c)
Add a shape to this scene with the specified color. Currently, are supported.


addShape

public void addShape(Shape s,
                     java.awt.Color c,
                     double border)

addShape

public void addShape(Shape s,
                     java.awt.Color c,
                     double border,
                     boolean fill)

removeShape

public void removeShape(Shape s)
Remove the specified shape from the scene


removeAllShapes

public void removeAllShapes()
Remove all shapes from the scene


repaint

public void repaint()
Repaint the scene


createJ2DSceneInFrame

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


main

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