1.2. What Can Java 2D Do?

Java 2D is designed to do anything you want it to do (with computer graphics, at least). Prior to Java 2D, AWT's graphics toolkit had some serious limitations:

  • All lines were drawn with a single-pixel thickness.

  • Only a handful of fonts were available.

  • AWT didn't offer much control over drawing. For example, you couldn't manipulate the individual shapes of characters.

  • If you wanted to rotate or scale anything, you had to do it yourself.

  • If you wanted special fills, like gradients or patterns, you had to make them yourself.

  • Image support was rudimentary.

  • Control of transparency was awkward.

The 2D API remedies these shortcomings and does a lot more, too. To appreciate what the 2D API can offer, you need to see it in action. Java 2 includes a sample program that demonstrates many of the features of the API. To run it, navigate to the demo/jfc/Java2D directory in the JDK installation directory. Then run the Java2Demo class. For example:

C:> cd \jdk1.2\demo\jfc\Java2D
C:> java Java2Demo
Figure 1.1. Sun's 2D demo

You should see a window that looks like Figure 1.1. Each of the tabs across the top displays a set of 2D's features. Spend some time with this application. Then come back and read about all the things 2D can do, including:

shapes

Arbitrary geometric shapes can be represented by combinations of straight lines and curves. The 2D API also provides a useful ...

Get Java 2D Graphics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.