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
plus these entirely new packages:
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
- 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
- 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.
- use many of the above tools and techiques thate are used with
shapes - e.g. transformations, compositing, clipping etc. - on
- Image processing
- apply image filters - edge enhancement, blurring, inversion,
etc. are available.
- 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.
new RenderingHints (RenderingHints.KEY_ANTIALISASING,
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,
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