Java Reference
In-Depth Information
/**
*
Change this window as follows:
*
Set the width to
150 + (
the month of
d)
*
Set the height to
150 + (
the year of
d) % 200
*
Set the title to
d.toString()
*/
public void
setByDate(Date d)
E6.
Write and test a class
DoubleVisionFrame
that customizes
JFrame
to have
two procedures,
createPartner()
and
resetPartner()
. The task of procedure
createPartner
in an instance of this class is to create and show another
JFrame
that is the same size as this one and whose origin is 20 pixels down and to the
right of the origin of this one. Of course, if you drag either frame to another place
or resize it, the other frame will not automatically follow. The task of procedure
resetPartner
is to again set the size of the partner frame to the size of the orig-
inal one and to move it 20 pixels down and to the right of the original frame.
Hint:
you will need an instance variable (field) that will contain the name of the
instance of the second
JFrame
.
E7.
Write and test a class
StarFrame
that customizes a
JFrame
to have two pro-
cedures,
createStar()
and
resetStar()
. The task of procedure
createStar
in
an instance of this class is to create and show four other
JFrames
; the width and
height of each is 1/2 the width and height of this frame. The four frames form a
four-pointed star around the original frame, each touching the original frame in
one of its four corners. The task of procedure
resetPartner
is to reset the four
frames so they have the size and position as stated for procedure
createStar
.
Hint:
you will need four instance variables (fields), one for each of the four new
frames. Be sure to put comments near their declarations so that the reader can see
which is which.
E8.
Write and test a class
BullseyeFrame
that customizes a
JFrame
and has a
method
public void
paint(Graphics g)
that paints four filled circles with
the same center, so that it looks like a bullseye. The inner circle should be filled
with red and have a radius of 10; the next circle, black with a radius of 20; the
next, white with a radius of 30; and the outer circle, black with a radius of 40. In
which order should you paint the circles to get them to look right? Try from inner
to outer; then try from outer to inner. Rather than using
g.drawOval
, you will
need to find a method that draws a filled oval. Look online at the API for
java.awt.Graphics
, and look for an appropriate method that begins with
“
fill
”. See Appendix II for instructions on how to navigate the API.
E9.
Below is a subclass of
JFrame
that paints a face in the frame. The mouth is
placed 1/2 the radius of the face down from its center (
10 + 20
,
15 + 20
) and is
1/2 the radius in length. The eyes are 1/3 of the radius of the face up and to the
left and right of the center of face. The diameter of an eye is 1/6 of the radius of
the face.
Search WWH ::
Custom Search