The Java TM Tutorial

| Start of Tutorial | Search

## Trail: 2D Graphics

### by Deborah Adair, Jennifer Ball and Monica Pawlan

> Your feedback is important to us! If you have comments about this trail, send them to: tutorial@java.sun.com. In your message, put 2D in the subject header.

This trail introduces you to the Java 2D API and shows you how to display and print 2D graphics in your Java programs. The Java 2D API enables you to easily

- Draw lines of any thickness
- Fill shapes with gradients and textures
- Move, rotate, scale, and shear text and graphics
- Composite overlapping text and graphics

For example, you could use the Java 2D API to display complex charts and graphs that use various line and fill styles to distinguish sets of data, like those shown in the following figure.

The Java 2D API also enables you to store and to manipulate image data--for example, you can easily perform image-filter operations, such as blur and sharpen, as shown in the following figure.

This trail covers the most common uses of the Java 2D APIs and briefly describes some of the more advanced features. For additional information about using the Java 2D APIs, see the Java 2D Programmer's Guide. Additional sample programs illustrating the Java 2D API features are also available online.

> Note: The sample applets in this trail can be run with the JDK 1.2 Applet Viewer, a browser with Java Plug-in 1.2 installed, or a JDK 1.2 compatible browser.

The Java 2D APIs are closely integrated with the Abstract Windowing Toolkit (AWT). If you are not familiar with AWT, you might find it useful to review the AWT documentation, which you can download.

Overview of the Java 2D API introduces the key Java 2D concepts and describes the Java 2D rendering model.

Displaying Graphics with Graphics2D teaches you how to set up the Graphics2D rendering context to use fancy stroke and fill styles, perform transformations, clip the drawing region, composite overlapping graphics, and specify rendering preferences.

Working with Text and Fonts shows you how to use a `Font` object to create a font with desired attributes, and to derive a new font by changing the attributes, determine the names of the fonts that are available on your system and position text within a component.

Manipulating and Displaying Images his lesson explains how to implement double buffering and how to perform image-filter operations with `BufferedImage` objects.

Printing teaches you how to render 2D graphics to a printer and how to print complex documents.

Solving Common 2D Graphics Problems gives the solutions to some problems you might encounter when writing 2D applets and applications.

| Start of Tutorial | Search