Java 2D
Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

, Java 2D is an API for drawing two-dimensional graphics
Computer graphics
Computer graphics are graphics created using computers and, more generally, the representation and manipulation of image data by a computer with help from specialized software and hardware....

 using the Java programming language
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

. Every Java 2D drawing operation can ultimately be treated as filling a shape using a paint and compositing the result onto the screen
Computer display
A monitor or display is an electronic visual display for computers. The monitor comprises the display device, circuitry, and an enclosure...

.

Organization

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:
  • The main package for the Java Abstract Window Toolkit
    Abstract Window Toolkit
    The Abstract Window Toolkit is Java's original platform-independent windowing, graphics, and user-interface widget toolkit. The AWT is now part of the Java Foundation Classes — the standard API for providing a graphical user interface for a Java program.AWT is also the GUI toolkit for a...

    .
  • The Java standard library of two dimensional geometric shapes such as lines, ellipses, and quadrilaterals.
  • The library for manipulating glyph
    Glyph
    A glyph is an element of writing: an individual mark on a written medium that contributes to the meaning of what is written. A glyph is made up of one or more graphemes....

    s in Java.
  • The library dealing with the many different ways that color can be represented.
  • The library for manipulating graphical images.
  • The library of tools for writing to paper.

Shapes

A shape in Java 2D is a boundary which defines an inside and an outside. Pixel
Pixel
In digital imaging, a pixel, or pel, is a single point in a raster image, or the smallest addressable screen element in a display device; it is the smallest unit of picture that can be represented or controlled....

s inside the shape are affected by the drawing operation, those outside are not.

Trying to fill a straight line segment
Line segment
In geometry, a line segment is a part of a line that is bounded by two end points, and contains every point on the line between its end points. Examples of line segments include the sides of a triangle or square. More generally, when the end points are both vertices of a polygon, the line segment...

 will result in no pixels being affected, as such a shape does not contain any pixels itself. Instead, a thin rectangle
Rectangle
In Euclidean plane geometry, a rectangle is any quadrilateral with four right angles. The term "oblong" is occasionally used to refer to a non-square rectangle...

 must be used so that the shape contains some pixels.

Paints

A paint generates the color
Color
Color or colour is the visual perceptual property corresponding in humans to the categories called red, green, blue and others. Color derives from the spectrum of light interacting in the eye with the spectral sensitivities of the light receptors...

s to be used for each pixel of the fill operation. The simplest paint is , which generates the same color for all pixels. More complicated paints may produce gradients, image
Image
An image is an artifact, for example a two-dimensional picture, that has a similar appearance to some subject—usually a physical object or a person.-Characteristics:...

s, 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

During any drawing operation, there is a source (the pixels being produced by the paint) and a destination (the pixels already onscreen). Normally, the source pixels simply overwrite the destination pixels, but the composite allows 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 is , which can treat the pixels being drawn as partially transparent, so that the destination pixels show through to some degree.

Filling

To fill a 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 if anti-aliasing
Anti-aliasing
In digital signal processing, spatial anti-aliasing is the technique of minimizing the distortion artifacts known as aliasing when representing a high-resolution image at a lower resolution...

 is 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

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

Transform

Every Java 2D operation is subject to a transform
Affine transformation
In geometry, an affine transformation or affine map or an affinity is a transformation which preserves straight lines. It is the most general class of transformations with this property...

, so that shapes may be translated, rotated, sheared, and scaled as they are drawn. The active transform is most often the identity transform, which does nothing.

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

Stroke

In addition to the fill operation, Java 2D provides a draw operation. 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 the stroke. 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

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 using , 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 it seem as if it is performing all of these steps for each operation, therefore retaining both great flexibility and high performance.

Destination

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 into vector graphic
Vector graphics
Vector graphics is the use of geometrical primitives such as points, lines, curves, and shapes or polygon, which are all based on mathematical expressions, to represent images in computer graphics...

 image files.

Java2D / OpenGL interoperability

Since Java SE 6, Java2D and OpenGL
OpenGL
OpenGL is a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL...

 have become interoperable, allowing, for example, the drawing of animated 3D graphics instead of icons on a Button (see JOGL).

External links

  • Java 2D Landing page at Oracle
    Oracle Corporation
    Oracle Corporation is an American multinational computer technology corporation that specializes in developing and marketing hardware systems and enterprise software products – particularly database management systems...

    .
  • Java 2D Tutorial at Oracle
    Oracle Corporation
    Oracle Corporation is an American multinational computer technology corporation that specializes in developing and marketing hardware systems and enterprise software products – particularly database management systems...

    .
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK