[Newbies] problems moving and resizing quadrangles

Alan Gauld alan.gauld at btinternet.com
Wed Dec 28 10:12:28 UTC 2011

In my tutorial I've been building a quadrangle editor.

The idea is that there are two menuus. One appears if I click on the 
background, the other if I click inside a quadrangle. In the quad menu I 
have options to move, resize, set color and delete the quadrangle that 
is being clicked. It all works ok except for the resize and move options.

Here is the menu building code that holds the critical blocks.
What I'm trying to do in both cases is create a new quadrangle from the 
original and replace the original in the OrderedCollection which is the 
super class for the editor.

	"edit a quadrangle"
	| menu cmdMenu quadMenu finished point
           actions quadActions cmdActions select qd |

	quadMenu := PopUpMenu labels: 'move
set to red
delete' lines: #(3 ).
	quadActions := Array new: 4.
		at: 1
		put: [:q | self
				at: (self whichIndex: q)
				put: (q translateBy: Point fromUser)].
		at: 2
		put: [:q | self
				at: (self whichIndex: q)
				put: (q scaleBy: Point fromUser)].
		at: 3
		put: [:q | q insideColor: Color red].
		at: 4
		put: [:q | super
				removeAt: (self whichIndex: q)].

whichIndex is a new method I added to the editor, and it seems to work 
since the remove menu action works OK.
It looks like:

whichIndex: aQuad
	"returns index containing aQuad or nil"
	1 to: lastIndex
		do: [:i | (self at: i) == aQuad
				ifTrue: [^ i]].
	^ nil

I assume I'm misunderstanding how translateBy and scaleBy work, or 
something? But I'm stuck. Any clues or pointers welcomed!

Alan G
Author of the Learn to Program web site

More information about the Beginners mailing list