[ Team LiB ] Previous Section Next Section

12.2 The Java 2D API

We now turn to the Java 2D API, which is available in Java 1.2 and later. Features of this API include:

  • The Graphics2D class, which is a subclass of Graphics that defines additional graphics primitives and attributes.

  • The Shape interface, which Java 2D uses to define many graphics primitives and other operations. The java.awt.geom package contains a number of useful implementations of Shape.

  • The Stroke interface, which describes how lines are drawn (or stroked). The BasicStroke class implements this interface and supports the drawing of wide and dashed lines.

  • The Paint interface, which describes how shapes are filled. The GradientPaint implementation allows filling with color gradients, while TexturePaint supports tiling with images. Also, as of Java 1.2, the Color class implements the Paint interface, to allow shapes to be filled with a solid color.

  • The Composite interface, which defines how colors being drawn are combined with the colors already on the drawing surface. The AlphaComposite class allows colors to be combined based on the rules of alpha transparency. The Color class also has new constructors and methods that support translucent colors.

  • The RenderingHints class, which allows an application to request particular types of drawing, such as antialiased drawing. Antialiasing uses transparent colors and compositing to smooth the edges of text and other shapes, preventing "jaggies."

  • The java.awt.geom.AffineTransform class, which defines coordinate-system transformations and allows shapes (and entire coordinate systems) to be translated, scaled, rotated, and sheared.

    Because AffineTransform allows coordinate-system transformations, including scaling, the Java 2D API distinguishes between the coordinate system of the device (i.e., device space) and the (possibly) transformed user coordinate system (i.e., user space). Coordinates in user space can be specified using float and double values, and graphics drawn using Java 2D are resolution-independent.

In addition, in Java 1.2 and later, your applications are no longer restricted to using a fixed set of logical fonts; you can now use any font installed on the system. Finally, the Java 2D API also includes easy-to-use image-processing classes.

The rest of this chapter is devoted to examples that demonstrate these features of the Java 2D API. Most of the examples are implementations of the GraphicsExample interface, which is shown in Example 12-5.

Example 12-5. GraphicsExample.java
package je3.graphics;
import java.awt.*;

 * This interface defines the methods that must be implemented by an
 * object that is to be displayed by the GraphicsExampleFrame object
public interface GraphicsExample {
    public String getName( );                      // Return the example name
    public int getWidth( );                        // Return its width
    public int getHeight( );                       // Return its height
    public void draw(Graphics2D g, Component c);  // Draw the example

You can run these examples using the GraphicsExampleFrame frame program, which is shown at the end of the chapter. This program creates a simple GUI, instantiates the classes listed on the command line, and displays them in a window. For example, to display the Shapes example we'll see in the next section, you can use GraphicsExampleFrame as follows:

% java je3.graphics.GraphicsExampleFrame Shapes

Many of the figures shown in this chapter are drawn at high resolution; they are not typical screenshots of a running program. GraphicsExampleFrame includes the printing functionality that generated these figures. Even though GraphicsExampleFrame doesn't do any graphics drawing of its own and is therefore more of an example of Java's printing capabilities, it is still listed in Example 12-20 in this chapter.

    [ Team LiB ] Previous Section Next Section