Java Reference
In-Depth Information
discrete pixels. When the shape is drawn, the pixels inside the shape are filled,
while the pixels outside are not. The outline of a shape rarely falls on perfect pixel
boundaries, however, so approximations are made at the edges. The result is
jagged lines that approximate the abstract shape you wish to represent.
Antialiasing is simply a technique for improving these approximations using
translucent colors. For example, if a pixel at the edge of a shape is half covered by
the shape, the pixel is filled using a color that is half opaque. If only one-fifth of
the pixel is covered, the pixel is one-fifth opaque. This technique works quite well
to reduce jaggies. Figure 11-9 illustrates the process of antialiasing: it shows an
antialiased figure that has been artificially enlarged to show the translucent colors
used at the edges of shapes and text glyphs. The figure is generated by the
straightforward code in Example 11-11.
Figur e 11•9. Antialiasing enlar ged
Example 11•11: AntiAlias.java
package com.davidflanagan.examples.graphics;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
/** A demonstration of anti-aliasing */
public class AntiAlias implements GraphicsExample {
static final int WIDTH = 650, HEIGHT = 350;
// Size of our example
public String getName() {return "AntiAliasing";}
// 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) {
BufferedImage image = // Create an off-screen image
new BufferedImage(65, 35, BufferedImage.TYPE_INT_RGB);
Graphics2D ig = image.createGraphics(); // Get its Graphics for drawing
// Set the background to a gradient fill. The varying color of
Search WWH ::




Custom Search