Java 2D API

The Java 2D API provides advanced two-dimensional graphics capabilities for programmers who require detailed and complex graphical manipulations. The API includes features for processing line art, text and images in packages java.awt, java.awt.image, java.awt.color, java.awt.font, java.awt.geom, java.awt.print and java.awt.image.renderable. The capabilities of the API are far too broad to cover in this textbook. For an overview of the capabilities, see the Java 2D demo (discussed in Chapter 20, Introduction to Java Applets) or visit java.sun.com/products/java-media/2D/index.html. In this section, we overview several Java 2D capabilities.

Drawing with the Java 2D API is accomplished with a Graphics2D reference (package java.awt). Graphics2D is an abstract subclass of class Graphics, so it has all the graphics capabilities demonstrated earlier in this chapter. In fact, the actual object used to draw in every paintComponent method is an instance of a subclass of Graphics2D that is passed to method paintComponent and accessed via the superclass Graphics. To access Graphics2D capabilities, we must cast the Graphics reference (g) passed to paintComponent into a Graphics2D reference with a statement such as

 Graphics2D g2d = ( Graphics2D ) g;

The next two examples use this technique.

Lines, Rectangles, Round Rectangles, Arcs and Ellipses

The next example demonstrates several Java 2D shapes from package java.awt.geom, including Line2D.Double, Rectangle2D.Double, RoundRectangle2D.Double, Arc2D.Double and Ellipse2D.Double. Note the syntax of each class name. Each of these classes represents a shape with dimensions specified as double-precision floating-point values. There is a separate version of each represented with single-precision floating-point values (e.g., Ellipse2D.Float ). In each case, Double is a static nested class of the class specified to the left of the dot (e.g., Ellipse2D). To use the static nested class, we simply qualify its name with the outer class name.

In Fig. 12.29Fig. 12.30, we draw Java 2D shapes and modify their drawing characteristics, such as changing line thickness, filling shapes with patterns and drawing dashed lines. These are just a few of the many capabilities provided by Java 2D.

Figure 12.29. Java 2D shapes.

(This item is displayed on pages 623 - 624 in the print version)

 1 // Fig. 12.29: ShapesJPanel.java
 2 // Demonstrating some Java 2D shapes.
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import java.awt.BasicStroke; 
 6 import java.awt.GradientPaint;
 7 import java.awt.TexturePaint; 
 8 import java.awt.Rectangle;
 9 import java.awt.Graphics2D; 
10 import java.awt.geom.Ellipse2D; 
11 import java.awt.geom.Rectangle2D; 
12 import java.awt.geom.RoundRectangle2D;
13 import java.awt.geom.Arc2D; 
14 import java.awt.geom.Line2D; 
15 import java.awt.image.BufferedImage; 
16 import javax.swing.JPanel;
17
18 public class ShapesJPanel extends JPanel
19 {
20 // draw shapes with Java 2D API
21 public void paintComponent( Graphics g )
22 {
23 super.paintComponent( g ); // call superclass's paintComponent
24
25 Graphics2D g2d = ( Graphics2D ) g; // cast g to Graphics2D
26
27 // draw 2D ellipse filled with a blue-yellow gradient 
28 g2d.setPaint( new GradientPaint( 5, 30, Color.BLUE, 35, 100,
29  Color.YELLOW, true ) ); 
30 g2d.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) ); 
31
32 // draw 2D rectangle in red 
33 g2d.setPaint( Color.RED ); 
34 g2d.setStroke( new BasicStroke( 10.0f ) ); 
35 g2d.draw( new Rectangle2D.Double( 80, 30, 65, 100 ) );
36
37 // draw 2D rounded rectangle with a buffered background
38 BufferedImage buffImage = new BufferedImage( 10, 10, 
39  BufferedImage.TYPE_INT_RGB ); 
40
41 // obtain Graphics2D from bufferImage and draw on it
42 Graphics2D gg = buffImage.createGraphics();
43 gg.setColor( Color.YELLOW ); // draw in yellow
44 gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle
45 gg.setColor( Color.BLACK ); // draw in black
46 gg.drawRect( 1, 1, 6, 6 ); // draw a rectangle
47 gg.setColor( Color.BLUE ); // draw in blue
48 gg.fillRect( 1, 1, 3, 3 ); // draw a filled rectangle
49 gg.setColor( Color.RED ); // draw in red
50 gg.fillRect( 4, 4, 3, 3 ); // draw a filled rectangle
51
52 // paint buffImage onto the JFrame 
53 g2d.setPaint( new TexturePaint( buffImage, 
54  new Rectangle( 10, 10 ) ) ); 
55 g2d.fill( 
56  new RoundRectangle2D.Double( 155, 30, 75, 100, 50, 50 ) );
57
58 // draw 2D pie-shaped arc in white 
59 g2d.setPaint( Color.WHITE ); 
60 g2d.setStroke( new BasicStroke( 6.0f ) ); 
61 g2d.draw( 
62  new Arc2D.Double( 240, 30, 75, 100, 0, 270, Arc2D.PIE ) );
63
64 // draw 2D lines in green and yellow 
65 g2d.setPaint( Color.GREEN ); 
66 g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) );
67
68 // draw 2D line using stroke 
69 float dashes[] = { 10 }; // specify dash pattern 
70 g2d.setPaint( Color.YELLOW ); 
71 g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND,
72  BasicStroke.JOIN_ROUND, 10, dashes, 0 ) ); 
73 g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) ); 
74 } // end method paintComponent
75 } // end class ShapesJPanel

Figure 12.30. Creating JFrame to display shapes.

(This item is displayed on pages 624 - 625 in the print version)

 1 // Fig. 12.30: Shapes.java
 2 // Demonstrating some Java 2D shapes.
 3 import javax.swing.JFrame;
 4
 5 public class Shapes
 6 {
 7 // execute application
 8 public static void main( String args[] )
 9 {
10 // create frame for ShapesJPanel
11 JFrame frame = new JFrame( "Drawing 2D shapes" );
12 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
13
14 // create ShapesJPanel
15 ShapesJPanel shapesJPanel = new ShapesJPanel();
16
17 frame.add( shapesJPanel ); // add shapesJPanel to frame
18 frame.setSize( 425, 200 ); // set frame size
19 frame.setVisible( true ); // display frame
20 } // end main
21 } // end class Shapes
 

Line 25 of Fig. 12.29 casts the Graphics reference received by paintComponent to a Graphics2D reference and assigns it to g2d to allow access to the Java 2D features.

The first shape we draw is an oval filled with gradually changing colors. Lines 2829 invoke Graphics2D method setPaint to set the Paint object that determines the color for the shape to display. A Paint object is an object that implements interface java.awt.Paint. The Paint object can be something as simple as one of the predeclared Color objects introduced in Section 12.3 (class Color implements Paint), or it can be an instance of the Java 2D API's GradientPaint, SystemColor or TexturePaint classes. In this case, we use a GradientPaint object.

Class GradientPaint helps draw a shape in gradually changing colorscalled a gradient. The GradientPaint constructor used here requires seven arguments. The first two specify the starting coordinate for the gradient. The third specifies the starting Color for the gradient. The fourth and fifth specify the ending coordinate for the gradient. The sixth specifies the ending Color for the gradient. The last argument specifies whether the gradient is cyclic (TRue) or acyclic (false). The two sets of coordinates determine the direction of the gradient. Because the second coordinate (35, 100) is down and to the right of the first coordinate (5, 30), the gradient goes down and to the right at an angle. Because this gradient is cyclic (TRue), the color starts with blue, gradually becomes yellow, then gradually returns to blue. If the gradient is acyclic, the color transitions from the first color specified (e.g., blue) to the second color (e.g., yellow).

Line 30 uses Graphics2D method fill to draw a filled Shape objectan object that implements interface Shape (package java.awt). In this case, we display an Ellipse2D.Double object. The Ellipse2D.Double constructor receives four arguments specifying the bounding rectangle for the ellipse to display.

Next we draw a red rectangle with a thick border. Line 33 invokes setPaint to set the Paint object to Color.RED. Line 34 uses Graphics2D method setStroke to set the characteristics of the rectangle's border (or the lines for any other shape). Method setStroke requires as its argument an object that implements interface Stroke (package java.awt). In this case, we use an instance of class BasicStroke. Class BasicStroke provides several constructors to specify the width of the line, how the line ends (called the end caps ), how lines join together (called line joins) and the dash attributes of the line (if it is a dashed line). The constructor here specifies that the line should be 10 pixels wide.

Line 35 uses Graphics2D method draw to draw a Shape objectin this case, a Rectangle2D.Double. The Rectangle2D.Double constructor receives four arguments specifying the upper-left x-coordinate, upper-left y-coordinate, width and height of the rectangle.

Next we draw a rounded rectangle filled with a pattern created in a BufferedImage (package java.awt.image) object. Lines 3839 create the BufferedImage object. Class BufferedImage can be used to produce images in color and grayscale. This particular BufferedImage is 10 pixels wide and 10 pixels tall (as specified by the first two arguments of the constructor). The third argument BufferedImage.TYPE_INT_RGB indicates that the image is stored in color using the RGB color scheme.

To create the fill pattern for the rounded rectangle, we must first draw into the BufferedImage. Line 42 creates a Graphics2D object (with a call to BufferedImage method createGraphics) that can be used to draw into the BufferedImage. Lines 4350 use methods setColor, fillRect and drawRect (discussed earlier in this chapter) to create the pattern.

Lines 5354 set the Paint object to a new TexturePaint (package java.awt) object. A TexturePaint object uses the image stored in its associated BufferedImage (the first constructor argument) as the fill texture for a filled-in shape. The second argument specifies the Rectangle area from the BufferedImage that will be replicated through the texture. In this case, the Rectangle is the same size as the BufferedImage. However, a smaller portion of the BufferedImage can be used.

Lines 5556 use Graphics2D method fill to draw a filled Shape objectin this case, a RoundRectangle2D.Double. The constructor for class RoundRectangle2D.Double receives six arguments specifying the rectangle dimensions and the arc width and arc height used to determine the rounding of the corners.

Next we draw a pie-shaped arc with a thick white line. Line 59 sets the Paint object to Color.WHITE. Line 60 sets the Stroke object to a new BasicStroke for a line 6 pixels wide. Lines 6162 use Graphics2D method draw to draw a Shape objectin this case, an Arc2D.Double. The Arc2D.Double constructor's first four arguments specify the upperleft x-coordinate, upper-left y-coordinate, width and height of the bounding rectangle for the arc. The fifth argument specifies the start angle. The sixth argument specifies the arc angle. The last argument specifies how the arc is closed. Constant Arc2D.PIE indicates that the arc is closed by drawing two linesone line from the arc's starting point to the center of the bounding rectangle and one line from the center of the bounding rectangle to the ending point. Class Arc2D provides two other static constants for specifying how the arc is closed. Constant Arc2D.CHORD draws a line from the starting point to the ending point. Constant Arc2D.OPEN specifies that the arc should not be closed.

Finally, we draw two lines using Line2D objectsone solid and one dashed. Line 65 sets the Paint object to Color.GREEN. Line 66 uses Graphics2D method draw to draw a Shape objectin this case, an instance of class Line2D.Double. The Line2D.Double constructor's arguments specify the starting coordinates and ending coordinates of the line.

Line 69 declares a one-element float array containing the value 10. This array will be used to describe the dashes in the dashed line. In this case, each dash will be 10 pixels long. To create dashes of different lengths in a pattern, simply provide the length of each dash as an element in the array. Line 70 sets the Paint object to Color.YELLOW. Lines 7172 set the Stroke object to a new BasicStroke. The line will be 4 pixels wide and will have rounded ends (BasicStroke.CAP_ROUND). If lines join together (as in a rectangle at the corners), their joining will be rounded (BasicStroke.JOIN_ROUND). The dashes argument specifies the dash lengths for the line. The last argument indicates the starting index in the dashes array for the first dash in the pattern. Line 73 then draws a line with the current Stroke.

General Paths

Next we present a general patha shape constructed from straight lines and complex curves. A general path is represented with an object of class GeneralPath (package java.awt.geom). The application of Fig. 12.31 and Fig. 12.32 demonstrates drawing a general path in the shape of a five-pointed star.

Figure 12.31. Java 2D general paths.

(This item is displayed on page 627 in the print version)

 1 // Fig. 12.31: Shapes2JPanel.java
 2 // Demonstrating a general path.
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import java.awt.Graphics2D;
 6 import java.awt.geom.GeneralPath;
 7 import java.util.Random;
 8 import javax.swing.JPanel;
 9
10 public class Shapes2JPanel extends JPanel
11 {
12 // draw general paths
13 public void paintComponent( Graphics g )
14 {
15 super.paintComponent( g ); // call superclass's paintComponent
16 Random random = new Random(); // get random number generator
17
18 int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
19 int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 }; 
20
21 Graphics2D g2d = ( Graphics2D ) g;
22 GeneralPath star = new GeneralPath(); // create GeneralPath object
23
24 // set the initial coordinate of the General Path
25 star.moveTo( xPoints[ 0 ], yPoints[ 0 ] ); 
26
27 // create the star--this does not draw the star
28 for ( int count = 1 ; count < xPoints.length; count++ )
29 star.lineTo( xPoints[ count ], yPoints[ count ] );
30
31 star.closePath(); // close the shape
32
33 g2d.translate( 200, 200 ); // translate the origin to (200, 200)
34
35 // rotate around origin and draw stars in random colors
36 for ( int count = 1 ; count <= 20; count++ )
37 {
38 g2d.rotate( Math.PI / 10.0 ); // rotate coordinate system
39
40 // set random drawing color
41 g2d.setColor( new Color( random.nextInt( 256 ),
42 random.nextInt( 256 ), random.nextInt( 256 ) ) );
43
44 g2d.fill( star ); // draw filled star
45 } // end for
46 } // end method paintComponent
47 } // end class Shapes2JPanel

Figure 12.32. Creating JFrame to display stars.

(This item is displayed on page 628 in the print version)

 1 // Fig. 12.32: Shapes2.java
 2 // Demonstrating a general path.
 3 import java.awt.Color;
 4 import javax.swing.JFrame;
 5
 6 public class Shapes2
 7 {
 8 // execute application
 9 public static void main( String args[] )
10 {
11 // create frame for Shapes2JPanel
12 JFrame frame = new JFrame( "Drawing 2D Shapes" );
13 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
14
15 Shapes2JPanel shapes2JPanel = new Shapes2JPanel();
16 frame.add( shapes2JPanel ); // add shapes2JPanel to frame
17 frame.setBackground( Color.WHITE ); // set frame background color
18 frame.setSize( 400, 400 ); // set frame size
19 frame.setVisible( true ); // display frame
20 } // end main
21 } // end class Shapes2
 

Lines 1819 declare two int arrays representing the x- and y-coordinates of the points in the star. Line 22 creates GeneralPath object star. Line 25 uses GeneralPath method moveTo to specify the first point in the star. The for statement in lines 2829 uses GeneralPath method lineTo to draw a line to the next point in the star. Each new call to lineTo draws a line from the previous point to the current point. Line 31 uses GeneralPath method closePath to draw a line from the last point to the point specified in the last call to moveTo. This completes the general path.

Line 33 uses Graphics2D method translate to move the drawing origin to location (200, 200). All drawing operations now use location (200, 200) as (0, 0).

The for statement in lines 3645 draws the star 20 times by rotating it around the new origin point. Line 38 uses Graphics2D method rotate to rotate the next displayed shape. The argument specifies the rotation angle in radians (with 360° = 2p radians). Line 44 uses Graphics2D method fill to draw a filled version of the star.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

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