diva.canvas.tutorial
Class TransformedFigureTutorial.CloudFigure

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

public static class TransformedFigureTutorial.CloudFigure
extends AbstractFigure

CloudFigure is a class that paints itself as a translucent "cloud." This example figure class illustrates the use of different paints and strokes to create the required image, and the use of TransformContext to position that image.


Field Summary
private  java.awt.geom.Rectangle2D _cachedBounds
           
private  java.awt.Shape _cachedShape
           
private  java.awt.Shape _cloud1
           
private  java.awt.Shape _cloud2
           
private  java.awt.Shape _shape
           
private  TransformContext _transformContext
           
 
Constructor Summary
TransformedFigureTutorial.CloudFigure(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.
 TransformContext getTransformContext()
          Get the transform context.
 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, 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

_shape

private java.awt.Shape _shape

_cloud1

private java.awt.Shape _cloud1

_cloud2

private java.awt.Shape _cloud2

_transformContext

private TransformContext _transformContext

_cachedBounds

private java.awt.geom.Rectangle2D _cachedBounds

_cachedShape

private java.awt.Shape _cachedShape
Constructor Detail

TransformedFigureTutorial.CloudFigure

public TransformedFigureTutorial.CloudFigure(double x,
                                             double y,
                                             double width,
                                             double height)
Create a new instance of this figure. The cloud is initially created at coordinates (0,0) and then transformed to the requested coordinates. To create the cloud shape, use the Area class in Java2D, which implements constructive area geometry, and join a bunch of circles into a single shape.

Method Detail

getBounds

public java.awt.geom.Rectangle2D getBounds()
Get the bounds of this figure. Because this figure has its own transform, we need to transform the internal bounds into the enclosing context. To make this more efficient, we use a previously-cached copy of the transformed bounds if there is one.

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

getShape

public java.awt.Shape getShape()
Get the shape of this figure. Because this figure has its own transform, we need to transform the internal shape into the enclosing context. To make this more efficient, we use a previously-cached copy of the transformed shape if there is one.

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

getTransformContext

public TransformContext getTransformContext()
Get the transform context. This method must be overridden since this figure defined its own context.

Specified by:
getTransformContext in interface CanvasComponent
Overrides:
getTransformContext in class AbstractFigure

paint

public void paint(java.awt.Graphics2D g)
Paint this figure onto the given graphics context. First we "push" the transform context onto the transform stack, so that the graphics port has the correct transform. Then we paint the cloud a translucent magenta (yum!), and then we make a couple of little clouds and paint them opaque. (The way this is done in this example is horrendously inefficient.) Finally, we "pop" the transform context off the stack.

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

transform

public void transform(java.awt.geom.AffineTransform at)
Transform the object. In this example, we pre-concatenate the given transform with the transform in the transform context. When the figure is repainted, it will be redrawn in the right place. We also must be sure to invalidate the cached geometry objects that depend on the transform.

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