Java Reference
In-Depth Information
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 2.42 What is the difference between a Java application and a Java applet?
SR 2.43 When is an applet's paint method invoked?
SR 2.44 What is wrong with the following HTML applet tag? Explain.
<applet code="DrawHouse.java" width="400" height="300">
</applet>
2.9 Drawing Shapes
The Java standard class library provides many classes that let us present and
manipulate graphical information. The Graphics class is fundamental to all such
processing.
The Graphics Class
The Graphics class is defined in the java.awt package. It
contains various methods that allow us to draw shapes,
including lines, rectangles, and ovals. Figure 2.12 lists some
of the fundamental drawing methods of the Graphics class.
Note that these methods also let us draw circles and squares, which are just spe-
cific types of ovals and rectangles, respectively. We discuss additional drawing
methods of the Graphics class later in the topic at appropriate points.
The methods of the Graphics class allow us to specify whether we want a
shape filled or unfilled. An unfilled shape shows only the outline of the shape and
is otherwise transparent (you can see any underlying graphics). A filled shape is
solid between its boundaries and covers any underlying graphics.
All of these methods rely on the Java coordinate system,
which we discussed earlier in this chapter. Recall that point
(0,0) is in the upper-left corner, such that x values get larger as
we move to the right, and y values get larger as we move down.
Any shapes drawn at coordinates that are outside the visible
area will not be seen.
KEY CONCEPT
Most shapes can be drawn filled
(opaque) or unfilled (as an outline).
KEY CONCEPT
A bounding rectangle is used to
define the position and size of
curved shapes such as ovals.
Many of the Graphics drawing methods are self-explanatory, but some require
a little more discussion. Note, for instance, that an oval drawn by the drawOval
method is defined by the coordinate of the upper-left corner and dimensions that
specify the width and height of a bounding rectangle. Shapes with curves, such
 
Search WWH ::




Custom Search