Java 2D

Java 2D

In computing, Java 2D is an API for drawing two-dimensional graphics using the Java programming language. Every Java 2D drawing operation can ultimately be treated as "filling" a "shape" using a "paint" and "compositing" the result onto the screen.

Organization

Java 2D and documentation are available for download as a part of JDK 6. Java 2D API classes are organised into the following packages in JDK 6:
*"java.awt" This is the main package for the Java Abstract Window Toolkit.
*"java.awt.geom" This is the Java standard library of two dimensional geometric shapes such as lines, ellipses, and quadrilaterals.
*"java.awt.font" This is the library for manipulating glyphs in Java.
*"java.awt.color" This is the library of tools for dealing with the many different ways that color can be represented.
*"java.awt.image" This is the library for manipulating graphical images.
*"java.awt.image.renderable"
*"java.awt.print" This is the library of tools for writing to paper.

Basic Concepts

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

hapes

A "shape" in Java 2D is an infinitely thin boundary which defines an inside and an outside. Pixels inside the shape are affected by the drawing operation, those outside are not.

Trying to fill an infinitely thin, straight line segment will result in no pixels being affected, as such a shape does not contain any pixels. Instead, a thin rectangle must be used so that the shape contains some pixels.

Paints

A "paint" generates the colors to be used for each pixel of the fill operation. The simplest paint is Javadoc:SE|package=java.awt|java/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

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 Javadoc:SE|package=java.awt|java/awt|AlphaComposite, 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 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, 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.

troke

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 Javadoc:SE|package=java.awt|java/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 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 image files.*

Java2D / OpenGL interoperability

Since build 51 of the Java SE 6, Java2D and OpenGL have become interoperable, allowing, for example, to draw animated 3D graphics instead of icons on a Button (see JOGL).

External links

* [http://java.sun.com/javase/6/docs/technotes/guides/2d/ Java 2D Landing page] at Sun.
* [http://java.sun.com/docs/books/tutorial/2d/index.html Java 2D Tutorial] at Sun.

ee also

* Java 3D
* Java Class Library


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • java — java …   Dictionnaire des rimes

  • Java 5 — Java (langage) Pour les articles homonymes, voir Java.  Ne doit pas être confondu avec JavaScript …   Wikipédia en Français

  • Java EE — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • Java EE — Java Platform, Enterprise Edition o Java EE (anteriormente conocido como Java 2 Platform, Enterprise Edition o J2EE hasta la versión 1.4), es una plataforma de programación parte de la Plataforma Java para desarrollar y ejecutar software de… …   Wikipedia Español

  • JAVA — Île volcanique de l’Indonésie, Java est séparé de Sumatra par le détroit de la Sonde, à l’ouest, et de Bali par le détroit du même nom à l’est. Sa superficie est de 132 000 kilomètres carrés. Alors que le relief suggère une division en bandes… …   Encyclopédie Universelle

  • Java EE — Java Enterprise Edition, ou Java EE (anciennement J2EE), est une spécification pour la technique Java de Sun plus particulièrement destinée aux applications d’entreprise. Ces applications sont considérées dans une approche multi niveaux[1]. Dans… …   Wikipédia en Français

  • Java SE — Java Platform, Standard Edition o Java SE (conocido anteriormente hasta la versión 5.0 como Plataforma Java 2, Standard Edition o J2SE), es una colección de APIs del lenguaje de programación Java útiles para muchos programas de la Plataforma Java …   Wikipedia Español

  • Java VM — Java Logo Die Java Virtual Machine (abgekürzt Java VM oder JVM) ist der Teil der Java Laufzeitumgebung (JRE) für Java Programme, der für die Ausführung des Java Bytecodes verantwortlich ist. Hierbei wird im Normalfall jedes gestartete Java… …   Deutsch Wikipedia

  • JAVA — Cette page d’homonymie répertorie les différents sujets et articles partageant un même nom …   Wikipédia en Français

  • Java ME — Java Platform, Micro Edition, abgekürzt Java ME [ˈdʒɑːvə ˌɛmˈiː], früher auch als Java Platform 2, Micro Edition oder kurz J2ME bezeichnet, ist eine Umsetzung der Programmiersprache Java für so genannte »embedded consumer products« wie etwa… …   Deutsch Wikipedia

  • Java 2 — Java 2, или Java 2.0 дальнейшее развитие и усовершенствование спецификации исходного стандарта языка и платформы Java, на который теперь принято ссылаться как на Java 1.0. В настоящее время спецификация платформы Java 2 продолжает интенсивно… …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”