The GradientPaint Class
A gradient refers to a colored strip with two defined endpoints. Both endpoints are defined with a different color, and all interior points are filled with colors “in between” the endpoints. The color of each interior pixel is interpolated based on its distance from the endpoints.
All interior points are defined as some color “in between” the two. In this example, all interior points are some shade of gray.
The Java GradientPaint class allows you to set up gradient-filled shapes. To use a GradientPaint, you must first have a Shape object and two points that will define what the endpoints of the paint will be.
Much like the BasicStroke class, the attributes for a GradientPaint object must be set when the object is allocated, that is, within one of its constructor methods. There are several constructor methods available for creating gradient paints.
The following applet, GradientTest, renders a star filled with a green-to-orange gradient paint.
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class GradientTest extends Applet
{
// the Polygon to draw
private Polygon poly;
// the two points that will define the paint's endpoints
private Point2D p1;
private Point2D p2;
public void init()
{
// the radius of two circles
final float[] radii = { 10.0f, 20.0f };
// the starting point and increment level for plotting points
double radians = 0.0;
final double increment = Math.toRadians(15.0);
poly = new Polygon();
// the shape will be determined by alternating between points on
// the perimeters of two circles
// since we are incrementing by 15 degrees, we can fit 24
// points in our shape (360/15 = 24)
for(int i = 0; i < 24; i++)
{
poly.addPoint((int)(radii[i%2]*Math.cos(radians)),
(int)(radii[i%2]*Math.sin(radians)));
radians += increment;
}
// set the endpoints of our paint. these values will be scaled
// by the Graphics2D object
p1 = new Point2D.Float(0.0f, +20.0f);
p2 = new Point2D.Float(0.0f, -20.0f);
}
public void paint(Graphics g)
{
// cast the sent Graphics context to get a usable Graphics2D object
Graphics2D g2d = (Graphics2D)g;
AffineTransform at = new AffineTransform();
at.translate(100,100);
at.scale(5, 5);
// draw the shape
g2d.setTransform(at);
g2d.setPaint(new GradientPaint(p1, Color.ORANGE, p2, Color.GREEN));
g2d.fill(poly);
}
} // GradientTest
Polygon objects are defined using integers. Therefore, it is impossible to model most objects to unit size to scale later. This is because the only values we can use would be zero or one. Most Shape objects are defined using float-point numbers and the Polygon class is defined by integers.
Also notice that the points that define the paint’s endpoints get transformed along with the shape. In the above example, the endpoints for the paint along the perimeter of the shape are defined.
The following applet, CycleTest, shows both cyclic and acyclic gradient paints. It also draws a line perpendicular to the shape at the points the gradient paint is defined.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class CycleTest extends Applet implements ItemListener
{
// the Rectangle to draw
private Rectangle2D rect;
// the line containing the points that will define the paint's endpoints
private Line2D line;
// checkboxes for selecting gradient cycle type
private Checkbox cyclic;
private Checkbox acyclic;
public void init()
{
// create a unit square
rect = new Rectangle2D.Float(-0.5f, -0.5f, 1.0f, 1.0f);
// set the endpoints of our paint.
line = new Line2D.Float(-0.25f, 0.0f, 0.25f, 0.0f);
setBackground(Color.ORANGE);
// create checkboxes for selecting gradient cycle type
CheckboxGroup cbg = new CheckboxGroup();
setLayout(new BorderLayout());
Panel p = new Panel();
p.setBackground(Color.GREEN);
cyclic = new Checkbox("cyclic", cbg, true);
cyclic.addItemListener(this);
p.add(cyclic);
acyclic = new Checkbox("acyclic", cbg, false);
acyclic.addItemListener(this);
p.add(acyclic);
add(p, BorderLayout.SOUTH);
}
public void paint(Graphics g)
{
// the scaled width of the rectangle
final double scaleWidth = 100.0f;
// cast the sent Graphics context to get a usable Graphics2D object
Graphics2D g2d = (Graphics2D)g;
// transform the shape
g2d.translate(100,100);
g2d.scale(scaleWidth, 50);
// draw the shape
g2d.setPaint(new GradientPaint(line.getP1(), Color.BLACK,
line.getP2(), Color.WHITE,
cyclic.getState()));
g2d.fill(rect);
// draw lines perpendicular to paint endpoints
g2d.setPaint(Color.RED);
g2d.setTransform(new AffineTransform());
g2d.translate(100-0.25*scaleWidth, 100);
g2d.rotate(Math.PI/2);
g2d.scale(scaleWidth/2, 1);
g2d.draw(line);
g2d.setTransform(new AffineTransform());
g2d.translate(100+0.25*scaleWidth, 100);
g2d.rotate(Math.PI/2);
g2d.scale(scaleWidth/2, 1);
g2d.draw(line);
}
public void itemStateChanged(ItemEvent e)
{
// update the change!
repaint();
}
} // CycleTest


LinkBack URL
About LinkBacks
Reply With Quote

LinkBacks Enabled by vBSEO
Bookmarks