One way of creating new graphical representations is by placing one morph inside another. This is called composition; morphs can be composed to any depth. You can place a morph inside another by sending the message
addMorph: to the container morph.
\(\bigstar\) Try adding a morph to another one:
star := StarMorph new color: Color yellow. joe addMorph: star. star position: joe position.
The last line positions the star at the same coordinates as joe. Notice that the coordinates of the contained morph are still relative to the screen, not to the containing morph. There are many methods available to position a morph; browse the geometry protocol of class
Morph to see for yourself. For example, to center the star inside joe, execute
star center: joe center.
If you now try to grab the star with the mouse, you will find that you actually grab joe, and the two morphs move together: the star is embedded inside joe. It is possible to embed more morphs inside joe. In addition to doing this programmatically, you can also embed morphs by direct manipulation.
\(\bigstar\) From the world menu select “objects” and click on the button labeled “Graphics”. Drag an ellipse and a star from the Supplies flap. Place the star over the ellipse, and yellow-click the star to bring up a menu1. Select embed into ⊳ Ellipse. Now the star and the ellipse move together.
To remove a sub-morph, do
joe removeMorph: star or
star delete. This can also be done by direct manipulation:
\(\bigstar\) Blue click twice on the star. Drag the star away from the ellipse using the grab handle.
The first click brings up the morphic halo on the ellipse; the second click the halo on the star. Each click moves the focus down one level of embedding.
- You can also blue-click the star to bring up the morphic halo, and then click the red menu handle.