diva.canvas.tutorial
Class FigureTutorial.CustomRectangle

java.lang.Object
  extended by diva.canvas.AbstractFigure
      extended by diva.canvas.tutorial.FigureTutorial.CustomRectangle
All Implemented Interfaces:
CanvasComponent, Figure, VisibleComponent, UserObjectContainer
Enclosing class:
FigureTutorial

public static class FigureTutorial.CustomRectangle
extends AbstractFigure

CustomRectangle is a class that paints itself as a rectangle and draw a red plus sign over the top of itself. This example figure class illustrates the use of different paints and strokes to create the required image. It overrides only the absolute minimum number of methods that must be overridden to create a new figure class.


Field Summary
private  java.awt.geom.Rectangle2D _bounds
           
 
Constructor Summary
FigureTutorial.CustomRectangle(double x, double y, double width, double height)
          Create a new instance of this figure.
 
Method Summary
 java.awt.geom.Rectangle2D getBounds()
          Get the bounds of this figure.
 java.awt.Shape getShape()
          Get the shape of this figure.
 void paint(java.awt.Graphics2D g)
          Paint this figure onto the given graphics context.
 void transform(java.awt.geom.AffineTransform at)
          Transform the object.
 
Methods inherited from class diva.canvas.AbstractFigure
contains, getInteractor, getLayer, getOrigin, getParent, getToolTipText, getTransformContext, getUserObject, hit, intersects, isVisible, paint, repaint, repaint, setInteractor, setParent, setToolTipText, setUserObject, setVisible, translate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_bounds

private java.awt.geom.Rectangle2D _bounds
Constructor Detail

FigureTutorial.CustomRectangle

public FigureTutorial.CustomRectangle(double x,
                                      double y,
                                      double width,
                                      double height)
Create a new instance of this figure. All we do here is take the coordinates that we have been given and remember them as a rectangle. In general, we may want several constructors, and methods to set and get fields that will control the visual properties of the figure.

Parameters:
x - a double
y - a double
width - a double
height - a double
Method Detail

getBounds

public java.awt.geom.Rectangle2D getBounds()
Get the bounds of this figure. Because, in this example, we have stroked the outline of the rectangle, we have to create a new rectangle that is the bounds of the outside of that stroke. In this method the stroke object is being created each time, but it would normally be created only once.

Specified by:
getBounds in interface Figure
Overrides:
getBounds in class AbstractFigure

getShape

public java.awt.Shape getShape()
Get the shape of this figure. In this example, it's just the bounding rectangle that we stored in the constructor. Note that in general, figures assume that clients will not modify the object returned by this method.

Specified by:
getShape in interface Figure
Specified by:
getShape in class AbstractFigure

paint

public void paint(java.awt.Graphics2D g)
Paint this figure onto the given graphics context. We first paint the rectangle blue with a stroke width of 1 unit, and then the plus sign with a stroke width of 4 units. The implementation is fairly inefficient: in general, we would want to cache the Stroke objects. Note that we have to set the stroke and paint in the graphics context before we can do anything useful.

Specified by:
paint in interface VisibleComponent
Specified by:
paint in class AbstractFigure

transform

public void transform(java.awt.geom.AffineTransform at)
Transform the object. There are various ways of doing this, some more complicated and some even morer complicated... In this example, we use a utility function in the class diva.canvas.CanvasUtils to transform the bounding box. Both before and after we do the transformation, we have to call the repaint() method so that the region of the canvas that changed is properly repainted.

Specified by:
transform in interface Figure
Specified by:
transform in class AbstractFigure