Chapter 12. Graphics

Java 1.0 and 1.1 provided rudimentary graphics capabilities through the java.awt.Graphics class and associated classes, such as java.awt.Color and java.awt.Font. In Java 1.2 and later, the Java 2D API provides state-of-the-art, two-dimensional graphics facilities using the java.awt.Graphics2D class (a subclass of Graphics) and associated classes, such as java.awt.BasicStroke, java.awt.GradientPaint, java.awt.TexturePaint, java.awt.AffineTransform, and java.awt.AlphaComposite.

This chapter demonstrates using all these classes; it shows how you can draw graphics with and without the Java 2D API. The key class for all graphics operations is Graphics (or in the Java 2D API, its subclass, Graphics2D). The purpose of this class is threefold:

It defines the drawing surface

A Graphics object can represent the on-screen drawing area within an AWT (or Swing) component. It can also represent an off-screen image you can draw into or a piece of paper to be printed on a printer.

It defines drawing methods

All primitive graphics operations, such as drawing lines, filling shapes, and displaying text, are performed using methods of the Graphics class.

It defines attributes used by the drawing methods

Various methods of the Graphics class can set the font, color, clipping region, and other attributes used when performing graphics operations, such as drawing lines, filling shapes, and displaying text. The values of these graphical attributes are often instances of AWT classes, such ...

Get Java Examples in a Nutshell, 3rd Edition 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.