Home | Articles

Java 2D Graphics Features

The Java 2D graphics classes provide a full range of 2D graphical capabilities. You now have an expanded set of 2D application programming interfaces (APIs) for designing interesting and more usable user interfaces, handling color, processing images, addressing multilingual requirements, and using stylized text.

This article presents an overview of the functionality, tells you where to find the classes, and includes a simple program to show how easy it is to draw and transform a general path geometry (polygon) using fill and line colors.

Other articles and tutorials in the Java 2D Series include:

New in the java.awt Package

The 2D classes are in the java.awt package where they build on the preexisting 2D graphics and imaging classes to give you more 2D functionality, extensibility, and backwards compatibility.

Here is how the 2D classes are packaged:


The preexisting java.awt.Rectangle and java.awt.Point classes are now extensions of the java.awt.geom.Rectangle2D and java.awt.geom.Point2D classes. This architecture makes 2D functionality available to your existing applications without rewriting old code.

There is a Graphics2D class that extends the preexisting java.awt.Graphics class to give you a graphics context with better control over such things as geometry, coordinate transformation, color management, and text layout.

The enhanced Color class creates colors in the standard red, green, and blue (sRGB) device-independent color space and now supports a full range of color spaces.

The enhanced Font class lets you choose any font available on the system, derive a font from an existing font, and access stored font, glyph, and typographic information.

The paint classes give you more texture map and fill pattern options. The TexturePaint class creates a custom fill pattern from a source image or storage format, and the GradientPaint class creates a fill pattern from a linear color gradient pattern.

The AlphaComposite class provides a rich set of image compositing and transparency capabilities for blending a color with an existing color to achieve a wide range of interesting visual effects.

The BasicStroke class provides line widths, line styles, end caps, and dash patterns for creating more interesting lines and frame edges.

The GraphicsConfiguration, GraphicsDevice, and GraphicsEnvironment classes provide a flexible device model so you can define printer and monitor characteristics and configurations to be in effect when using a graphics device.


The java.awt.color package supports high-quality color output using profiles and a full array of color spaces for defining device-dependent and device-independent color attributes.

Common device-dependent color spaces are red, green, blue (RGB), and cyan, magenta, yellow, and black (CMYK). Device-dependent means that, for example, the same color defined in RGB might look different on one monitor when compared to how it looks on a different monitor.

A color profile contains data to enable the transformation of a color between a device-dependent and device-independent color space when it is not practical to use a device-independent color space, such as the International Commission on Illumination (CIE) three-component (XYZ) CIEXYZ color space.


The java.awt.font package supports glyphs and text with multiple fonts. By first customer ship (FCS), underlining, bold, italics, subscript, superscript, and multilanguage support will also be available. Text and glyphs can be transformed and drawn into a graphics context just like any geometric shape.


The java.awt.geom package lets you create basic geometry shapes and perform affine transformations. The AffineTransform class translates, rotates, scales, and shears geometry shapes while maintaining parallel lines. All lines parallel before the affine transformation are parallel after the transformation.

Points now have integer, float, or double precision, and lines have float precision. New point classes are Point2D abstract base class, Point2D.Double, and Point2D.Float. The preexisting java.awt.Point class provides integer precision. New line classes are Line2D abstract base class and Line2D.Float.

You can now create regular rectangles with double or float precision, and round rectangles with float precision. New regular rectangle classes are Rectangle2D abstract base class, Rectangle2D.Double, and Rectangle2D.Float. The preexisting java.awt.Rectangle class provides integer precision. New round rectangle classes are RoundRectangle2D abstract base class and RoundRectangle2D.Float.

The RectangularShape abstract base class is the parent class of Rectangle2D, RoundRectangle2D, Arc2D, and Ellipse2D.

You can now create arcs and ellipses with float precision. New classes are Arc2D and Ellipse2D abstract base classes, and Arc2D.Float and Ellipse2D.Float.

The curve classes provide parametric and quadratic curve capabilities with float precision. The parametric curve classes are CubicCurve2D abstract base class and CubicCurve2D.Float. The quadratic curve classes are QuadCurve2D abstract base class and the QuadCurve2D.Float.

The Area class lets you create an arbitrary shape by starting with either an empty area or geometric shape and adding or subtracting additional shapes.

The GeneralPath class lets you create a polygon on a point-by-point basis and supports the non-zero and even-odd winding rules. You can iterate along a GeneralPath object with GeneralpathIterator or get the flattened view of a path iterator objects such as an ellipse with FlatteningPathIterator.


The java.awt.image package supports a full-range of image processing capabilities including affine transformation, amplitude scaling, lookup-table modification, color conversions, and convolutions. The java.awt.image.BufferedImage class describes an image with an accessible buffer of image data consisting of color model and data layout information.


The java.awt.print package gives you the power and flexibility you need to meet application-level printing requirements. Anything that can be rendered to a Graphics or Graphics2D object can be printed.

Simple 2D Program and Code

Here is a simple 2D program to show how easy it is to use the 2D classes to create the design below. The code creates a canvas class with a cyan background and calls the paint method where a GeneralPath object is created and drawn into a 2D graphics context using a blue outline. The GeneralPath object is first scaled, translated, and drawn into the 2D graphics context using an affine transformation and a red fill. Notice how the Graphics context passed to the paint method is cast to the Graphics2D context so the drawing appears on the canvas.

Simple 2D Demo

Click here for the source code file.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class PathsFill extends Canvas {
  public PathsFill() {

  public void paint(Graphics g) {
    int n = 0;
    Dimension theSize = getSize();
    Graphics2D g2;
    g2 = (Graphics2D) g;

    GeneralPath p = new GeneralPath(1);
    p.moveTo( theSize.width/6, theSize.height/6);
    p.lineTo(theSize.width*5/6, theSize.height/6);
    p.lineTo(theSize.width*5/6, theSize.height*5/6);
    p.lineTo( theSize.width/6, theSize.height*5/6);

    AffineTransform at = new AffineTransform();
    at.scale(.5, .5);
    at.translate(theSize.width/2, theSize.height/2);

Next, an array of colors is set up to draw scaled and rotated rectangles using ten colors.

    Color colorArray[] = new Color[10];
    colorArray[0] = Color.blue;
    colorArray[1] = Color.green;
    colorArray[2] = Color.magenta;
    colorArray[3] = Color.lightGray;
    colorArray[4] = Color.pink;
    colorArray[5] = Color.white;
    colorArray[6] = Color.yellow;
    colorArray[7] = Color.black;
    colorArray[8] = Color.gray;
    colorArray[9] = Color.orange;

    for(n = 0;  n < 10; n++){
      at.scale(.9, .9);
      15, theSize.width/2, theSize.height/2);

  public static void main(String s[]) {
    WindowListener l = new WindowAdapter() {
      public void windowClosing(WindowEvent e) 
      public void windowClosed(WindowEvent e) 

    Frame f = new Frame("Simple 2D Demo ...");
    f.add("Center", new PathsFill());
    f.setSize(new Dimension(400,400));

© 1994-2005 Sun Microsystems, Inc.