Professional Documents
Culture Documents
Drawing Shapes
Drawing Shapes
Drawing Shapes
when a certain period oi time passes without any activity on a computer, a screensaver be-
comes active and draws difieient types of geometric patterns or textual messages.
In this section we will develop an application that simulates a screensaver. We will learn a
develop alone or as a project team member, we often find ourselves writing a class that
needs to behave in a specific way so that it works correctly with other classes The other
classes may come trom standard Java packages or could be developed by the other team
members.
in this particular case, we use the aningBoald class (written by the author). This
isa helper class that takes care ofprogramming aspectswe have not yet mastered, such as
moving multiple geometric shapes in a smooth motion across the screen. It is not an issue
of whethei we (an develop this (lass by ourselves, bKause no matte! how good we be—
We already used many predefined classes from the Java standard libraries. but
the way we will use the predefined class here is different. When we developed programs
belove,the classes we wrote called the methods oi predefined classes. Oul main method
creating a GregoriinCilendil object and calling its methods is one example. Here, tor
us to use a predefined class,we must define another class that provides necessary ser-
vices to this predefined class, Figure 5.14 ditierentiates the two types ot predefined
classesThe first type does not place any restriction other than calling the methods cor-
rectly. while the second type requires us to implement helper classes in a specific manner
to support it,
in mu (use, the piedelined (lass aningBarlld will vequive anothei Class named
shapes. So, to use the nraiivingaoard class in our program, we must implement the class
wayi The use of the DrawingBoald class dinatesthat we define a set of fixed methods in
the Drlwlhleshape class. We (an add more, but we must at the minimum piovide the
specified set oi fixed methods because the Drawlllgloard class will need to call these
parameters and theirtypes,and return type~tmt the method body can be defined in any
way we llke, This is how the flexlbility is achieved. For example, the Drawablesllipe class
we define must include a method named draw with the dictated signatuveBut it’s up to us
to decide what we put in the method body. So we can choose, for example, to implement
the method to draw a circle, rectangle, or any other geometric shape of our choosing.
The incremental development steps we will take here are slightly difierent in character
irom those we have seen so far. In the previous incremental developments, we knew all
the ingredients,soto speak. Here we have to include a stepto explore the oreirringaoard
class. We will find out shortlythat to usethe Drawingloard class, we will have to deal with
some Java standard classes we have not seen yet. Pedagogicaliy, a textbook may try to
Figure 5.” Twolypcs ntpeaefined classes theirs: type does not require us Io doarmhlng more
than use the predefined (lassa try talling their methodslhe setand type lequives us to define helper
classes (of IM plkdefined classes we wan! la “5!.
a time when we enmnler some urtan stages. We need to learn how to deal wrtit
vroblem Statement
Ihapes m diltererrt colaIL The um has the option ofchooxing a type (ellipsz or
Oven" Plan
We wlll begin with our avemll ptan lot the development. let's begln with the outlme at
program 109k. We first let the user yelect the shape, its mavemenL and its color, and we
then start drawing. We express the program flow as having taut tasks
Let‘s look a! eaen ask and decenninean nnjemnai will be iespunsilale lei nandling
me ask rel me filSK wee Ilsks,we can use our old lrlend 50mm. We will gel me me
delalls ol Exanly hnw we ask me users to input (hose value; in the late! inaeinemal
seeps. Fm me last [ask at annally diawing me seamed shape, we need m define our awn
classfile task is {co specifi: lo the progum, and lime is ad suiiable dine“ in me my»
dard padlagzs (hat does meioh As dimmed earllenwe will use a given pvedefinad (lass
on; dais (unimhape As explained "I Sedan 41mm will makethiscunlml amen
0m max task a to find out amuuhe gwen dais We mum have deangnea me Inpul your
(me; first. but wumeul knawmg men about me gnen (lass, n wamd be Menu la dailgn
aunaMe mpm muunes when we use an unknown (lass, n 13 mm! appmpnate mfind all!
move about 2m; gnen dag: heme we p‘an any mm“ or output roulmes Just aslhe de
\nstead enmng m find an! evemmng about me elaaa at onee,we new man we hasK
features and :keiemn eaae. As we \eam me about (he gwen (1355 maememaHy, we
Dmulnglolld
An inmn‘: Mfihis (lass wxll suppm the thawing a: Drawah I esnape med; Shapes
(0119(ny .5. me ween m. a... .5 5.. «.5. mum wwh m. Dminglnvd 5....
We need In veniy n... m ".5 5.e.. when . mmea window m... me mm back
5...“... we.” 5.. me ween. we know the m... em. e... e..ee...ea We.»
Drmlhlnshlpndass