Java Reference
In-Depth Information
drawer for the class. Hence, a file drawer for a class can contain two kinds of
things:
1. manila folders of the class,
2. components that are declared with keyword
static
.
Class
Math
, which is
public
and, therefore, accessible everywhere, contains
many methods, and they are all
static
because there is no need to put them
inside folders. Instead, file drawer
Math
simply contains a lot of static methods.
It also contains two static fields, one of which is
Math.PI
which is a
double
value that approximates
pi
, the ratio of the circumference of
a circle to its diameter.
Property
static
will be discussed in more detail later. Here, we just want-
ed to make you aware of it because you are sure to see the keyword
static
from
time to time.
1.6
Graphics in a JFrame
We show you how to paint rectangles, circles, text, etc. in a
JFrame
. This process
shows the usefulness and flexibility of working with classes and objects and
extending (customizing) classes. Conceptually, there is nothing new in this sec-
tion; it is just a matter of learning about some more predefined classes in the Java
API and seeing how to use them. We keep this section short because the materi-
al is covered thoroughly and more easily in
ProgramLive
.
We will create another subclass of
JFrame
. The only thing we will do in an
instance of the subclass is to draw in it —lines, rectangles, ovals, text, and the
like, and in different colors.
Each instance of class
JFrame
contains a procedure
paint
, which is called
every time your system has to redraw the associated window (for example,
because it just became visible). This procedure does not do anything because its
body has no statement in it. But if we define a subclass of
JFrame
, we can cus-
tomize this procedure to draw pictures. We
override
procedure
paint
in
JFrame
with a new one in the subclass.
Figure 1.12 contains a subclass
GraphicsFrame
of
JFrame
that defines
method
paint
. Here is the window that is shown by creating an instance of this
subclass and showing it —we resized the window before taking a snapshot of it:
Activity 1-5.6
also discusses
JFrame's
method paint.
We investigate procedure paint in Fig. 1.12. First, your program never calls
Search WWH ::
Custom Search