Home : Course Map : Chapter 6 : Java : Supplements :
Course Map
Chapter 6

  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Text Draw 
  Demo 10
  Demo 11

  Demo 1
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
  Demo 4
  Demo 5   Demo 6
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Topic Index
     Course Guide
     What's New

The Java 2D API refers to the expanded set of classes and packages added to the core AWT when version 1.2 of Java was released. The API offers improved and expanded drawing and image processing capabilities.

We only provide a brief overview of a few of the Java2D topics. Just as with Swing, the Java2D API is very broad and deep. For a more extensive introduction, see Sun's Java2D Tutorial. and the books that cover Java2D (references).


Java2D API includes the the previous packages with some extra classes added

  • java.awt
  • java.awt.image

plus these entirely new packages:

  • java.awt.color
  • java.awt.font
  • java.awt.geom
  • java.awt.print
  • java.awt.renderable

As we see, Java2D does not replace the previous graphics classes. Rather, Java2D expands upon the previous capabilities.

The Java2D classes and methods are fully compatible with both the AWT components and the new Swing components. The Graphics object passed in paint(Graphics)and in the Swing component's paintComponent() method, is simply cast to the Graphics2D object, which provides all of the additional Java2D graphics tools.

The Java 2D API involves a number of capabilities and features that include:

  • Shapes - create standard shape objects and draw them in one method call instead of drawing each segment with multiple method calls. The shapes include rectangles and ellipses and also arbitrary shapes such as the Polygon class. The shape classes implement the Shape interface and provide a PathIterator that steps through the segments of the given shape.

  • Areas - can combine shapes in different ways to create new areas.

  • Stroke - vary the thickness of a line, produce arbitrary dot-dash sequences, select how corners and line ends are shaped, etc.

  • Paint - fill the interiors of shapes, as well as outlines, not just with solid colors but with
  • Transformations - affine transformations (i.e. those geometrical transformations that keep parallel lines parallel) can take a shape and translate it, rotate it, shear it, compress or expand it. Multiple transformations can be compounded together.

  • Compositing - overlay shapes and images according to several optional rules that take advantage of the alpha transparency.

  • Clipping - to speed up performance, only redraw the portion of the display that lies within a given outline or clip. With Java2d one can use arbitrary clip outlines rather than just rectangles.

  • Text - vast array of text rendering capabilities that range from the basic drawString() method to creating your own editor from scratch.

  • Color - lots of tools to help render colors in a consistent manner regardless of the device on which they are displayed.

  • Images - use many of the above tools and techiques thate are used with shapes - e.g. transformations, compositing, clipping etc. - on images.

  • Image processing - apply image filters - edge enhancement, blurring, inversion, etc. are available.

  • Printing - print the graphics display to the local printer in manner faithful to what is seen on the monitor.

The typical Java 2D process begins with method invocations to draw shapes and strokes on what can be thought of as an off-screen image surface with arbitarily fine resolution. A rasterizer converts this off-screen image to a 2D array of pixel or point values, which can be rendered on a monitor screen or a printer.

You can influence the style of the rendering with the setRenderingHits() method. The RenderingHints class provides several different types of hints but the most common hints deal with the edges. If only a single color is used for the pixels of a curved shape, the edge will be jagged or aliased. An anti-aliasing algorithm adds pixels along the edges with graded transparency that gives the edges a much smoother, continuous appearance. This process, however, takes longer to calculate and so can be turned off if not needed.

  g2.setRenderingHints (
    new RenderingHints (RenderingHints.KEY_ANTIALISASING,

Java2D Coordinates

The coordinate system for Java2D follows that of the basic AWT system, but with some enhancements. Java 2D makes a clear distinction between the user coordinate space where the drawing methods operate and the device coordinate space of computer monitors and printers. While the Graphics methods work only with integer pixel coordinates and dimensions, Graphics2D works with floating-point values in a user space that is transformed to the device space of a screen display or printer. The rendering system does the work of transforming the floating-point values in the drawing method arguments to integer pixel or dot numbers for drawing on a device

For low resolution devices, such as monitor screens, the conversion from user space units goes as the screen resolution, e.g. 96.0 units give 1 inch on a 96 pixels/inch screen, 2 inches for 48 pixels/inch, etc. On a high resolution device such as a printer, 72 user units always gives 1 inch regardless of the dots-per-inch setting (ref. Baldwin). You can modify the conversions with the scale() method in Graphics2D.

References & Web Resources


Latest update: Dec. 3, 2004

Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3

Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
22 23 24

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.