Starting with Java 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.
Java SE 1.2 introduced the Java 2D library, which implements a powerful set of graphicaloperations. In this chapter, we only look at the basics of the Java 2D library—see theAdvanced AWT chapter in Volume II 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. Ever since Java SE 2, methods suchas paintComponentautomatically receive an object of the Graphics2D class. Simply use acast, as follows:
The Java 2D library organizes geometric shapes in an object-oriented fashion. In particular, there are classes to represent lines, rectangles, and ellipses:
These classes all implement the Shape interface.
NOTE: The Java 2D library supports more complex shapes—in particular, arcs, quadratic and cubic curves, and general paths.
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:
NOTE: Before the Java 2D library appeared, programmers used methods of the Graphics class such as drawRectangleto draw shapes. Superficially, the old-style method calls look abit simpler. However, by using the Java 2D library, you keep your options open—you canlater enhance your drawings with some of the many tools that the Java 2D library supplies.
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 floatingpointcoordinates. 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 some times 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:
This statement does not compile because the constant 1.2 has type double, and the compileris nervous about loss of precision. The remedy is to add an F suffix to the floatingpointconstant:Now consider this statement:
This statement does not compile either, for the same reason. The getWidthmethod returns a double. This time, the remedy is to provide a cast:
Because the suffixes and casts are a bit of a pain, the designers of the 2D library decidedto 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:
2D rectangle classes
It 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 Float Rectangle 2D and Double Rectangle 2D.
When you construct a Rectangle2D.Float object, you supply the coordinates as float numbers. For a Rectangle 2D .Double object, you supply them as double numbers.
Actually, because both Rectangle 2D .Float and Rectangle 2D .Double extend the common Rectangle 2D class and the methods in the subclasses simply override methods in the Rectangle 2D superclass, there is no benefit in remembering the exact shape type. You cansimply use Rectangle2D variables to hold the rectangle references.
That is, you only need to use the pesky inner classes when you construct the shapeobjects. The construction parameters denote the top-left corner, width, and height of therectangle.
NOTE: Actually, the Rectangle2D.Float class has one additional method that is not inheritedfrom Rectangle2D, namely, setRect(float x, float y, float h, float w). You lose that method ifyou store the Rectangle 2D .Float reference in a Rectangle 2D variable. But it is not a big loss—the Rectangle 2D class has a setRectmethod with double parameters.
The Rectangle2D methods use double parameters and return values. For example, the get Width method returns a double value, even if the width is stored as a float in a Rectangle 2D. Floatobject.
TIP: Simply use the Double shape classes to avoid dealing with float values altogether. However, if you are constructing thousands of shape objects, then you can consider using the Float classes to conserve memory.
What we just discussed for the Rectangle2D classes holds for the other shape classes as well. Further more, there is a Point2D class with sub classes Point2D .Float and Point2D .Double.Here is how to make a point object.
TIP: The Point2D class is very useful—it is more object oriented to work with Point2D objects than with separate x- and y- values. Many constructors and methods accept Point2D parameters. We suggest that you use Point2D objects when you can—they usually make geometric computations easier to understand.
The classes Rectangle2D and Ellipse2D both inherit from the common superclass Rectangular Shape. Admittedly, ellipses are not rectangular, but they have a bounding rectangle .
The bounding rectangle of an ellipse
The Rectangular Shape class defines over 20 methods that are common to these shapes, among them such useful methods as get Width, getHeight, getCenterX, and get CenterY(but sadly, at the time of this writing, not a get Center method that returns the center as a Point2D object).
Finally, a couple of legacy classes from Java 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.
Relationships between the shape classes
However, the Double and Float subclasses are omitted. Legacy classes are marked with a gray fill. Rectangle2D and Ellipse 2D objects are simple to construct. You need to specify
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, some times 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 Rectangle 2D.
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 set Frame From Diagonal method, as follows:
Or, even better, if you know the corner points as Point2D objects p and q, then
When constructing an ellipse, you usually know the center, width, and height, and notthe corner points of the bounding rectangle (which don’t even lie on the ellipse). The
set Frame From Center 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:
To construct a line, you supply the start and end points, either as Point2D objects or as pairs of numbers:
Drawing geometric shapes
returns the center, minimum, or maximum x- or y-value of the enclosing rectangle.
constructs a rectangle with the given top-left corner, width, and height.
constructs a rectangle with the given top-left corner, width, and height.
constructs an ellipse whose bounding rectangle has the given top-left corner, width, and height.
constructs a point with the given coordinates.
Core Java Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||JSP Interview Questions|
|Android Interview Questions||JavaServer Faces (JSF) Interview Questions|
|Java collections framework Interview Questions||Java 8 Interview Questions|
|Java Collections Interview Questions||Java Exception Handling Interview Questions|
|Java Concurrency Interview Questions||Java Serialization Interview Questions|
|Java Programmer Interview Questions||Java Inheritance Interview Questions|
|Java IO Interview Questions||Object Oriented Programming in PHP Interview Questions|
Core Java Tutorial
An Introduction To Java
The Java Programming Environment
Fundamental Programming Structures In Java
Objects And Classes
Interfaces And Inner Classes
User Interface Components With Swing
Deploying Applications And Applets
Exceptions, Logging, Assertions, And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.