Jump to content

Java 2D

From Wikipedia, the free encyclopedia

Java 2Dis anAPIfor drawingtwo-dimensionalgraphicsusing theJava programming language.Every Java 2D drawing operation can ultimately be treated asfillingashapeusing apaintandcompositingthe result onto thescreen.

Organization

[edit]

The Java 2D API and its documentation are available for download as a part of JDK 6. Java 2D API classes are organised into the following packages in JDK 6:

Basic concepts

[edit]

These objects are a necessary part of every Java 2D drawing operation.

Shapes

[edit]

Ashapein Java 2D is a boundary which defines an inside and an outside.Pixelsinside the shape are affected by the drawing operation, those outside are not.

Trying to fill a straightline segmentwill result in no pixels being affected, as such a shape does not contain any pixels itself. Instead, a thinrectanglemust be used so that the shape contains some pixels.

Paints

[edit]

Apaintgenerates thecolorsto be used for each pixel of the fill operation. The simplest paint isjava.awt.Color,which generates the same color for all pixels. More complicated paints may produce gradients,images,or indeed any combination of colors. Filling a circular shape using the color yellow results in a solid yellow circle, while filling the same circular shape using a paint that generates an image produces a circular cutout of the image.

Composites

[edit]

During any drawing operation, there is asource(the pixels being produced by the paint) and adestination(the pixels already onscreen). Normally, the source pixels simply overwrite the destination pixels, but thecompositeallows this behavior to be changed.

The composite, given the source and destination pixels, produces the final result that ultimately ends up onscreen. The most common composite isjava.awt.AlphaComposite,which can treat the pixels being drawn as partially transparent, so that the destination pixels show through to some degree.

Filling

[edit]

Tofilla shape, the first step is to identify which pixels fall inside the shape. These pixels will be affected by the fill operation. Pixels that are partially inside and partially outside the shape may be affected to a lesser degree ifanti-aliasingis enabled.

The paint is then asked to generate a color for each of the pixels to be painted. In the common case of a solid-color fill, each pixel will be set to the same color.

The composite takes the pixels generated by the paint and combines them with the pixels already onscreen to produce the final result.

Advanced objects

[edit]

These objects can be viewed as performing their duties in terms of the simpler objects described above.

Transform

[edit]

Every Java 2D operation is subject to atransform,so that shapes may be translated, rotated, sheared, and scaled as they are drawn. The active transform is most often theidentity transform,which does nothing.

Filling using a transform can be viewed as simply creating a new, transformed shape and then filling that shape.

Stroke

[edit]

In addition to thefilloperation, Java 2D provides adrawoperation. While fill draws the interior of a shape, draw draws its outline. The outline can be as simple as a thin line, or as complicated as a dashed line with each dash having rounded edges.

The object responsible for generating the outline is thestroke.Given an input shape, the stroke produces a new shape representing its outline. For instance, an infinitely thin line segment (with no interior) might be stroked into a one-pixel-wide rectangle.

A draw operation can therefore be described as creating a new, stroked object and then filling that object.

Technically speaking, the stroke is only required to accept an input shape and produce a new shape. The stroke implementation provided with Java 2D implements the outline rules described above, but a custom-written stroke could produce any shape it wished.

Optimizations

[edit]

Conceptually, drawing a straight black line in Java 2D can be thought of as creating a line segment, transforming it according to the current transform, stroking it to create a thin rectangle, querying this shape to compute the pixels being affected, generating the pixels usingjava.awt.Color.BLACK,and then compositing the results onto the screen.

However, performing this entire sequence of steps for each drawing operation would be very inefficient. Java 2D therefore optimizes common drawing operations so that many of these steps can be skipped. If the paint is a simple solid color, for instance, there is no need to actually command it to generate a list of colors to be painted. Likewise, if the default fully opaque composite is in use, actually asking it to perform the compositing operation is unnecessary and would waste effort.

Java 2D performs the minimum amount of work necessary to make itseemas if it is performing all of these steps for each operation, therefore retaining both great flexibility and high performance.

Destination

[edit]

For simplicity, the textual examples provided in this article have assumed that the screen is the destination device. However, the destination can be anything, such as a printer, memory image, or even an object which accepts Java 2D graphics commands and translates them intovector graphicimage files.

Java2D / OpenGL interoperability

[edit]

SinceJava SE 6,Java2D andOpenGLhave become interoperable, allowing, for example, the drawing of animated 3D graphics instead of icons on a Button (seeJOGL).

See also

[edit]

References

[edit]
[edit]