|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object
|
+--java.util.Observable
|
+--grad.BasicGradientPainter
|
+--grad.GradientPainter
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), and can also fill the gradient into an Image or just an array of pixel values.
This implementation can only paint several kinds of of gradients: linear gradients at any angle, conical gradients at any angle, square gradients at any angle, and radial gradients. The algorithm is fairly simple:
The image is cached for later use; each GradientPainter object will cache exactly one Image. [This way, if you use a separate GradientPainter for each region you wish to paint then the first paint call will be slow but subsequent ones will be very fast, at the cost of hogging more memory for caching.]
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:
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 noo segment corresponding to them will be painted with the background color. Regions that overlap will be painted with the lowest-numbered segment that applies to them.
This class supports Java 1.1 and Java 2. Under Java 1.1 the Shape to paint may be a Rectangle or a Polygon or any other shape supported by java.awt.Graphics.setClip(), [which is usually just Rectangle; the code uses a hideous kludge to auto-recognize this piece of Java1.1-lameness and work around it by making parts of the image transparent, ack!]. Under Java 2, it supports any Shape that can be used to clip with a Graphics2D, include Ellipse2D and GeneralPath. Eventually, I hope to support the full Java2D Paint interface with a separate FancyGradientPaint class.
Objects of class GradientPainter serve as Observables, so that other objects interested in knowing when the gradient has changed (and therefore needs to be repainted) can register themselves as Observers.
GradientSegment,
Shape, Serialized Form| Field Summary | |
protected java.awt.Color |
bg
|
protected int |
cacheAngle
|
protected java.awt.Point |
cacheBasePoint
|
protected java.awt.Rectangle |
cacheBaseRect
|
protected int |
cacheDefRGB
|
protected int |
cacheExtent
|
protected java.awt.Polygon |
cacheHackClipShape
|
protected java.awt.Image |
cacheImage
|
protected int |
cacheMode
|
static int |
CONICAL_GRADIENT
A conical gradient pointing, by default, to the center right. |
protected int |
defAngle
|
protected java.awt.Point |
defCenter
|
protected int |
defExtent
|
protected double |
exf
|
protected int |
glim
|
protected java.awt.Polygon |
hackClipShape
|
static int |
LINEAR_GRADIENT
Linear gradient, by default painted from along the x axis left to right. |
static int |
RADIAL_GRADIENT
A radial gradient with the center at the center of the Shape bounding box. |
static int |
SQUARE_GRADIENT
A square gradient with the center at the center of the Shape bounding box. |
protected double |
th
|
protected int |
tmode
|
protected double |
tw
|
protected double |
tx
|
protected double |
ty
|
protected double |
xrx
|
protected double |
xry
|
protected double |
yrx
|
protected double |
yry
|
| Fields inherited from class grad.BasicGradientPainter |
DEFAULT_MODE, master, segments |
| Fields inherited from interface java.awt.image.ImageObserver |
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
| Constructor Summary | |
GradientPainter()
Create a new GradientPainter with 0 segments. |
|
GradientPainter(BasicGradientPainter src)
Create a GradientPainter with the same segments as the supplied BasicGradientPainter. |
|
GradientPainter(java.awt.Color c1,
java.awt.Color c2)
Create a simple GradientPainter with 1 simple RGB segment. |
|
| Method Summary | |
protected int |
computeColorFor(double x,
double y)
Compute the color for a given point and return it. |
java.awt.Image |
computeGradientImage(java.awt.Toolkit tk,
java.awt.Rectangle baserect,
java.awt.Point basepoint,
int ext,
int angle,
int mode,
int defrgb)
Get an image with the requested gradient in it. |
void |
computeGradientPixels(int w,
int h,
int[] pixels,
java.awt.Point basepoint,
int ext,
int angle,
int mode,
int defrgb)
Fill the pixel array for a given gradient. |
java.awt.Color |
getBackground()
Retrieve the default background color. |
int |
getDefaultAngle()
Retreive the default angle. |
java.awt.Point |
getDefaultCenter()
Retreive the default center point. |
int |
getDefaultExtent()
Retreive the default angle. |
void |
gradientsChanged()
Invalidate the cached image, if any, because the GradientSegments have been changed somehow. |
boolean |
imageUpdate(java.awt.Image im,
int info,
int x,
int y,
int w,
int h)
Do-nothing version of imageUpdate to allow this object to be used as a lazy imageObserver |
void |
paint(java.awt.Graphics g,
java.awt.Shape s,
int mode)
Paint a gradient into a given Shape using a given Graphics. |
void |
paint(java.awt.Graphics g,
java.awt.Shape s,
java.awt.Point center,
int extent,
int angle,
int mode)
Paint a gradient into a given Shape area using a given Graphics. |
void |
setBackground(java.awt.Color c)
Set the default background color; this is the color that will be used where no segment applies. |
void |
setDefaultAngle(int angle)
Set default values for the angle, normally 0. |
void |
setDefaultCenter(java.awt.Point pt)
Set default center point for the gradient drawing. |
void |
setDefaultExtent(int extent)
Set default values for the extent, which is normally auto-computed from the size of the paint region. |
| Methods inherited from class grad.BasicGradientPainter |
addSegment, countSegments, flipSegment, getSegment, 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 |
public static final int LINEAR_GRADIENT
LinearGradientPainterpublic static final int CONICAL_GRADIENT
public static final int RADIAL_GRADIENT
public static final int SQUARE_GRADIENT
protected java.awt.Color bg
protected int defAngle
protected int defExtent
protected java.awt.Point defCenter
protected transient java.awt.Image cacheImage
protected transient java.awt.Rectangle cacheBaseRect
protected transient int cacheAngle
protected transient java.awt.Point cacheBasePoint
protected transient int cacheMode
protected transient int cacheExtent
protected transient int cacheDefRGB
protected transient java.awt.Polygon cacheHackClipShape
protected transient java.awt.Polygon hackClipShape
protected transient double xrx
protected transient double xry
protected transient double yrx
protected transient double yry
protected transient double tx
protected transient double ty
protected transient double tw
protected transient double th
protected transient double exf
protected transient int glim
protected transient int tmode
| Constructor Detail |
public GradientPainter(java.awt.Color c1,
java.awt.Color c2)
public GradientPainter(BasicGradientPainter src)
public GradientPainter()
| Method Detail |
public void setBackground(java.awt.Color c)
public java.awt.Color getBackground()
public void paint(java.awt.Graphics g,
java.awt.Shape s,
int mode)
paint in class BasicGradientPaintergrad.BasicGradientPainterg - Graphics to paint intos - Shape to fill (usually a Rectangle)mode - Gradient generation mode (defined per subclass)public void setDefaultAngle(int angle)
angle - The default angle to use, in degreespublic int getDefaultAngle()
public void setDefaultExtent(int extent)
angle - The default extent to use, >1public int getDefaultExtent()
public void setDefaultCenter(java.awt.Point pt)
pt - Default center point to use (absolute!)public java.awt.Point getDefaultCenter()
public void paint(java.awt.Graphics g,
java.awt.Shape s,
java.awt.Point center,
int extent,
int angle,
int mode)
g - Graphics into which to paints - Shape of the area to paint (usually rectangular)center - The center of the gradient (or null)extent - Size of the gradient computation (>1)angle - Angle of the gradient computationmode - Kind of gradient to paint (default: Linear)
public java.awt.Image computeGradientImage(java.awt.Toolkit tk,
java.awt.Rectangle baserect,
java.awt.Point basepoint,
int ext,
int angle,
int mode,
int defrgb)
tk - The AWT toolkit to use, typically from Component.getToolkit()baserect - A rectangle at origin 0,0 - gives the size of the imagecenter - The centerpoint of the gradient (from origin 0,0)ext - Extent of the gradient, usually related to size of imageangle - Angle of the gradient in degreesmode - Kind of gradient to paintdefrgb - Color to use where the gradient doesnt paint
public void computeGradientPixels(int w,
int h,
int[] pixels,
java.awt.Point basepoint,
int ext,
int angle,
int mode,
int defrgb)
w - Width of the raster to fill in with pixelsh - Height of the raster to fill in with pixelspixels - Array of ints, w*h in sizebasepoint - Base point for the clipping region (usually null)ext - Extent of the gradient (very important, must be >1)angle - Angle of the gradient in degrees (ignored for Radial)mode - Gradient mode, linear, radial, conical, squaredefrgb - raw sRGB int color to use for unpainted areas
protected final int computeColorFor(double x,
double y)
public void gradientsChanged()
gradientsChanged in class BasicGradientPainter
public boolean imageUpdate(java.awt.Image im,
int info,
int x,
int y,
int w,
int h)
imageUpdate in interface java.awt.image.ImageObserver
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||