public class J2DScene
extends java.lang.Object
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.Modifier and Type | Field and Description |
---|---|
javax.swing.JFrame |
frame |
Constructor and Description |
---|
J2DScene()
Construct a representation of a 2D scene.
|
Modifier and Type | Method and Description |
---|---|
void |
addClickListener(ClickListener cl)
Add a click-listener that gets called every time an object or the background is clicked
|
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
|
void |
savePng(java.lang.String fName) |
java.awt.Point |
transformPoint(Point p) |
Point |
transformPoint(java.awt.Point p) |
public javax.swing.JPanel getCanvas()
public void centerCamera()
public void autoZoom()
public void addShape(Shape s)
ProGAL.geom2d.Circle
ProGAL.geom2d.LineSegment
and ProGAL.geom2d.viewer.TextShape
ProGAL.geom2d.LSC
public void addShape(Shape s, java.awt.Color c)
ProGAL.geom2d.Circle
ProGAL.geom2d.LineSegment
and ProGAL.geom2d.viewer.TextShape
ProGAL.geom2d.LSC
public void addShape(Shape s, java.awt.Color c, double border)
public void addShape(Shape s, java.awt.Color c, double border, boolean fill)
public void savePng(java.lang.String fName)
public void removeShape(Shape s)
public void removeAllShapes()
public void addClickListener(ClickListener cl)
public void repaint()
public java.awt.Point transformPoint(Point p)
public Point transformPoint(java.awt.Point p)
public static J2DScene createJ2DSceneInFrame()
J2DScene.frame
field.public static void main(java.lang.String[] args)