An Overview of Java 2D


Java 2D offers a set of graphics features that address the inadequacies in the older AWT graphics classes. Weaknesses in AWT include only supporting single pixel thickness lines, limited fonts, poor shape manipulation (e.g., no rotation), and no special fills, gradients, or patterns inside shapes.

Java 2D replaces most of the shape primitives in AWT (e.g., rectangles, arcs, lines, ellipses, polygons) with versions that can take double or floating pointing coordinates, though many people still use the old drawLine( ), drawRect( ), and fillRect( ) methods. Of more interest is the ability to create arbitrary geometric shapes by using set operations on other shapes with union, intersection, subtraction, and exclusive-or. A GeneralPath class permits a shape to be built from a series of connected lines and curves, and curves can be defined using splines. (A spline's curviness is specified using a series of control point.)

Java 2D distinguishes between shape stroking and filling. Stroking is the drawing of lines and shape outlines, which may employ various patterns and thicknesses. Shape filling can use a solid color (as in AWT), and patterns, color gradients, and images acting as textures.

Affine transformations can be applied to shapes and images, including translation, rotation, scaling, and shearing, and groups of transformations can be composed together. drawImage( ) can be supplied with such a transformation, which is applied before the image is rendered. Shapes and images can be drawn together using eight different compositing rules, optionally combined with varying transparency values. Clipping can be applied, based on an arbitrary shape (not just a rectangle, as in AWT).

Rendering hints include the anti-aliasing of shapes and text (i.e., the smoothing of their jagged edges), image interpolation, and whether to use high-speed or high-quality rendering.

As a bonus, Java-based printing became relatively easy to control with Java 2D.


Java's top-level web page for Java 2D is http://java.sun.com/products/java-media/2D/, with extensive documentation and a tutorial trail in J2SE.

The Graphics2D Class

The central Java 2D class is Graphics2D, a subclass of AWT's Graphics. paint( ) or paintComponent( ) must cast the graphics context to become a Graphics2D object before Java 2D operations can be employed, as shown in the paintComponent( ) method:

     public void paintComponent(Graphics g)     // draw a blue square     {       super.paintComponent(g);       Graphics2D g2d = (Graphics2D) g;  // cast the graphics context       g2d.setPaint(Color.blue);       Rectangle2D.Double square = new Rectangle2D.Double(10,10,350,350);       g2d.fill(square);     }

The shape can be drawn in outline with draw( ) or filled using the current pen settings by calling fill( ).

Java 2D and Active Rendering

Java 2D operations can be easily utilized in the active rendering approach described in Chapters 2 through 4. As you may recall, a Graphics object for the off-screen buffer is obtained by calling getGraphics( ) inside gameRender( ). This can be cast to a Graphics2D object:

     // global variables for off-screen rendering     private Graphics2D dbg2D;    // was a Graphics object, dbg     private Image dbImage = null;     private void gameRender( )     // draw the current frame to an image buffer     {       if (dbImage == null){  // create the buffer         dbImage = createImage(PWIDTH, PHEIGHT);         if (dbImage == null) {           System.out.println("dbImage is null");           return;         }         else           dbg2D = (Graphics2D) dbImage.getGraphics( );       }       // clear the background using Java 2D       // draw game elements using Java 2D       // existing logic       if (gameOver)         gameOverMessage(dbg2D);     }  // end of gameRender( )

Methods called from gameRender( ), such as gameOverMessage( ), can utilize the Graphics2D object, dbg2D.

In FSEM, the Graphics object is obtained by calling geTDrawGraphics( ), and its result can be cast:

     private Graphics2D gScr2d;    // global, was Graphics gScr     private void screenUpdate( )     { try {         gScr2d = (Graphics2D) bufferStrategy.getDrawGraphics( );         gameRender(gScr2d);         gScr2d.dispose( );         // previously shown logic     }

gameRender( ) receives a Graphics2D object, so it has the full range of Java 2D operations at its disposal.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net