grad
Class GradientSegment

java.lang.Object
  |
  +--grad.GradientSegment
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class GradientSegment
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

A portion of a gradient, from a Color c1 to a Color c2, in either RGB space or HSB space. Every gradient segment is defined as a particular portion of a gradient, where every gradient goes from 0.0 to 1.0. By using multiple GradientSegments, you can use a GradientPainter to get very fancy effects.

This is for Java 1.1. Java 2 already has gradient painting support.

See Also:
Serialized Form

Field Summary
protected  java.awt.Color c1c
           
protected  float[] c1p
           
protected  java.awt.Color c2c
           
protected  float[] c2p
           
protected  float end
           
protected  float start
           
protected  boolean useHSB
           
 
Constructor Summary
GradientSegment(java.awt.Color c1, java.awt.Color c2)
          Create a simple GradientSegment in RGB space, from color c1 to c2 on range 0.0 to 1.0.
GradientSegment(java.awt.Color c1, java.awt.Color c2, double s, double e, boolean isHSB)
          Create a GradientSegment from a Color c1 to a Color 2, over the specified range s to e.
GradientSegment(java.awt.Color c1, java.awt.Color c2, float s, float e, boolean isHSB)
          Create a GradientSegment from Color c1 to c2, over the specified range s to e.
 
Method Summary
 java.lang.Object clone()
          Create a new GradientSegment that is the same as this one.
 void flip()
          Flip the colors of this segment.
 java.awt.Color getColor(float v)
          Get a Color corresponding to the value on the range 0.0 to 1.0.
 boolean getColor(float v, float[] r)
          Get a Color corresponding to the value on the range 0.0 to 1.0 and write the RGB components into the given float array.
 java.awt.Color getColor1()
          Retrieve the start color
 java.awt.Color getColor2()
          Retrieve the end color
 int getColorInt(double v)
          Get a Color as an int with format 0x00RRGGBB.
 float getRangeEnd()
          Retrieve the end of the range
 float getRangeStart()
          Retrieve the start of the range
protected  void getRGBColorComponents(java.awt.Color c, float[] f)
           
 boolean getUseHSB()
          Retrieve the HSB/RGB flag.
protected  void prepareColors()
           
 void setColor1(java.awt.Color c)
          Set the start color
 void setColor2(java.awt.Color c)
          Set the end color
 void setRange(float s, float e)
          Set the start and end values
 void setUseHSB(boolean isHSB)
          Set the HSB/RGB flag
 GradientSegment[] split()
          Split a segment into two segments at its midpoint, the segments will be contiguous, so the overall gradient would not be affected by replacing the original segment with this pair.
 java.lang.String toCodeFragment(java.lang.String addToPainter)
          Render this GradientSegment as a code fragment that is a call to the 5-arg constructor.
 java.lang.String toShortString()
          Render this GradientSegment as a short, informative string.
 java.lang.String toString()
          Render this GradientSegment as a String.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

c1c

protected java.awt.Color c1c

c2c

protected java.awt.Color c2c

c1p

protected float[] c1p

c2p

protected float[] c2p

start

protected float start

end

protected float end

useHSB

protected boolean useHSB
Constructor Detail

GradientSegment

public GradientSegment(java.awt.Color c1,
                       java.awt.Color c2)
Create a simple GradientSegment in RGB space, from color c1 to c2 on range 0.0 to 1.0.

GradientSegment

public GradientSegment(java.awt.Color c1,
                       java.awt.Color c2,
                       double s,
                       double e,
                       boolean isHSB)
Create a GradientSegment from a Color c1 to a Color 2, over the specified range s to e.
Parameters:
s - Start of range, s>=0.0, s<1.0
e - End of range, e>0.0, e<=1.0, e>s

GradientSegment

public GradientSegment(java.awt.Color c1,
                       java.awt.Color c2,
                       float s,
                       float e,
                       boolean isHSB)
Create a GradientSegment from Color c1 to c2, over the specified range s to e.
Method Detail

split

public GradientSegment[] split()
Split a segment into two segments at its midpoint, the segments will be contiguous, so the overall gradient would not be affected by replacing the original segment with this pair.
Returns:
An array of 2 GradientSegments

prepareColors

protected void prepareColors()

getRGBColorComponents

protected void getRGBColorComponents(java.awt.Color c,
                                     float[] f)

getColor1

public java.awt.Color getColor1()
Retrieve the start color

getColor2

public java.awt.Color getColor2()
Retrieve the end color

setColor1

public void setColor1(java.awt.Color c)
Set the start color

setColor2

public void setColor2(java.awt.Color c)
Set the end color

getRangeStart

public float getRangeStart()
Retrieve the start of the range

getRangeEnd

public float getRangeEnd()
Retrieve the end of the range

setRange

public void setRange(float s,
                     float e)
Set the start and end values

setUseHSB

public void setUseHSB(boolean isHSB)
Set the HSB/RGB flag
Parameters:
isHSB - Set to true to use HSB, false for RGB

getUseHSB

public boolean getUseHSB()
Retrieve the HSB/RGB flag.

getColor

public java.awt.Color getColor(float v)
Get a Color corresponding to the value on the range 0.0 to 1.0. If the value given is outside the range that this segment is responsible for, then null is returned.

flip

public void flip()
Flip the colors of this segment.

getColorInt

public final int getColorInt(double v)
Get a Color as an int with format 0x00RRGGBB. This is the fastest method, but also the one that is hardest to use for drawing. If the value v does not fall in this segment, then we return 0. (Normally, we could never return 0 because we have to return an opaque color with alpha=FF.)
Parameters:
v - gradient value on the range [0,1]
Returns:
a color value, or 0.
See Also:
Color

getColor

public boolean getColor(float v,
                        float[] r)
Get a Color corresponding to the value on the range 0.0 to 1.0 and write the RGB components into the given float array. Note that this form is much faster than the other form of getColor for RGB, but no faster for HSB computations. This method returns true if the output array r[] has been filled in and should be painted, and false otherwise.

toString

public java.lang.String toString()
Render this GradientSegment as a String.
Overrides:
toString in class java.lang.Object

toShortString

public java.lang.String toShortString()
Render this GradientSegment as a short, informative string.

toCodeFragment

public java.lang.String toCodeFragment(java.lang.String addToPainter)
Render this GradientSegment as a code fragment that is a call to the 5-arg constructor. If the addToPainter string is not null, then return a longer code fragment that adds this segment to the named painter, like this:
painter1.addSegment(new GradientSegment(new Color(-519),new Color(-1),0.0, 0.5, false));
Parameters:
addToPainter - name of a notional GradientPainter variable

clone

public java.lang.Object clone()
Create a new GradientSegment that is the same as this one.
Overrides:
clone in class java.lang.Object