[ Team LiB ] |
![]() ![]() |
12.2 The Java 2D APIWe now turn to the Java 2D API, which is available in Java 1.2 and later. Features of this API include:
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.javapackage 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 ] |
![]() ![]() |