Since JDK version 1.0, the Graphics class had methods to draw lines, rectangles, ellipses, and so on. But those drawing operations are very limited. For example, you cannot vary the line thickness and you cannot rotate the shapes. JDK 1.2 introduced the Java 2D library, which implements a powerful set of graphical operations. In this chapter, we only look at the basics of the Java 2D library see the Advanced AWT chapter in Volume 2 for more information on the advanced features. To draw shapes in the Java 2D library, you need to obtain an object of the Graphics2D class. This class is a subclass of the Graphics class. If you use a version of the JDK that is Java 2D enabled, methods such as paintComponent automatically receive an object of the Graphics2D class. Simply use a cast, as follows: public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; . . . } The Java 2D library organizes geometric shapes in an object-oriented fashion. In particular, there are classes to represent lines, rectangles, and ellipses: Line2D Rectangle2D Ellipse2D These classes all implement the Shape interface. NOTE
To draw a shape, you first create an object of a class that implements the Shape interface and then call the draw method of the Graphics2D class. For example, Rectangle2D rect = . . .; g2.draw(rect); NOTE
Using the Java 2D shape classes introduces some complexity. Unlike the 1.0 draw methods, which used integer pixel coordinates, the Java 2D shapes use floating-point coordinates. In many cases, that is a great convenience because it allows you to specify your shapes in coordinates that are meaningful to you (such as millimeters or inches) and then translate to pixels. The Java 2D library uses single-precision float quantities for many of its internal floating-point calculations. Single precision is sufficient after all, the ultimate purpose of the geometric computations is to set pixels on the screen or printer. As long as any roundoff errors stay within one pixel, the visual outcome is not affected. Furthermore, float computations are faster on some platforms, and float values require half the storage of double values. However, manipulating float values is sometimes inconvenient for the programmer because the Java programming language is adamant about requiring casts when converting double values into float values. For example, consider the following statement: float f = 1.2; // Error This statement does not compile because the constant 1.2 has type double, and the compiler is nervous about loss of precision. The remedy is to add an F suffix to the floating-point constant: float f = 1.2F; // Ok Now consider this statement: Rectangle2D r = . . . float f = r.getWidth(); // Error This statement does not compile either, for the same reason. The getWidth method returns a double. This time, the remedy is to provide a cast: float f = (float) r.getWidth(); // Ok Because the suffixes and casts are a bit of a pain, the designers of the 2D library decided to supply two versions of each shape class: one with float coordinates for frugal programmers, and one with double coordinates for the lazy ones. (In this book, we fall into the second camp and use double coordinates whenever we can.) The library designers chose a curious, and initially confusing, method for packaging these choices. Consider the Rectangle2D class. This is an abstract class with two concrete subclasses, which are also static inner classes: Rectangle2D.Float Rectangle2D.Double Figure 7-8 shows the inheritance diagram. Figure 7-8. 2D rectangle classesIt is best to try to ignore the fact that the two concrete classes are static inner classes that is just a gimmick to avoid names such as FloatRectangle2D and DoubleRectangle2D. (For more information on static inner classes, see Chapter 6.) When you construct a Rectangle2D.Float object, you supply the coordinates as float numbers. For a Rectangle2D.Double object, you supply them as double numbers. Rectangle2D.Float floatRect = new Rectangle2D.Float(10.0F, 25.0F, 22.5F, 20.0F); Rectangle2D.Double doubleRect = new Rectangle2D.Double(10.0, 25.0, 22.5, 20.0); Actually, because both Rectangle2D.Float and Rectangle2D.Double extend the common Rectangle2D class and the methods in the subclasses simply override methods in the Rectangle2D superclass, there is no benefit in remembering the exact shape type. You can simply use Rectangle2D variables to hold the rectangle references. Rectangle2D floatRect = new Rectangle2D.Float(10.0F, 25.0F, 22.5F, 20.0F); Rectangle2D doubleRect = new Rectangle2D.Double(10.0, 25.0, 22.5, 20.0); That is, you only need to use the pesky inner classes when you construct the shape objects. The construction parameters denote the top-left corner, width, and height of the rectangle. NOTE
The Rectangle2D methods use double parameters and return values. For example, the getWidth method returns a double value, even if the width is stored as a float in a Rectangle2D.Float object. TIP
What we just discussed for the Rectangle2D classes holds for the other shape classes as well. Furthermore, there is a Point2D class with subclasses Point2D.Float and Point2D.Double. Here is how to make a point object. Point2D p = new Point2D.Double(10, 20); TIP
The classes Rectangle2D and Ellipse2D both inherit from the common superclass RectangularShape. Admittedly, ellipses are not rectangular, but they have a bounding rectangle (see Figure 7-9). Figure 7-9. The bounding rectangle of an ellipseThe RectangularShape class defines over 20 methods that are common to these shapes, among them such useful methods as getWidth, getHeight, getCenterX, and getCenterY (but sadly, at the time of this writing, not a getCenter method that returns the center as a Point2D object). Finally, a couple of legacy classes from JDK 1.0 have been fitted into the shape class hierarchy. The Rectangle and Point classes, which store a rectangle and a point with integer coordinates, extend the Rectangle2D and Point2D classes. Figure 7-10 shows the relationships between the shape classes. However, the Double and Float subclasses are omitted. Legacy classes are marked with a gray fill. Figure 7-10. Relationships between the shape classesRectangle2D and Ellipse2D objects are simple to construct. You need to specify
For ellipses, these refer to the bounding rectangle. For example, Ellipse2D e = new Ellipse2D.Double(150, 200, 100, 50); constructs an ellipse that is bounded by a rectangle with the top-left corner at (150, 200), width 100, and height 50. However, sometimes you don't have the top-left corner readily available. It is quite common to have two diagonal corner points of a rectangle, but perhaps they aren't the top-left and bottom-right corners. You can't simply construct a rectangle as Rectangle2D rect = new Rectangle2D.Double(px, py, qx - px, qy - py); // Error If p isn't the top-left corner, one or both of the coordinate differences will be negative and the rectangle will come out empty. In that case, first create a blank rectangle and use the setFrameFromDiagonal method. Rectangle2D rect = new Rectangle2D.Double(); rect.setFrameFromDiagonal(px, py, qx, qy); Or, even better, if you know the corner points as Point2D objects p and q, rect.setFrameFromDiagonal(p, q); When constructing an ellipse, you usually know the center, width, and height, and not the corner points of the bounding rectangle (which don't even lie on the ellipse). The setFrameFromCenter method uses the center point, but it still requires one of the four corner points. Thus, you will usually end up constructing an ellipse as follows: Ellipse2D ellipse = new Ellipse2D.Double(centerX - width / 2, centerY - height / 2, width, height); To construct a line, you supply the start and end points, either as Point2D objects or as pairs of numbers: Line2D line = new Line2D.Double(start, end); or Line2D line = new Line2D.Double(startX, startY, endX, endY); The program in Example 7-4 draws
Figure 7-11 shows the result. Example 7-4. DrawTest.java1. import java.awt.*; 2. import java.awt.geom.*; 3. import javax.swing.*; 4. 5. public class DrawTest 6. { 7. public static void main(String[] args) 8. { 9. DrawFrame frame = new DrawFrame(); 10. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 11. frame.setVisible(true); 12. } 13. } 14. 15. /** 16. A frame that contains a panel with drawings 17. */ 18. class DrawFrame extends JFrame 19. { 20. public DrawFrame() 21. { 22. setTitle("DrawTest"); 23. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 24. 25. // add panel to frame 26. 27. DrawPanel panel = new DrawPanel(); 28. add(panel); 29. } 30. 31. public static final int DEFAULT_WIDTH = 400; 32. public static final int DEFAULT_HEIGHT = 400; 33. } 34. 35. /** 36. A panel that displays rectangles and ellipses. 37. */ 38. class DrawPanel extends JPanel 39. { 40. public void paintComponent(Graphics g) 41. { 42. super.paintComponent(g); 43. Graphics2D g2 = (Graphics2D) g; 44. 45. // draw a rectangle 46. 47. double leftX = 100; 48. double topY = 100; 49. double width = 200; 50. double height = 150; 51. 52. Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); 53. g2.draw(rect); 54. 55. // draw the enclosed ellipse 56. 57. Ellipse2D ellipse = new Ellipse2D.Double(); 58. ellipse.setFrame(rect); 59. g2.draw(ellipse); 60. 61. // draw a diagonal line 62. 63. g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height)); 64. 65. // draw a circle with the same center 66. 67. double centerX = rect.getCenterX(); 68. double centerY = rect.getCenterY(); 69. double radius = 150; 70. 71. Ellipse2D circle = new Ellipse2D.Double(); 72. circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius); 73. g2.draw(circle); 74. } 75. } Figure 7-11. Rectangles and ellipses java.awt.geom.RectangularShape 1.2
java.awt.geom.Rectangle2D.Double 1.2
java.awt.geom.Rectangle2D.Float 1.2
java.awt.geom.Ellipse2D.Double 1.2
java.awt.geom.Point2D.Double 1.2
java.awt.geom.Line2D.Double 1.2
|