Java 2D
| Company type | API |
|---|---|
| Industry | Software, Computer graphics |
| Products | 2D graphics rendering and image processing API |
Java 2D is an API designed for rendering two-dimensional graphics within the Java environment. Each drawing action in Java 2D consists of filling a shape using a specified paint and then compositing the outcome onto the display screen. The API offers an extensive collection of classes tailored for sophisticated 2D graphics and image processing, encompassing line art, typography, and raster images. It supports features such as image compositing, alpha transparency, precise color space management and conversion, along with a diverse range of imaging operations optimized for display.
Organization
Included with JDK 6, the Java 2D API and its documentation are structured into several packages:
- java.awt – the fundamental package of the Java Abstract Window Toolkit.
- java.awt.geom – provides a standard set of two-dimensional geometric figures including lines, ellipses, and quadrilaterals.
- java.awt.font – utilities for handling and manipulating glyphs.
- java.awt.color – manages different models of color representation.
- java.awt.image – functionality for image processing and manipulation.
- java.awt.print – tools dedicated to printing capabilities.
Basic concepts
Essential components involved in every Java 2D rendering process include:
Shapes
A shape defines a boundary that separates interior pixels from exterior ones; only pixels inside this boundary are affected during drawing. Since individual line segments contain no pixels themselves, thin rectangles are employed to ensure pixel coverage.
Paints
A paint determines the colors applied to each pixel when filling a shape. The simplest form is java.awt.Color, which applies a uniform color throughout. More sophisticated paints can produce gradients or use images, allowing shapes to be filled with patterns or complex visuals.
Composites
Drawing combines a source, which consists of pixels created by the paint, with a destination, representing the existing pixels on screen. Typically, source pixels replace destination pixels directly; however, the composite alters this behavior. The widely used java.awt.AlphaComposite enables partial transparency by blending source and destination pixels to create effects such as translucency.
Filling
To fill a shape, one must first determine which pixels fall within its boundaries. These pixels are impacted by the fill operation, with partially covered pixels receiving less influence if anti-aliasing is active.
Next, the paint generates colors for each pixel. For solid fills, this usually means applying the same color uniformly.
Finally, the composite merges these newly painted pixels with those already present on the screen to produce the final rendered output.
Advanced objects
These components build upon the fundamental operations outlined above.
Transform
All Java 2D drawing actions are subject to an affine transform, which allows shapes to be moved, rotated, sheared, or scaled during rendering. By default, this is often set to the identity transform, implying no modification.
When filling with a transform applied, a transformed version of the shape is created first and then filled accordingly.
Stroke
Unlike fill, which colors the interior area of shapes, Java 2D's draw operation produces an outline around them. These outlines can range from simple thin lines to intricate dashed patterns with rounded caps.
A stroke object generates these outlines by converting an input shape into another shape that represents its border—for instance, transforming a line segment into a narrow rectangle one pixel wide.
Therefore, drawing involves creating this stroked outline shape and then filling it.
From a technical perspective, a stroke must accept an input shape and return a new shape representing its outline. While Java 2D’s default stroke adheres to conventional outlining rules, custom strokes may produce any arbitrary shape.
Optimizations
In principle, rendering a straight black line using Java 2D entails converting a line segment into a narrow rectangle, identifying the pixels impacted, producing colors with Template:Javadoc:SE, and then compositing the result onto the display.
Performing these operations for each drawing command would be resource-intensive. To enhance efficiency, Java 2D streamlines frequent tasks by omitting redundant procedures—for instance, it skips color computation when solid colors are used or bypasses compositing when the default opaque composite is active.
Such enhancements reduce computational overhead while preserving both adaptability and rendering speed.
Destination
Although demonstrations typically consider the screen as the target device, Java 2D output can also be directed to printers, in-memory images, or any target capable of processing Java 2D graphics instructions and converting them into vector graphic formats.
Java2D / OpenGL interoperability
Starting with Java SE 6, interoperability between Java2D and OpenGL has been supported, facilitating sophisticated capabilities such as displaying animated 3D graphics within user interface components (refer to JOGL).