The Basic Java 2D Recipe

As stated previously, there is a basic three-step recipe for writing a graphics program in Java:

1.
Get a graphics context.
2.
Set the context.
3.
Render something.

Getting the graphics context is pretty straightforward. Cast the Graphics object as a Graphics2D object as follows:

public void paint(Graphics g) {
     Graphics2D g2d = (Graphics2D) g;
}

The result of making this cast is that the programmer has access to the increased functionality of the methods, classes, and interfaces of the Graphics2D object. These extended capabilities enable the advanced graphics operations described in the next several chapters. The Graphics2D object is covered in detail in the section “Set the Graphics2D Context….”

Step 2 of the ...

Get Java™ Media APIs: Cross-Platform Imaging, Media, and Visualization 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.