How do I center a child morph object within the parent object?
Last updated at 8:36 am UTC on 14 January 2022
Open a Workspace and paste the following code into it.
parent := Morph new.
parent extent: 480@320.
parent color: Color white.
parent borderWidth: 50.
parent borderColor: Color lightBlue.
child := EllipseMorph new.
child color: Color red.
child extent: 180@180.
child borderWidth: 0.
parent addMorph: child.
"CENTER THE CHILD MORPH"
child center: parent center.
parent openInHand
Select the code and choose do-it.

In Cuis
- Need for a BorderedRectMorph
- Method #morphExtent: instead of #extent:
- No method #center; #referencePosition: is used instead.
parent := BorderedRectMorph new.
parent morphExtent: 480@320.
parent color: Color white.
parent borderWidth: 50.
parent borderColor: Color lightBlue.
child := EllipseMorph new.
child color: Color red.
child morphExtent: 180@180.
child borderWidth: 0.
parent addMorph: child.
"CENTER THE CHILD MORPH"
child referencePosition: parent referencePosition.
parent openInHand
For an application of #center: method see A4 and A3 sheets in Morphic.
tagBeginner