[ Team LiB ] Previous Section Next Section

12.8 Antialiasing

As we've already seen, you can request that Java 2D perform antialiasing when it draws text and graphics. Antialiasing smooths the edges of shapes (such as text glyphs) and lines and reduces jaggies. Antialiased drawing is necessary because the outline of a shape drawn on a computer monitor can never be perfectly smooth; a mathematically perfect shape can't be mapped precisely onto a grid of 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 12-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 12-11.

Figure 12-9. Antialiasing enlarged
Example 12-11. AntiAlias.java
package je3.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
        // the background helps to demonstrate the anti-aliasing effect
        ig.setPaint(new GradientPaint(0,0,Color.black,65,35,Color.white));
        ig.fillRect(0, 0, 65, 35);

        // Set drawing attributes for the foreground.
        // Most importantly, turn on anti-aliasing.
        ig.setStroke(new BasicStroke(2.0f));                   // 2-pixel lines
        ig.setFont(new Font("Serif", Font.BOLD, 18));          // 18-point font
        ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING,   // Anti-alias!

        // Now draw pure blue text and a pure red oval
        ig.drawString("Java", 9, 22);
        ig.drawOval(1, 1, 62, 32);
        // Finally, scale the image by a factor of 10 and display it
        // in the window.  This will allow us to see the anti-aliased pixels
        g.drawImage(image, AffineTransform.getScaleInstance(10, 10), c);

        // Draw the image one more time at its original size, for comparison
        g.drawImage(image, 0, 0, c);
    [ Team LiB ] Previous Section Next Section