grad
Class LinearGradientPainter

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

public class LinearGradientPainter
extends BasicGradientPainter
implements java.io.Serializable

GradientPainter is a class that accepts parameters defining a gradient, and can also paint the gradient into a Shape (actually, into the Shape's bounding box).

This implementation can only paint two simple kinds of gradients: linear gradients horizontal and vertical. This is because the current implementation uses a very stupid but easy approach of drawing a bunch of 1-pixel lines in different colors. This is easy and fast for small areas and avoids using gobs of memory for large areas, but it is totally impractical for more sophisticated or complicated gradients. [Note: for fancies gradients, you must use an algorithms that renders pixels into some kind of buffer, computing the RGB of each pixel based on it's position in the gradient. That's easy for some gradient shapes but complicated for others. It also requires a buffer big enough to hold all the pixels and computation enough to set all the pixels.]

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 GradientSegments.

The usual sequence for using this class is:

  1. Create the GradientPainter
  2. Add one or more segments
  3. Paint a gradient into a Shape
Then you would do step 3 repeatedly, probably from the paint() method of an AWT component subclass.

Internally, the GradientPainter holds its list of segments on a Vector and uses them in sequence. It is the responsibility of the caller to ensure that the segments link up correctly. Regions of the range [0.0..1.0] that have no segment corresponding to them will not be painted. Regions that overlap will be painted with the lowest-numbered segment that applies to them.

See Also:
GradientSegment, GradientPainter, Serialized Form

Field Summary
static int BOTTOM_TO_TOP
          Linear gradient paint from bottom to top
static int LEFT_TO_RIGHT
          Linear gradient paint from left to right.
static int RIGHT_TO_LEFT
          Linear gradient paint from left to right.
static int TOP_TO_BOTTOM
          Linear gradient paint from top to bottom
 
Fields inherited from class grad.BasicGradientPainter
DEFAULT_MODE, master, segments
 
Constructor Summary
LinearGradientPainter()
          Create a new GradientPainter with 0 segments.
LinearGradientPainter(BasicGradientPainter src)
          Create a new LinearGradientPainter with a copy of the segments of some other gradient painter
LinearGradientPainter(java.awt.Color c1, java.awt.Color c2)
          Create a simple GradientPainter with 1 simple RGB segment.
 
Method Summary
 void paint(java.awt.Graphics g, int x, int y, int w, int h, int mode)
          Paint a gradient into a given rectangular area using a given Graphics.
 void paint(java.awt.Graphics g, java.awt.Shape s, int mode)
          Paint a gradient into a given Shape using a given Graphics.
protected  void paintSimpleLinear(java.awt.Graphics g, int bval, int fval, int min, int max, int incr, boolean h)
           
 
Methods inherited from class grad.BasicGradientPainter
addSegment, countSegments, flipSegment, getSegment, gradientsChanged, removeSegment, setSegment, slaveTo, splitSegment, swapAt, toCodeFragment, toString, update
 
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

LEFT_TO_RIGHT

public static final int LEFT_TO_RIGHT
Linear gradient paint from left to right.

RIGHT_TO_LEFT

public static final int RIGHT_TO_LEFT
Linear gradient paint from left to right.

TOP_TO_BOTTOM

public static final int TOP_TO_BOTTOM
Linear gradient paint from top to bottom

BOTTOM_TO_TOP

public static final int BOTTOM_TO_TOP
Linear gradient paint from bottom to top
Constructor Detail

LinearGradientPainter

public LinearGradientPainter(java.awt.Color c1,
                             java.awt.Color c2)
Create a simple GradientPainter with 1 simple RGB segment.

LinearGradientPainter

public LinearGradientPainter()
Create a new GradientPainter with 0 segments.

LinearGradientPainter

public LinearGradientPainter(BasicGradientPainter src)
Create a new LinearGradientPainter with a copy of the segments of some other gradient painter
Method Detail

paint

public void paint(java.awt.Graphics g,
                  java.awt.Shape s,
                  int mode)
Paint a gradient into a given Shape using a given Graphics. Note that you can use any Shape, including a Rectangle or a Polygon. If Shape is null, then uses the current clip shape of the Graphics. Mode must be one of the constants LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, or BOTTOM_TO_TOP.
Overrides:
paint in class BasicGradientPainter
Following copied from class: grad.BasicGradientPainter
Parameters:
g - Graphics to paint into
s - Shape to fill (usually a Rectangle)
mode - Gradient generation mode (defined per subclass)

paint

public void paint(java.awt.Graphics g,
                  int x,
                  int y,
                  int w,
                  int h,
                  int mode)
Paint a gradient into a given rectangular area using a given Graphics. Mode must be one of the constants LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, or BOTTOM_TO_TOP.

paintSimpleLinear

protected void paintSimpleLinear(java.awt.Graphics g,
                                 int bval,
                                 int fval,
                                 int min,
                                 int max,
                                 int incr,
                                 boolean h)