0

Smalltalk: Drawing using Morphic

In this post, I will demonstrate how to use basic drawing function in Squeak Smalltalk.

Screenshots

Here is how the code looks like in Squeak Smalltalk.

Create Form and Draw dotted line

"Create Window"
window := SystemWindow new.
window setLabel:'New Window'.
pasteUpMorph := PasteUpMorph new.
window addMorph: pasteUpMorph frame: (0@0 corner: 1@1).
window openInWorld.

"Create ImageMorph"
myImageMorph := ImageMorph new.
pasteUpMorph addMorph: myImageMorph.
myImageMorph newForm: (Form extent: (pasteUpMorph extent) depth: 32).
myImageMorph bounds: (pasteUpMorph bounds).

"Create Pen and use it to draw on ImageMorph"
myPen := Pen newOnForm: myImageMorph image.
myPen squareNib: 3.
myPen color: Color blue.

1 to: 100 do: [:x |
		posx := 50 + (x * 5).
		myPen place: posx@50.
		myPen go: 0@0.
	 ].  
window changed.

Results

Explanation

The code is self-explanatory, but what I did here is that I created a new SystemWindow and added a PasteUpMorph to it so that content can be added to it. Next step was to make the window visible, and once that was done I added an ImageMorph to it for drawing purpose. ImageMorph is useful for drawing functions as a Pen object can be used to draw on it. And then, in the last step, I created a blue pen with 3 points nib and used it to draw dots on the image using a loop. That’s all! and you get the above result.


Code at GitHub

humayun