Drawing Shapes

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

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

development skill very commonly used in objectoriented programmingwhether we

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—

come as programmers, we would rarely develop an application completely on our own.

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

DrawableShape that will assume the responsibility oi drawing individual geometric

shapes. So, to use the nraiivingaoard class in our program, we must implement the class

named DrawableShape. And we must implementthe oravrablesmpe class in a specific

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

methods, The methods ate”fixed"in the method signatuie—method name,the numbev of

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.

As always,we will develop this program lollowing incremental development steps.

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!.

explain beforehand everything that is neeessary m undevsmd the sample pmgiams

But no texdaoaltean explain everything. When we develop pnagramsthere will always h!

a time when we enmnler some urtan stages. We need to learn how to deal wrtit

such a situation in our development steps

vroblem Statement

Writean appll'mkian thatwnulates a saeensam bydmwirtg various geometric

Ihapes m diltererrt colaIL The um has the option ofchooxing a type (ellipsz or

mtangIEL mlamndmavemem tsratiarrary, smooth, or random).

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

mm I. Gel the shape the user wants lo draw.

“'“ 2. Gel the tolornlthe thosen shape.

2. Gel the type at movement the user wants m use.

4. Start the drawing.

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

Null-guild and define me iequiied helper class nun-Mesh.”


We will define a lap-level mmml whim inai Manges all mese dbiens. We will all

on; dais (unimhape As explained "I Sedan 41mm will makethiscunlml amen

(he main class. were: aurworklng design downlth

We w." .mpxemenums program m lhefollewmg 5m mam! sup;

1. Stan man a program akelenan. Explove the Dvawlngbaard elaaa

2. Define an expenmemal Dvwablushiy- (lass (ha! dvaws a dummy shape

a. Add (one In auowme usev menu a shape Extend me Dvawabllshapl and

cthev dam: as neeessary

g Add (me la auowme usev mapaewy me (alonExlend me anablashay- and

cthev (wage; as naeesxary.

5. Add (MemaHowme usenaspeewyme monan type Extend me Diwahhshap-

and other classes as necessaq

s. Finalize the code by lymg up loose end;

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

vempmenz steps ave .nuemenral, our exploration at me gnen dag: mu be maemenxal

\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

extend am (one mrrespundmgly

Slep ‘ Develapmenl: Pragram skelean

We begmlhe deve‘opmenlwwth we deem mam (lassfhe mam a..wa .n axepv me


use me Dvwlnglwvd dais ‘41 me sumpiest manner m embhan me 4annm pad by me

deve‘opmenl to do so, we must inst \eam a an aaam me gnen Drawingbaud (lass

Hews a and aesmpnon 0! me anlngloam elaaa \n a .eaWana annauame wanna

be nnmng an! about (he gwen dag: by veadmg n: aunmpanymg dommenlamn or

same emev gxtema‘ souKeLThe dammemauan may come m 1mm 0! mm .avaaae

dommenls or ye4eyenee mama‘s,

Dmulnglolld

An inmn‘: Mfihis (lass wxll suppm the thawing a: Drawah I esnape med; Shapes

can be dawn mind smtmnary pagnanaa. randnm pmitmnsmr in a 5mm mmicm

mnexpeanea speedme anualdvawing anne mdeua‘ shapesis dun: \nsxdetht

Drawableshapedass vneenen. pmgvammevdamdcswhwh Shapelu dvaw.

Among m. mm memodsnue 5:: m. mum. mmd is m: an: m mflkz

n appeav an [M xtmen All other mhudfi mm m mm mum.» mum;

and mm; m morale: a . mum-om men. We wvll lxpllln [he sundard

Mammy!" dag whm wens! 1M Mum mm m the lalev step In this

slenwe wlll keep [hemdevery smoke by my making I! aware" the mama mu

deal wilh mhu methndx m m. \anvslzps

mmamg mgn don-men! fauna china-45mm“ “mm;

The 9......» of 5m. . .em .5 m vmiy m. . DrmingBo-vd emee. we...

(0119(ny .5. me ween m. a... .5 5.. «.5. mum wwh m. Dminglnvd 5....

.. .5 Wham: a... we 5.e .m. undamnndmg .5 “meme... MN m... 5mm.

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.»

A1291 we vuliy me mee. em...“ 5.... 5.e.. . mg”... we w... me... m We

men. mm... methuds 5. (hSDrMhlp: m... gnaw... u...“ up [he .eq....ee

Drmlhlnshlpndass

Sup} Memo”... Dvaw . shape

.. the m... deve‘nvmem 5.e.. we w... .mplemew. . plehmlnavy gunmen...

5.... and makz 5m we. 5pm. cm . nnmuum wmdnw r5 draw 5.....e5

we Med meme." .5 . amuse... wmdww 5......) e... meg need .5 dzfinum

You might also like