com.flagstone.transform.util.shape
Class Canvas

java.lang.Object
  extended by com.flagstone.transform.util.shape.Canvas

public final class Canvas
extends Object

The Canvas class is used to create shape definitions. Arbitrary paths can be created using a series of move, line or curve segments. Drawing operations using both absolute coordinates and coordinates relative to the current point (updated after every operation) are supported.

For curves both cubic and quadratic curves are supported. Flash only supports quadratic curves so cubic curves are approximated by a series of line segments using (converting cubic to quadratic curves is mathematically difficult). The smoothness of cubic curves is controlled by the flatness attribute which can be used to limit the number of line segments that are drawn.

As a path is drawn the maximum and minimum x and y coordinates are recorded so that the bounding rectangle that completely encloses the shape can be defined. This is used when creating shape definitions using the DefineShape, DefineShape2 or DefineShape3 classes.

When drawing paths whether coordinates are specified in twips or pixels is set when the Canvas object is created. When specifying coordinates in pixels all coordinates are converted internally to twips to perform the actual drawing.


Constructor Summary
Canvas()
          Creates a new Canvas object with no path defined.
 
Method Summary
 void clear()
          Creates a new path, discarding any path elements drawn.
 void close()
          Closes the current path by drawing a line from the current point to the starting point of the path.
 void curve(int acontrolX, int acontrolY, int aanchorX, int aanchorY)
          Draw a quadratic bezier curve from the current point to the point (x,y) with the control point (x1, y1).
 void curve(int cax, int cay, int cbx, int cby, int anx, int any)
          Draw a cubic bezier curve from the current point to the point (x,y) with the off-curve control points (x1, y1) and (x2, y2).
 DefineShape2 defineShape(int identifier)
          Generates a shape containing the current path and styles.
 DefineShape3 defineTransparentShape(int identifier)
          Generates a transparent shape containing the current path and styles.
 Bounds getBounds()
          Generates the bounding box that encloses the current path.
 List<FillStyle> getFillStyles()
          Get a copy of the list of fill styles.
 List<LineStyle> getLineStyles()
          Get a copy of the list of line styles.
 Shape getShape()
          Returns the Shape object containing the objects used to draw the current path.
 boolean isPixels()
          Are the coordinates used when drawing a path are expressed in pixels (true) or twips (false).
 void line(int xCoord, int yCoord)
          draw a line from the current point to the point (x,y).
 void move(int xCoord, int yCoord)
          Move to the point (x,y).
 void moveForFont(int xCoord, int yCoord)
          Move to the point (x,y).
 void polygon(int[] points)
          Draws a closed shape with vertices defines by pairs of coordinates from the array argument.
 void rcurve(int rcontrolX, int rcontrolY, int ranchorX, int ranchorY)
          Draw a quadratic bezier curve relative to the current point to the point.
 void rcurve(int controlAX, int controlAY, int controlBX, int controlBY, int anchorX, int anchorY)
          Draw a cubic bezier curve relative to the current point.
 void reflect(int xCoord, int yCoord)
          Draw a quadratic bezier curve from the current point to the point (x,y) using the control point for the previously drawn curve.
 void reflect(int ctrlX, int ctrlY, int anchorX, int anchorY)
          Draw a cubic bezier curve from the current point to the point (x,y).
 void rline(int xCoord, int yCoord)
          Draw a line relative to the current point.
 void rmove(int xCoord, int yCoord)
          Move relative to the current point.
 void rpolygon(int[] points)
          Draws a closed shape with vertices defines by pairs of coordinates from the array argument.
 void rreflect(int xCoord, int yCoord)
          Draw a quadratic bezier curve relative to the current point to the point using the control point for the previously drawn curve.
 void rreflect(int ctrlX, int ctrlY, int anchorX, int anchorY)
          Draw a cubic bezier curve relative to the current point.
 void setAltStyle(FillStyle style)
          Set the style used to fill overlapping enclosed areas.
 void setFillStyle(FillStyle style)
          Set the style used to fill enclosed areas.
 void setLineStyle(LineStyle1 style)
          Set the style used to draw lines.
 void setPixels(boolean arePixels)
          Sets whether the coordinates used when drawing a path are expressed in pixels (true) or twips (false).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Canvas

public Canvas()
Creates a new Canvas object with no path defined.

Method Detail

isPixels

public boolean isPixels()
Are the coordinates used when drawing a path are expressed in pixels (true) or twips (false).

Returns:
true if coordinates are expressed in pixels, false if they are twips.

setPixels

public void setPixels(boolean arePixels)
Sets whether the coordinates used when drawing a path are expressed in pixels (true) or twips (false). Flash coordinates are specified in twips (1 twip equals 1/1440th of an inch or 1/20th of a point). Allowing coordinates to be specified in pixels simplifies the drawing process avoiding the conversion to twips by multiplying each value by 20.

Parameters:
arePixels - true if coordinates are expressed in pixels, false if they are twips.

getBounds

public Bounds getBounds()
Generates the bounding box that encloses the current path.

Returns:
the bounding box that encloses the current shape.

getLineStyles

public List<LineStyle> getLineStyles()
Get a copy of the list of line styles.

Returns:
the list of line styles.

getFillStyles

public List<FillStyle> getFillStyles()
Get a copy of the list of fill styles.

Returns:
the list of fill styles.

getShape

public Shape getShape()
Returns the Shape object containing the objects used to draw the current path.

Returns:
an Shape object contain the Line, Curve and ShapeStyle objects used to construct the current path.

setLineStyle

public void setLineStyle(LineStyle1 style)
Set the style used to draw lines.

Parameters:
style - a line style.

setFillStyle

public void setFillStyle(FillStyle style)
Set the style used to fill enclosed areas.

Parameters:
style - a fill style.

setAltStyle

public void setAltStyle(FillStyle style)
Set the style used to fill overlapping enclosed areas.

Parameters:
style - a fill style.

defineShape

public DefineShape2 defineShape(int identifier)
Generates a shape containing the current path and styles. The shape is constructed with copies of the line and fill styles and the shape representing the path drawn. This allows the number of styles to be changed without affecting previously created shapes.

Parameters:
identifier - an unique identifier for the shape.
Returns:
this object.

defineTransparentShape

public DefineShape3 defineTransparentShape(int identifier)
Generates a transparent shape containing the current path and styles. The shape is constructed with copies of the line and fille styles and the shape representing the path drawn. This allows the number of styles to be changed without affecting previously created shapes.

Parameters:
identifier - an unique identifier for the shape.
Returns:
this object.

clear

public void clear()
Creates a new path, discarding any path elements drawn.


close

public void close()
Closes the current path by drawing a line from the current point to the starting point of the path.


move

public void move(int xCoord,
                 int yCoord)
Move to the point (x,y).

Parameters:
xCoord - the x-coordinate of the point to move to.
yCoord - the y-coordinate of the point to move to.

moveForFont

public void moveForFont(int xCoord,
                        int yCoord)
Move to the point (x,y). Use only when creating font definitions.

Parameters:
xCoord - the x-coordinate of the point to move to.
yCoord - the y-coordinate of the point to move to.

rmove

public void rmove(int xCoord,
                  int yCoord)
Move relative to the current point.

Parameters:
xCoord - the distance along the x-axis.
yCoord - the distance along the y-axis.

line

public void line(int xCoord,
                 int yCoord)
draw a line from the current point to the point (x,y).

Parameters:
xCoord - the x-coordinate of the end of the line.
yCoord - the y-coordinate of the end of the line.

rline

public void rline(int xCoord,
                  int yCoord)
Draw a line relative to the current point.

Parameters:
xCoord - the distance along the x-axis to the end of the line.
yCoord - the distance along the y-axis to the end of the line.

curve

public void curve(int acontrolX,
                  int acontrolY,
                  int aanchorX,
                  int aanchorY)
Draw a quadratic bezier curve from the current point to the point (x,y) with the control point (x1, y1).

Parameters:
acontrolX - the x-coordinate of the control point.
acontrolY - the y-coordinate of the control point.
aanchorX - the x-coordinate of the end of the curve.
aanchorY - the y-coordinate of the end of the curve.

rcurve

public void rcurve(int rcontrolX,
                   int rcontrolY,
                   int ranchorX,
                   int ranchorY)
Draw a quadratic bezier curve relative to the current point to the point.

Parameters:
rcontrolX - the distance along the x-axis from the current point to the control point.
rcontrolY - the distance along the y-axis from the current point to the control point.
ranchorX - the distance along the x-axis from the current point to the end of the curve.
ranchorY - the distance along the y-axis from the current point to the end of the curve.

curve

public void curve(int cax,
                  int cay,
                  int cbx,
                  int cby,
                  int anx,
                  int any)
Draw a cubic bezier curve from the current point to the point (x,y) with the off-curve control points (x1, y1) and (x2, y2). IMPORTANT: Converting cubic bezier curves to the quadratic bezier curves supported by Flash is mathematically difficult. The cubic curve is approximated by a series of straight line segments.

Parameters:
cax - the x-coordinate of the first control point.
cay - the y-coordinate of the first control point.
cbx - the x-coordinate of the second control point.
cby - the y-coordinate of the second control point.
anx - the x-coordinate of the end of the curve.
any - the y-coordinate of the end of the curve.

rcurve

public void rcurve(int controlAX,
                   int controlAY,
                   int controlBX,
                   int controlBY,
                   int anchorX,
                   int anchorY)
Draw a cubic bezier curve relative to the current point. IMPORTANT: Converting cubic bezier curves to the quadratic bezier curves supported by Flash is mathematically difficult. The cubic curve is approximated by a series of straight line segments.

Parameters:
controlAX - the distance along the x-axis from the current point to the first control point.
controlAY - the distance along the y-axis from the current point to the first control point.
controlBX - the distance along the x-axis from the current point to the second control point.
controlBY - the distance along the y-axis from the current point to the second control point.
anchorX - the distance along the x-axis from the current point to the end of the curve.
anchorY - the distance along the y-axis from the current point to the end of the curve.

reflect

public void reflect(int xCoord,
                    int yCoord)
Draw a quadratic bezier curve from the current point to the point (x,y) using the control point for the previously drawn curve. If no curve has been drawn previously then a control point midway along the previous line or move is used.

Parameters:
xCoord - the x-coordinate of the end of the curve.
yCoord - the y-coordinate of the end of the curve.

rreflect

public void rreflect(int xCoord,
                     int yCoord)
Draw a quadratic bezier curve relative to the current point to the point using the control point for the previously drawn curve. If no curve has been drawn previously then a control point midway along the previous line or move is used.

Parameters:
xCoord - the distance along the x-axis from the current point to the end of the curve.
yCoord - the distance along the y-axis from the current point to the end of the curve.

reflect

public void reflect(int ctrlX,
                    int ctrlY,
                    int anchorX,
                    int anchorY)
Draw a cubic bezier curve from the current point to the point (x,y). The first control point is the one defined for the previously drawn curve. The second control point is the coordinates (x2, y2). If no curve has been drawn previously then a control point midway along the previous line or move is used.

Parameters:
ctrlX - the x-coordinate of the control point.
ctrlY - the y-coordinate of the control point.
anchorX - the x-coordinate of the end of the curve.
anchorY - the y-coordinate of the end of the curve.

rreflect

public void rreflect(int ctrlX,
                     int ctrlY,
                     int anchorX,
                     int anchorY)
Draw a cubic bezier curve relative to the current point. The first control point is the one defined for the previously drawn curve. The second control point is the relative point (x2, y2). If no curve has been drawn previously then a control point midway along the previous line or move is used.

Parameters:
ctrlX - the distance along the x-axis from the current point to the second control point.
ctrlY - the distance along the y-axis from the current point to the second control point.
anchorX - the distance along the x-axis from the current point to the end of the curve.
anchorY - the distance along the y-axis from the current point to the end of the curve.

rpolygon

public void rpolygon(int[] points)
Draws a closed shape with vertices defines by pairs of coordinates from the array argument. The first pair of points in the array specifies a move. Line segments a drawn relative to the current point which is updated after each segment is drawn. If the number of points is an odd number then the last point will be ignored.

Parameters:
points - and array of coordinate pairs. The first pair of points defines the coordinates of a move operation, successive pairs define the coordinates for relative lines.

polygon

public void polygon(int[] points)
Draws a closed shape with vertices defines by pairs of coordinates from the array argument. The first pair of points in the array specifies a move. Line segments a drawn using absolute coordinates. The current point which is updated after each segment is drawn. If the number of points is an odd number then the last point will be ignored.

Parameters:
points - and array of coordinate pairs. The first pair of points defines the coordinates of a move operation, successive pairs define the coordinates of the lines.


Copyright © 2002-2010 Flagstone Software Ltd.. All Rights Reserved.