Home Segments Top Top Previous Next

1135: Mainline

If you like, you can use setColor with Graphics2D graphics contexts, just as you can use setColor with ordinary Graphics graphics contexts. Alternatively, you can create an instance of the Paint class, to produce fancy effects. For example, the following creates an instance of the GradientPaint subclass of the Paint class, for which the color changes continuously from one point to another:

                       *-- Coordinates of first point 
                       | 
                       |                    *-- Color at first point 
                       v                    v 
GradientPaint paint  -------------------  ---------- 
 = new GradientPaint(- meterWidth / 2, 0, Color.blue, 
                     + meterWidth / 2, 0, Color.red); 
                     -------------------  ---------- 
                       ^                    ^ 
                       |                    *-- Color at second point 
                       | 
                       *-- Coordinates of second point 

To attach the paint to a Graphics2D graphics context, g, you use the setPaint method:

g.setPaint(paint);