Java Reference
In-Depth Information
FIGURE 19-5
Okay, it's not 3D in the usual sense and you probably can't see the effect in the topic. In this case, the
edges of the rectangle are highlighted so that they appear to be beveled and lift from the top left-hand
corner (or the coordinate origin).
How It Works
The graphics context is passed as the argument to the paint() method as type Graphics , the base class
for Graphics2D , so to use the methods defined in the Graphics2D class you must first cast it to that type.
After you have cast the graphics context object, you set the color in which you draw by calling the
setPaint() method for the Graphics2D object with the drawing color as the argument. All subse-
quent drawing operations are now in the color Color.RED . You can change this with another call to
setPaint() whenever you want to draw in a different color.
Next, you call the draw3DRect() method for the Graphics2D object, and this draws the 3D rectangle.
The first two arguments are integers specifying the x and y coordinates of the top-left corner of the rect-
angle to be drawn, relative to the user space origin of the component, which in this case is the top-left
corner of the view object in the content pane. The third and fourth arguments are integers specifying the
width and height of the rectangle respectively, also in units determined by the user coordinate system.
The fifth argument is a boolean value that makes the rectangle appear to be raised when the value is
true .
The drawString() method draws the string specified as the first argument at the position determined by
the second and third arguments — these are the x and y coordinates in user coordinates of the bottom-left
corner of the first letter of the string. The string is drawn by obtaining the glyphs for the current Font
object in the device context corresponding to the characters in the string. As I said when I discussed Font
objects, the glyphs for a font define the physical appearance of the characters.
However, there's more to drawing than is apparent from this example. The graphics context has inform-
ation about the line style to be drawn, as well as the color, the font to be used for text, and more besides.
Let's dig a little deeper into what is going on.
The Drawing Process
 
Search WWH ::




Custom Search