grad
Class BasicGradientPainter

java.lang.Object
  |
  +--java.util.Observable
        |
        +--grad.BasicGradientPainter
All Implemented Interfaces:
java.util.Observer, java.io.Serializable
Direct Known Subclasses:
GradientPainter, LinearGradientPainter

public abstract class BasicGradientPainter
extends java.util.Observable
implements java.io.Serializable, java.util.Observer

BasicGradientPainter is an abstract base class that handles the management of GradientSegment management for other GradientPainter classes.

A gradient can be a simple gradient, defined by a start and end color, or it can be a multi-part gradient composed of several GradientSegment objects.

This class also extends Observable. Whenever the stored gradients vector changes, the observers will be notified.

See Also:
GradientSegment, GradientPainter, LinearGradientPainter, Serialized Form

Field Summary
static int DEFAULT_MODE
          The default painting mode -- 0.
protected  BasicGradientPainter master
           
protected  java.util.Vector segments
           
 
Constructor Summary
BasicGradientPainter()
          Create a new BasicGradientPainter with 0 segments.
BasicGradientPainter(BasicGradientPainter src)
          Create a new BasicGradientPainter, initialized with a copy of the gradient segments of the supplied painter.
 
Method Summary
 void addSegment(GradientSegment gs)
          Append another GradientSegment to the sequence used by the painter.
 int countSegments()
          Return the count of how many segments this painter is currently holding.
 void flipSegment(int i)
          Flip the specified segment, exchange its start and end color values.
 GradientSegment getSegment(int i)
          Get the GradientSegment no.
 void gradientsChanged()
          Take any necessary action if the gradients have changed.
abstract  void paint(java.awt.Graphics g, java.awt.Shape s, int mode)
          Paint a gradient into a given Shape using a given Graphics, using the given mode.
 GradientSegment removeSegment(int i)
          Remove the GradientSegment no.
 void setSegment(int i, GradientSegment gs)
          Replace the GradientSegment no.
 void slaveTo(BasicGradientPainter m)
          Share our gradient segment list with another gradient painter.
 void splitSegment(int i)
          Split the specified GradientSegment no.
 boolean swapAt(int i)
          Swap segments i and i-1, preserving the offset of the other segments.
 java.lang.String toCodeFragment(java.lang.String toPainter)
          A representation of this GradientPainter as a code fragment creating a GradientPainter named toPainter.
 java.lang.String toString()
          A simple representation of this GradientPainter as a string.
 void update(java.util.Observable o, java.lang.Object x)
          Observe our master if we have one.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_MODE

public static final int DEFAULT_MODE
The default painting mode -- 0. All implementations of this class must support a usable mode with a value of 0.

segments

protected java.util.Vector segments

master

protected BasicGradientPainter master
Constructor Detail

BasicGradientPainter

public BasicGradientPainter()
Create a new BasicGradientPainter with 0 segments.

BasicGradientPainter

public BasicGradientPainter(BasicGradientPainter src)
Create a new BasicGradientPainter, initialized with a copy of the gradient segments of the supplied painter.
Parameters:
src - Painter whose segments are to be copied
Method Detail

slaveTo

public void slaveTo(BasicGradientPainter m)
Share our gradient segment list with another gradient painter. The other one becomes the 'master' and this one becomes the 'slave'. All modifications to the segment list should be done in the master.

update

public void update(java.util.Observable o,
                   java.lang.Object x)
Observe our master if we have one. When the master changes, then we notify our observers too.
Specified by:
update in interface java.util.Observer

paint

public abstract void paint(java.awt.Graphics g,
                           java.awt.Shape s,
                           int mode)
Paint a gradient into a given Shape using a given Graphics, using the given mode. This would normally be called to do the simplest kind of painting into a Canvas or other AWT Component. Subclasses may (and should) provide more sophisticated paint() methods.
Parameters:
g - Graphics to paint into
s - Shape to fill (usually a Rectangle)
mode - Gradient generation mode (defined per subclass)

gradientsChanged

public void gradientsChanged()
Take any necessary action if the gradients have changed. This implementation notifies our observers that our gradients have changed and thus they might want to repaint. All the gradient-setting and -removing methods call it, so subclasses that need to react (possibly by invalidating a cache) can do so by overriding this method. Note that if a subclass does override this method, it should still call super.gradientsChanged() to take care of notifying our observers.

splitSegment

public void splitSegment(int i)
Split the specified GradientSegment no. i at its midpoint. If the value i is out of bounds, then we silently do nothing.

flipSegment

public void flipSegment(int i)
Flip the specified segment, exchange its start and end color values.

swapAt

public boolean swapAt(int i)
Swap segments i and i-1, preserving the offset of the other segments.
Returns:
true if a swap actually took place

getSegment

public GradientSegment getSegment(int i)
Get the GradientSegment no. i, returns null if i is out of bounds.

removeSegment

public GradientSegment removeSegment(int i)
Remove the GradientSegment no. i, return null and remove nothing if i is out of bounds.

setSegment

public void setSegment(int i,
                       GradientSegment gs)
Replace the GradientSegment no. i. Throws ArrayIndexOutOfBoundsException if i is out of bounds.

addSegment

public void addSegment(GradientSegment gs)
Append another GradientSegment to the sequence used by the painter.

countSegments

public int countSegments()
Return the count of how many segments this painter is currently holding.

toString

public java.lang.String toString()
A simple representation of this GradientPainter as a string.
Overrides:
toString in class java.lang.Object

toCodeFragment

public java.lang.String toCodeFragment(java.lang.String toPainter)
A representation of this GradientPainter as a code fragment creating a GradientPainter named toPainter. Note that the printed classname is always "GradientPainter" even if the instance is actually something else (gack!).