
Her e’s an example of creating and using a GradientPaint:
Graphics2D g; // Initialized elsewhere
Paint p = new GradientPaint(0, 0, Color.red, 100, 100, Color.pink, true);
g.setPaint(p);
g.fillRect(0, 0, 300, 300);
Both TexturePaint and GradientPaint objects are immutable: they have no set()
methods that allow their attributes to be changed. This is important because it
means that a
Graphics2D object can use a Paint object without worrying about its
attributes being changed concurrently (i.e., the
Graphics2D object does not have to
make a private copy of its
Paint attribute).
Blending Colors with AlphaComposite
Although a Paint object is used to generate the colors used when drawing and fill-
ing with Java 2D, these colors are not always the final colors that end up displayed
on the screen. Another Java 2D attribute, the
Composite object, controls the way in
which the colors being drawn combine with the colors that are alr eady visible on
the drawing surface.
A compositing operation combines the pixels of your drawing (the source pixels)
with the pixels of the drawing surface (the destination pixels) to produce a new,
composite set of pixels. Prior to Java 2D, you could use the
setXORMode() method
of a
Graphics object to produce a simple and very specialized kind of compositing
operation. Java 2D supports generalized compositing through the
java.awt.Com-
posite inter face and its implementation, java.awt.AlphaComposite ...