public static class FigureTutorial.CustomRectangle
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.
public CustomRectangle(double x,
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.
x - a double
y - a double
width - a double
height - a double
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
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
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.
g - The 2D graphics object that this object
it to be painted upon.
public void transform(java.awt.geom.AffineTransform at)
Transform the object. There are various ways of doing this,
some more complicated and some even more 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.