Drawing the shape's appearance
The draw method is a simple one. It checks the shape attribute's value to determine which Smalltalk method to call for the actual drawing. This way, you can easily support many different kinds of shapes (including circles, triangles, and rectangles) in a single part class. This means you need to write two instance methods, draw, and circle.
draw
"Draw the shape"
self widget isNil ifFalse: [
self shape = 'circle' ifTrue: [self circle].
self shape = 'rectangle' ifTrue: [self rectangle].
self shape = 'triangle' ifTrue: self triangle].
].
 
Note:
If you are an experienced Smalltalk programmer, you might be tempted to use the perform: message instead of sending a specific message for drawing each shape. Using perform: this way usually causes errors when you package stand-alone applications. This is because VA Smalltalk only automatically packages the methods that are referenced explicitly in your code. If you had used perform: in the method above, the methods circle, rectangle, and triangle would be missing from the stand-alone application. These methods can be included in a packaged application using packaging rules.
Using perform: is actually better practice, but for simplicity this section uses the straight-forward approach. After you are adept at packaging and prerequisites you can consider using the perform: method.
Because the shape attribute has a value of 'circle' by default, the draw method will call the circle method:
circle
"Ask the widget to draw a filled circle"
self widget drawFilledCircle
The actual drawing commands are done by the widget, which you'll write in the next section. Later, you'll add the rectangle and triangle methods to support other kinds of shapes.
Last modified date: 01/29/2015