Package diva.canvas.tutorial
Class FigureTutorial.CustomRectangle
- java.lang.Object
-
- diva.canvas.AbstractFigure
-
- 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.
-
-
Constructor Summary
Constructors Constructor Description CustomRectangle(double x, double y, double width, double height)
Create a new instance of this figure.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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
-
-
-
-
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 doubley
- a doublewidth
- a doubleheight
- 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 interfaceFigure
- Overrides:
getBounds
in classAbstractFigure
- 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 interfaceFigure
- Specified by:
getShape
in classAbstractFigure
- 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 interfaceVisibleComponent
- Specified by:
paint
in classAbstractFigure
- 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 interfaceFigure
- Specified by:
transform
in classAbstractFigure
- Parameters:
at
- The transform to be used.
-
-