10.1. Getting Started with Java 2D

In Java 2, the paintComponent method is supplied with a Graphics2D object, which contains a much richer set of drawing operations than the AWT Graphics object. However, to maintain compatibility with Swing as used in Java 1.1, the declared type of the paintComponent argument is Graphics (Graphics2D inherits from Graphics), so you must first cast the Graphics object to a Graphics2D object before drawing. Technically, in Java 2, all methods that receive a Graphics object (paint, paintComponent, getGraphics) actually receive a Graphics2D object.

The traditional approach for performing graphical drawing in Java 1.1 is reviewed in Listing 10.1. Here, every AWT Component defines a paint method that is passed a Graphics ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.