Java Reference
In-Depth Information
shape by tiling an image. These classes can be used to achieve some of the fill
effects shown in Figure 11-8.
Figure 11•8. Filling shapes with Paint objects
Example 11-10 shows the code that generates Figure 11-8. In addition to using the
GradientPaint and TexturePaint classes, this example demonstrates a variety of
other Java 2D capabilities: translucent colors, font glyphs as Shape objects that cre-
ate “text art,” an AffineTransform and a translucent color that produce a shadow
effect, and a BufferedImage that performs off-screen drawing. Example 11-10 also
illustrates the use of GenericPaint , a custom Paint implementation that we'll see
in Example 11-16.
Example 11•10: Paints.java
package com.davidflanagan.examples.graphics;
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
/** A demonstration of Java2D transformations */
public class Paints implements GraphicsExample {
static final int WIDTH = 800, HEIGHT = 375; // Size of our example
public String getName() { return "Paints"; } // From GraphicsExample
public int getWidth() { return WIDTH; }
// From GraphicsExample
public int getHeight() { return HEIGHT; }
// From GraphicsExample
/** Draw the example */
public void draw(Graphics2D g, Component c) {
// Paint the entire background using a GradientPaint.
// The background color varies diagonally from deep red to pale blue
g.setPaint(new GradientPaint(0, 0, new Color(150, 0, 0),
WIDTH, HEIGHT, new Color(200, 200, 255)));
g.fillRect(0, 0, WIDTH, HEIGHT);
// fill the background
// Use a different GradientPaint to draw a box.
// This one alternates between deep opaque green and transparent green.
Search WWH ::




Custom Search