This tutorial introduces you to the Java 2D API and shows you how to display and print 2D graphics in your Java programs. The tutorial is intended for developers who want to enrich their knowledge of the Java 2D API, as well as for beginners in computer graphics. Almost every section contains relevant examples to illustrate specific capabilities.

The Graphics class provides many methods for manipulating a graphics device, be it a screen or off-screen image. The Graphics2D class is a subclass of the Graphics class and contains even more useful methods for manipulating 2D graphics, such as primitive shape rendering and affine transformations. Most of your image creation should take place before you enter the game but for maximum efficiency stored as off-screen sprites and then drawn in the game as a memory copy routine with as little overhead as possible. We will see about creating off-screen images a little later. In the previous examples in this chapter, we have seen the Graphics object passed as a parameter to the methods paint and paintComponent. This is our context for drawing to the component screen. There we can explicitly cast the object to a reference of type Graphics2D, as the true type of the object is not Graphics but a subclass of Graphics.

Graphics2D g2D = (Graphics2D)g;

This means that we can now take advantage of the extra methods in the Graphics2D class on our object. The parameter will always remain of type Graphics to maintain backward compatibility (code compiled in previous versions of Java running on a more up-to-date Java Virtual Machine will still need to run, so newer versions of Java need to contain the same structures of older versions).

The Java 2D API enables you to easily perform the following tasks:

This chapter also explains less familiar concepts such as compositing.

This figure represents different graphics capabilities
Using 2D Graphics API to display complex charts
This figure represents the using of filtering operation
Using image-filtering operations

This chapter describes the concept of drawing on-screen and off-screen images, as well as surfaces and printer devices. This tutorial covers the most common uses of the Java 2D APIs and briefly describes some of the more advanced features.

introduces the key Java 2D concepts and describes the Java 2D rendering model. This lesson is more conceptual than other lessons of this trail, it enables you to get deep into basic notions and classes descriptions.

uses a developed example to show you how to obtain a Graphics object and use it for common graphics rendering tasks.

teaches you how to use APIs to draw graphic primitives and arbitrary shapes, and how to apply fancy strokes and fill styles.

shows you how to effectively use text APIs, including how to create a Font object with desired attributes, measure text, and determine the names of the fonts available on your system.

explains how to create a BufferedImage object, perform image-filtering operations, and draw on an image.

teaches you how to render 2D graphics to a printer, print complex documents, and use Print Services.

explains how to perform transformations, clip the drawing region, composite overlapping graphics, specify rendering preferences, and control rendering quality.