Java 2D is an
API
An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how ...
for drawing
two-dimensional
In mathematics, a plane is a Euclidean (flat), two-dimensional surface that extends indefinitely. A plane is the two-dimensional analogue of a point (zero dimensions), a line (one dimension) and three-dimensional space. Planes can arise as s ...
graphics
Graphics () are visual images or designs on some surface, such as a wall, canvas, screen, paper, or stone, to inform, illustrate, or entertain. In contemporary usage, it includes a pictorial representation of data, as in design and manufacture ...
using the
Java programming language
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
. Every Java 2D drawing operation can ultimately be treated as ''filling'' a ''shape'' using a ''paint'' and ''compositing'' the result onto the
screen
Screen or Screens may refer to:
Arts
* Screen printing (also called ''silkscreening''), a method of printing
* Big screen, a nickname associated with the motion picture industry
* Split screen (filmmaking), a film composition paradigm in which mul ...
.
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
The Abstract Window Toolkit (AWT) is Java's original platform-dependent windowing, graphics, and user-interface widget toolkit, preceding Swing. The AWT is part of the Java Foundation Classes (JFC) — the standard API for providing a graphic ...
.
*' The Java standard library of two dimensional geometric shapes such as lines, ellipses, and quadrilaterals.
*' The library for manipulating
glyph
A glyph () is any kind of purposeful mark. In typography, a glyph is "the specific shape, design, or representation of a character". It is a particular graphical representation, in a particular typeface, of an element of written language. A g ...
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.
Basic concepts
These objects are a necessary part of every Java 2D drawing operation.
Shapes
A ''shape'' in Java 2D is a boundary which defines an inside and an outside.
Pixel
In digital imaging, a pixel (abbreviated px), pel, or picture element is the smallest addressable element in a raster image, or the smallest point in an all points addressable display device.
In most digital display devices, pixels are the smal ...
s inside the shape are affected by the drawing operation, those outside are not.
Trying to fill a straight
line segment
In geometry, a line segment is a part of a straight line that is bounded by two distinct end points, and contains every point on the line that is between its endpoints. The length of a line segment is given by the Euclidean distance between ...
will result in no pixels being affected, as such a shape does not contain any pixels itself. Instead, a thin
rectangle
In Euclidean plane geometry, a rectangle is a quadrilateral with four right angles. It can also be defined as: an equiangular quadrilateral, since equiangular means that all of its angles are equal (360°/4 = 90°); or a parallelogram containi ...
must be used so that the shape contains some pixels.
Paints
A ''paint'' generates the
color
Color (American English) or colour (British English) is the visual perceptual property deriving from the spectrum of light interacting with the photoreceptor cells of the eyes. Color categories and physical specifications of color are associ ...
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
An image is a visual representation of something. It can be two-dimensional, three-dimensional, or somehow otherwise feed into the visual system to convey information. An image can be an artifact, such as a photograph or other two-dimensiona ...
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 may refer to any of a number of techniques to combat the problems of aliasing in a sampled signal such as a digital image or digital audio recording.
Specific topics in anti-aliasing include:
* Anti-aliasing filter, a filter used be ...
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
Transform may refer to:
Arts and entertainment
* Transform (scratch), a type of scratch used by turntablists
* ''Transform'' (Alva Noto album), 2001
* ''Transform'' (Howard Jones album) or the title song, 2019
* ''Transform'' (Powerman 5000 album ...
, 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 is a form of computer graphics in which visual images are created directly from geometric shapes defined on a Cartesian plane, such as points, lines, curves and polygons. The associated mechanisms may include vector display a ...
image files.
Java2D / OpenGL interoperability
Since
Java SE 6
The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community ...
, Java2D and
OpenGL
OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardwa ...
have become interoperable, allowing, for example, the drawing of animated 3D graphics instead of icons on a Button (see
JOGL
Java OpenGL (JOGL) is a wrapper library that allows OpenGL to be used in the Java programming language. It was originally developed by Kenneth Bradley Russell and Christopher John Kline, and was further developed by the Sun Microsystems Game Techn ...
).
See also
*
Java 3D
Java 3D is a scene graph-based 3D application programming interface (API) for the Java platform. It runs on top of either OpenGL or Direct3D until version 1.6.0, which runs on top of Java OpenGL (JOGL). Since version 1.2, Java 3D has been deve ...
*
Java Class Library
The Java Class Library (JCL) is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at run time. Because the Java Platform is not dependent on a specific operating system, applications cannot rely on any of ...
*
Java applet
Java applets were small applications written in the Java programming language, or another programming language that compiles to Java bytecode, and delivered to users in the form of Java bytecode. The user launched the Java applet from a ...
References
External links
Java 2D Landing pageat
Oracle
An oracle is a person or agency considered to provide wise and insightful counsel or prophetic predictions, most notably including precognition of the future, inspired by deities. As such, it is a form of divination.
Description
The word '' ...
Java 2D Tutorialat
Oracle
An oracle is a person or agency considered to provide wise and insightful counsel or prophetic predictions, most notably including precognition of the future, inspired by deities. As such, it is a form of divination.
Description
The word '' ...
{{Java desktop
Java APIs
Graphics libraries