Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
21.5.2 Adding <strong>an</strong> inst<strong>an</strong>ce creation method<br />
Now we have a class called BoxDrawFigure, we need some way of inst<strong>an</strong>tiating this class. HotDraw<br />
expects a figure’s inst<strong>an</strong>ce creation method to be called createAt: (rather th<strong>an</strong> new or open with<br />
which you may be more familiar). We will therefore define a method createAt: in the protocol<br />
“inst<strong>an</strong>ce creation”:<br />
createAt: aPoint<br />
^self new initializeAt: aPoint<br />
This method first creates a new inst<strong>an</strong>ce of the BoxDrawFigure <strong><strong>an</strong>d</strong> then sends it <strong>an</strong><br />
initializeAt: message with aPoint as the current cursor position . The initializeAt:<br />
method will actually draw the figure <strong><strong>an</strong>d</strong> its component elements. We will look at this method below.<br />
21.5.3 Defining a creation tool<br />
While we are still on the class side, we shall define the creationTool method which specifies what<br />
icon to use in the ToolPalette for a BoxDrawFigure. Rather th<strong>an</strong> define a new<br />
creationTool class, we will provide a creationTool method which will return the default<br />
creationTool, parameterized for BoxDrawFigure. This method will be defined in the class protocol<br />
“tool creation”. The creationTool method is:<br />
creationTool<br />
^Tool<br />
icon: (Image<br />
extent: 16 @ 16<br />
depth: 1<br />
palette: MappedPalette blackWhite<br />
bits: #[255 255 255 255 255 255 128 1<br />
191 253 160 125 191 253 160 197<br />
191 253 160 125 191 253 28 1<br />
255 255 255 255 255 255 255 255]<br />
pad: 8)<br />
cursor: Cursor crossHair<br />
class: self<br />
creationMessage: #createAt:<br />
This method is based on that defined in PERTEvent in one of the HotDraw example applications. It<br />
returns <strong>an</strong> inst<strong>an</strong>ce of CreationTool which uses a particular icon on class BoxDrawFigure <strong><strong>an</strong>d</strong><br />
uses a crossHair (rather th<strong>an</strong> origin) style cursor. Note that the creationMessage is specified here.<br />
We could have used a different creation message to that used elsewhere in HotDraw. However, to<br />
remain consistent with existing HotDraw applications we used createAt:.<br />
21.5.4 The initializeAt: method<br />
We now need to define what the elements of this drawing will be. That is, “what the figure will look<br />
like”. This is done within initializeAt: defined in “initialize” protocol on the inst<strong>an</strong>ce side of the<br />
class. The code for this method is:<br />
initializeAt: aPoint<br />
| title myFigures aRect<strong>an</strong>gle |<br />
"Get the bounding rect<strong>an</strong>gle"<br />
aRect<strong>an</strong>gle := aPoint extent: 90 @ 45.<br />
origin := aRect<strong>an</strong>gle origin.<br />
"Position the text field relative to the origin"<br />
title := FixedTextFigure string: 'A Name' at: origin + (5 @ 5).<br />
title maxLength: 65.<br />
"Add the text field to the subelements of this figure"<br />
myFigures := (OrderedCollection new) add: title; yourself.<br />
self setFigures: myFigures visibleArea: aRect<strong>an</strong>gle.<br />
"Draw the bounding box of this figure"<br />
self showVisibleAreaIndicator.<br />
This method first creates a rect<strong>an</strong>gle whose right h<strong><strong>an</strong>d</strong> corner is indicates by aPoint <strong><strong>an</strong>d</strong> whose extent<br />
is 90 by 45. Having done that, it then sets the origin of the figure relative to the rect<strong>an</strong>gle. Next it<br />
creates a text field at a particular point <strong><strong>an</strong>d</strong> then uses the bounding box of the figure to draw the shape of<br />
the figure (in this case a rect<strong>an</strong>gle).<br />
178