5.3.USINGGRAPHICALOBJECTS 67
Circley:x:
100100center: Point
radius:draw( )
.
.
.GraphWin
.
.
.Low-level drawing commandswin:30circ:Figure5.5:Objectinteractionstodraw a circle.Incorrect way to createtwo circles.
leftEye = Circle(Point(80,50), 5)
leftEye.setFill(’yellow’)
leftEye.setOutline(’red’)
rightEye = leftEye
rightEye.move(20,0)
Thebasicideais tocreatethelefteyeandthencopy thatintoa righteyewhichis thenmovedover 20units.
Thisdoesn’t work.Theproblemhereis thatonlyoneCircleobjectis created.Theassignment
rightEye = leftEye
simplymakesrightEyerefertotheverysamecircleasleftEye. Figure5.6showsthesituation.When
theCircleis movedinthelastlineofcode,bothrightEyeandleftEyerefertoit initsnew location
ontherightside.Thissituationwheretwo variablesrefertothesameobjectis calledaliasing, andit can
sometimesproduceratherunexpectedresults.
rightEye:leftEye: Circley:x:
5080center: Point
radius: 10Figure5.6:VariablesleftEyeandrightEyearealiases.Onesolutiontothisproblemwouldbetocreatea separatecircleforeacheye.A correct way to createtwo circles.
leftEye = Circle(Point(80,50), 5)
leftEye.setFill(’yellow’)