Class 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.
    • Constructor Detail

      • CustomRectangle

        public 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
        Returns:
        the bounding box of this figure.
      • 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
        Returns:
        the outline shape of this figure
      • 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
        Parameters:
        g - The 2D graphics object that this object it to be painted upon.
      • 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 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.
        Specified by:
        transform in interface Figure
        Specified by:
        transform in class AbstractFigure
        Parameters:
        at - The transform to be used.