Download presentation
Presentation is loading. Please wait.
Published byMeghan Welch Modified over 9 years ago
1
Graphics Object
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. 2
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. 3
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. 4
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: 5 In assignment 3, the Graphics object is created in the Paint event.
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. 6
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: 7 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.
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. 8
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: 9 NorthEastSouthWest In the Draw sub in the BandMember class, the same five points are assigned to different members of the array, as shown above.
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. 10
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. 11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.