com.flagstone.transform
Class FSMorphGradientFill

java.lang.Object
  extended by com.flagstone.transform.FSTransformObject
      extended by com.flagstone.transform.FSFillStyle
          extended by com.flagstone.transform.FSMorphGradientFill
All Implemented Interfaces:
java.lang.Cloneable

public class FSMorphGradientFill
extends FSFillStyle

FSMorphGradientFill defines how a colour changes across an area filled in a morphing shape.

Two type of gradient fill are supported:

Linear The gradient changes in one direction across the area to be filled.
Radial The gradient changes radially from the centre of the area to be filled.

Gradients are defined in terms of a standard space called the gradient square, centred at (0,0) and extending from (-16384, -16384) to (16384, 16384).

A coordinate transform is required to map the gradient square to the coordinates of the filled area. The transformation is applied in two steps. First the gradient square is scaled so the colour covers the shape followed by a translation to map the gradient square coordinates to the coordinate range of the shape.

A series of control points is used to control how the colour displayed changes across the gradient. At least two control points are required to define a gradient - one for the starting colour and one for the final colour. Up to eight control points may be specified to generate complex colour transitions. When the Flash Player displays the control points they are sorted by the ratio defined in each FSGradient object, with the smallest ratio value displayed first.

Attributes
type The type of gradient fill, either FSFillStyle.Linear or FSFillStyle.Radial.
startTransform The coordinate transform used to map the logical coordinates of the gradient square on to a set of physical coordinates at the start of the morphing process.
endTransform The coordinate transform used to map the logical coordinates of the gradient square on to a set of physical coordinates at the end of the morphing process.
gradients An array of up to eight FSGradient objects that define the control points across the gradient square that control the colour transitions. The Flash Player performs the interpolation of the colour values between the control points. Alpha channel (defining transparency) must be defined for the colour in each control point.

In addition to specifying the type of gradient fill that will be displayed the type attribute may be used when iterating through an array of fill style objects to identify the type of fill style without using run-time type checking.

Example

For the coordinate transformation, the scaling factor is calculated by diving the width of the gradient square by the width of the shape:

 float gradientSquareWidth = 32768.0f;
 
 float scale = gradientSquareWidth / shapeWidth;
 

The translation is defined by calculating the difference between the minimum coordinates of the scaled gradient square and the minimum coordinates of the shape:

 int gradientX = -16384; // minimum x coordinate of the gradient square.
 
 int gradientY = -16384; // minimum y coordinate of the gradient square.
 
 // Get the minimum coordinates of the shape being filled;
 int shapeX = shape.getBounds().getMinX();
 
 int shapeY = shape.getBounds().getMinY();
 
 // Now calculate the translation
 int translateX = shapeX - (gradientX / scale);
 
 int translateY = shapeY - (gradientY / scale);
 

The FSCoordTransform has a convenient constructor to create the two-step transformation:

 FSCoordTransform composite = new FSCoordTransform(translateX, translateY,
                                scale, scale);
 

The transform is independent of any transformation defined for the parent shape in which the gradient is displayed. The gradient will be displayed correctly if the shape is scaled, rotated, etc. when it is added to the display list.

The above process is repeated to calculate the transform for the gradient fill at the end of the morphing process. The transforms are independent of any transformation defined for the parent morphing shape in which the gradients are displayed.

History

The FSMorphGradientFill class represents the MorphFillStyle structure from the Macromedia Flash (SWF) File Format Specification. It was introduced in Flash 3.


Field Summary
 
Fields inherited from class com.flagstone.transform.FSFillStyle
Clipped, Linear, Radial, Solid, Tiled, type, Unsmoothed_Clipped, Unsmoothed_Tiled
 
Constructor Summary
FSMorphGradientFill(FSCoder coder)
          Construct an FSMorphGradientFill object, initialising it with values decoded from an encoded object.
FSMorphGradientFill(FSMorphGradientFill obj)
          Constructs an FSMorphGradientFill object by copying values from an existing object.
FSMorphGradientFill(int aType, FSCoordTransform aStartTransform, FSCoordTransform anEndTransform, java.util.ArrayList anArray)
          Constructs an FSMorphGradientFill object specifying the type of fill, starting and ending coordinate transforms and the array of gradient records.
 
Method Summary
 void add(FSMorphGradient aGradient)
          Add a FSMorphGradient object to the array of gradient objects.
 void appendDescription(java.lang.StringBuffer buffer, int depth)
          AppendDescription is used to present a string description of the object including all nested objects up to a specified depth.
 java.lang.Object clone()
          Creates a deep copy of the entire object.
 void decode(FSCoder coder)
           
 void encode(FSCoder coder)
           
 boolean equals(java.lang.Object anObject)
          Returns true if anObject is equal to this one.
 FSCoordTransform getEndTransform()
          Gets the coordinate transform mapping the gradient square onto physical coordinates at the end of the morphing process.
 java.util.ArrayList getGradients()
          Gets the array of up to eight FSMorphGradient objects defining the control points for the gradient.
 FSCoordTransform getStartTransform()
          Gets the coordinate transform mapping the gradient square onto physical coordinates at the start of the morphing process.
 int length(FSCoder coder)
           
 void setEndTransform(FSCoordTransform aTransform)
          Sets the coordinate transform mapping the gradient square onto physical coordinates at the end of the morphing process.
 void setGradients(java.util.ArrayList anArray)
          Sets the array of control points that define the gradient.
 void setStartTransform(FSCoordTransform aTransform)
          Sets the coordinate transform mapping the gradient square onto physical coordinates at the start of the morphing process.
 
Methods inherited from class com.flagstone.transform.FSFillStyle
getType, setType
 
Methods inherited from class com.flagstone.transform.FSTransformObject
name, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FSMorphGradientFill

public FSMorphGradientFill(FSCoder coder)
Construct an FSMorphGradientFill object, initialising it with values decoded from an encoded object.

Parameters:
coder - an FSCoder containing the binary data.

FSMorphGradientFill

public FSMorphGradientFill(int aType,
                           FSCoordTransform aStartTransform,
                           FSCoordTransform anEndTransform,
                           java.util.ArrayList anArray)
Constructs an FSMorphGradientFill object specifying the type of fill, starting and ending coordinate transforms and the array of gradient records.

Parameters:
aType - the type of gradient fill, either FSFillStyle.Linear or FSFillStyle.Radial.
aStartTransform - the coordinate transform mapping the gradient square onto physical coordinates at the start of the morphing process.
anEndTransform - the coordinate transform mapping the gradient square onto physical coordinates at the end of the morphing process.
anArray - an array of up to eight FSMorphGradient objects defining the control points for the gradient.

FSMorphGradientFill

public FSMorphGradientFill(FSMorphGradientFill obj)
Constructs an FSMorphGradientFill object by copying values from an existing object.

Parameters:
obj - an FSMorphGradientFill object.
Method Detail

add

public void add(FSMorphGradient aGradient)
Add a FSMorphGradient object to the array of gradient objects.

Parameters:
aGradient - an FSMorphGradient object.

getStartTransform

public FSCoordTransform getStartTransform()
Gets the coordinate transform mapping the gradient square onto physical coordinates at the start of the morphing process.

Returns:
the starting coordinate transform.

getEndTransform

public FSCoordTransform getEndTransform()
Gets the coordinate transform mapping the gradient square onto physical coordinates at the end of the morphing process.

Returns:
the ending coordinate transform.

getGradients

public java.util.ArrayList getGradients()
Gets the array of up to eight FSMorphGradient objects defining the control points for the gradient.

Returns:
the array of control points.

setStartTransform

public void setStartTransform(FSCoordTransform aTransform)
Sets the coordinate transform mapping the gradient square onto physical coordinates at the start of the morphing process.

Parameters:
aTransform - the starting coordinate transform.

setEndTransform

public void setEndTransform(FSCoordTransform aTransform)
Sets the coordinate transform mapping the gradient square onto physical coordinates at the end of the morphing process.

Parameters:
aTransform - the ending coordinate transform.

setGradients

public void setGradients(java.util.ArrayList anArray)
Sets the array of control points that define the gradient.

Parameters:
anArray - an array of up to eight FSMorphGradient objects.

clone

public java.lang.Object clone()
Description copied from class: FSTransformObject
Creates a deep copy of the entire object.

Overrides:
clone in class FSTransformObject
Returns:
a copy of the object.

equals

public boolean equals(java.lang.Object anObject)
Description copied from class: FSFillStyle
Returns true if anObject is equal to this one. Objects are considered equal if they would generate identical binary data when they are encoded to a Flash file.

Overrides:
equals in class FSFillStyle
Returns:
true if this object would be identical to anObject when encoded.

appendDescription

public void appendDescription(java.lang.StringBuffer buffer,
                              int depth)
Description copied from class: FSTransformObject
AppendDescription is used to present a string description of the object including all nested objects up to a specified depth. This method provide a more controlled way of creating a string representation of an object since large objects such as font or shape definitions can contain dozens of nested objects. The representation of the object is appended to the StringBuffer, showing the name of the class and values of the attributes it contains. If the object contains any attributes that are objects then the object graph will be traversed up to the specified depth. If objects are nested at a level less than specified depth then the full string representation of the object is displayed. For objects at the specified depth only the name of the class is displayed. Any objects below this depth are not displayed.

Specified by:
appendDescription in class FSTransformObject
Parameters:
buffer - a StringBuffer to which the description of each object is appended.
depth - the maximum level of nesting up to which objects will be displayed.

length

public int length(FSCoder coder)
Overrides:
length in class FSFillStyle

encode

public void encode(FSCoder coder)
Overrides:
encode in class FSFillStyle

decode

public void decode(FSCoder coder)
Overrides:
decode in class FSFillStyle