FileDocCategorySizeDatePackage
ColorGradient.javaAPI DocExample2845Sat Jan 24 10:44:34 GMT 2004je3.graphics

ColorGradient

public class ColorGradient extends Applet
An applet that demonstrates the Color class

Fields Summary
Color
startColor
Color
endColor
Font
bigFont
Constructors Summary
Methods Summary
public voidfillGradient(java.awt.Component c, java.awt.Graphics g, java.awt.Color start, java.awt.Color end)
Draw a color gradient from the top of the specified component to the bottom. Start with the start color and change smoothly to the end

	Rectangle bounds = this.getBounds();  // How big is the component?
	// Get the red, green, and blue components of the start and end
	// colors as floats between 0.0 and 1.0.  Note that the Color class
	// also works with int values between 0 and 255
	float r1 = start.getRed()/255.0f;
	float g1 = start.getGreen()/255.0f;
	float b1 = start.getBlue()/255.0f;
	float r2 = end.getRed()/255.0f;
	float g2 = end.getGreen()/255.0f;
	float b2 = end.getBlue()/255.0f;
	// Figure out how much each component should change at each y value
	float dr = (r2-r1)/bounds.height;
	float dg = (g2-g1)/bounds.height;
	float db = (b2-b1)/bounds.height;

	// Now loop once for each row of pixels in the component
	for(int y = 0; y < bounds.height; y++) {
	    g.setColor(new Color(r1, g1, b1));    // Set the color of the row
	    g.drawLine(0, y, bounds.width-1, y);  // Draw the row
	    r1 += dr; g1 += dg; b1 += db;         // Increment color components
	}
    
public voidinit()
Get the gradient start and end colors as applet parameter values, and parse them using Color.decode(). If they are malformed, use white.

	try {
	    startColor = Color.decode(getParameter("startColor"));
	    endColor = Color.decode(getParameter("endColor"));
	}
	catch (NumberFormatException e) {
	    startColor = endColor = Color.white;
	}
	bigFont = new Font("Helvetica", Font.BOLD, 72);
    
public voidpaint(java.awt.Graphics g)
Draw the applet. The interesting code is in fillGradient() below

	fillGradient(this, g, startColor, endColor);  // display the gradient
	g.setFont(bigFont);                    // set a font
	g.setColor(new Color(100, 100, 200));  // light blue
	g.drawString("Colors!", 100, 100);     // draw something interesting