11
Graphics Object

Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

Embed Size (px)

Citation preview

Page 1: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

Graphics Object

Page 2: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

2

Group Activity!

• In each group, pull out one piece of blank paper and one pen or pencil

• Start with the piece of paper on the left side of the group. We’ll need one pencil or pen for each group.

• Once everybody’s ready, we’ll go to the next slide.

Page 3: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

3

Graphics Object

• First : Draw a basic landscape background—some hills and maybe a lake.

• Second person (board/paper): Draw a few foreground objects, like trees or a lake.

• Third person: Add some man-made objects, like houses or cars.

• Fourth person: Add some people.• Fifth person (if there is one): Add something

else.

Page 4: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

4

Graphics Object

• In VB, we call the pieces of paper “graphics objects.” They are instances of the Graphics class.

• Each of you would be instances of classes that know how to draw.

• In the Marching Band program, the BandMember class knows how to draw in detail.

• The Rank class knows how to draw by delegation—by telling other objects to draw. I played the role of Rank in this demonstration.

Page 5: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

5

Graphics Object• In VB, Graphics objects are generally associated with either

PictureBoxes or Forms.• A Graphics object (it’s called “g” in the program) can be

passed around like the sheets of paper and drawn on by objects that know how to draw.

• In this program, g is created as soon as the form is created:

In assignment 3, the Graphics object is created in the Paint event.

Page 6: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

6

Updating the Picture• In the marching band program, every time the

Timer ticks, the same things happen:– The previous drawing is erased.– The rank moves forward.– The rank gets drawn in its new location.

Page 7: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

7

Passing the Paper• The line “Rank1.Draw(g)” begins the virtual

passing of the paper.• Rank1.Draw(g) calls the Draw routine of the Rank

class:

• As you can see, the only thing that this Draw sub does is tell each BandMember to draw themselves, while passing them the graphics object g, one after the other, so they’ll know WHERE to draw themselves.

Page 8: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

8

Finally! Someone to do the work!• The Draw sub in the BandMember class is the first one that actually draws

something.• The two highlighted lines are the only ones that draw.• They do this by calling built-in subroutines of the Graphics class: FillRectangle

and DrawLines.• VB.NET Graphics class has many drawing subroutines for drawing and filling

shapes, text, and patterns.

Page 9: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

9

Drawing the M• The DrawLines method takes a Pen object and an array of points as its parameters• It connects the points with lines in the order they are in the array• A block M can be drawn in any of the four directions using the same five points:

North East South West

• In the Draw sub in the BandMember class, the same five points are assigned to different members of the array, as shown above.

Page 10: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

10

M Code: Lengthy version• The Draw sub in the BandMember class is the first one that actually draws

something.• The two highlighted lines are the only ones that draw.• They do this by calling built-in subroutines of the Graphics class: FillRectangle

and DrawLines.• VB.NET Graphics class has many drawing subroutines for drawing and filling

shapes, text, and patterns.

Page 11: Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side

11

M Code: Simplified (sort of)• Here is a slightly more compact version of drawing the M.• In general, shorter code is preferred. In this case, though, it is pretty much the same

either way; for a given direction, approximately the same number of lines of code run in either version.