This section contains the detail about the Java2D in java.
Introduction to Java2D
In Java programming language, for drawing two dimensional graphics we used Java 2D API.
Java 2D API classes are organized into the following packages in JDK 6 :
- java.awt - The main package for the Java Abstract Window Toolkit.
- java.awt.geom - The Java standard library of two dimensional geometric shapes such as lines, ellipses, and quadrilaterals.
- java.awt.font - The library for manipulating glyphs in Java.
- java.awt.color - The library dealing with the many different ways that color can be represented.
- java.awt.image - The library for manipulating graphical images.
- java.awt.print - The library of tools for writing to paper.
Given below objects are essential part of the Java 2D API :
Shapes
In Java 2D , shape is defined as boundary which has an inside and an outside. The inside pixels can be effected by the drawing operations. The outside pixels are not effected by the drawing operation.
Given below some of the shapes classes which provide some of the built in shapes :
- Arc2D.Double, Arc2D.FloatArea (a shape built by adding/subtracting other shapes)
- CubicCurve2D.Double, CubicCurve2D.Float
- Ellipse2D.Double, Ellipse2D.Float
- GeneralPath (a series of connected shapes)
- Line2D.Double, Line2D.Float
- Polygon
- QuadCurve2D.Double, QuadCurve2D.Float
- Rectangle2D.Double, Rectangle2D.Float, Rectangle
- RoundRectangle2D.Double, RoundRectangle2D.Float
java.awt.geom package contains these classes.
Paints
A paint generates the colors to be used for each pixel of the fill operation. This can be a Color (solid color), a GradientPaint (gradient fill gradually combining two colors), a TexturePaint (tiled image), or a new version of Paint that you write yourself. Use setPaint and getPaint to change and retrieve the Paint settings :
Given below some of the major built-in Paint classes :
Color
Has the same constants (Color.red, Color.yellow, etc.) as the AWT version, plus
some extra constructors.
GradientPaint
Constructors takes two points, two colors, and optionally a boolean flag that
indicates that the color pattern should cycle. The first color is used at the
first point, the second color at the second point, and points in between are
colored based on how close they are to each of the points.
TexturePaint
Constructor takes a BufferedImage and a Rectangle2D, maps the image to the
rectangle, then tiles the rectangle. Creating a BufferedImage from a GIF or JPEG
file is a pain. First load an Image normally, get its size, create a
BufferedImage that size with BufferedImage.TYPE_INT_ARGB as the image type, get
the BufferedImage's Graphics object via createGraphics, then draw the Image into
the BufferedImage using drawImage. An example of this process is shown later.
Composites
In any drawing operation, there is a source pixel which is produced by the
paint and a destination pixel which is already on screen. Normally, the
source pixel overwrite the existing pixel on screen. But the composite
has a
different behavior from this. The composite, given the source and destination pixels, produces the final
result that ultimately ends up onscreen. The most common composite is
java.awt.AlphaComposite, which can treat the pixels being drawn as
partially transparent, so that the destination pixels show through to some
degree.
Filling
The first step in filling a shape is to check whether the pixel is inside the shape or not. The pixels which are partially inside and partially outside is corrected by the anti-aliasing. The paint is then asked to generate a color for each of the pixels to be painted.
Advanced objects
Given below 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.
Stroke
Java2D has a draw operation which draws the shape's 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.
[ 0 ] Comments