Home Segments Index Top Previous Next

671: Mainline

You can use the pen to display text as well as lines. The display work is done by displayText:at: or centerText:at: messages. In the following, one argument of centerText:at: is a string to be printed, and the other specifies a position just under the primitive meter.

The exact distance under the primitive meter is twice the height of the font. To determine that height, you send a font message to extract the current font instance associated with the pen, and then you send a height message to determine the font instance's height:

MeterGraphPane method definition • instance 
drawMeter 
  | thePen meterWidth xLft yBot | 
  meterWidth := self width * 3 // 4. 
  xLft := self width - meterWidth // 2. 
  yBot := self height // 2. 
  thePen := self pen. 
  thePen lineFrom: xLft @ yBot to: (xLft + meterWidth) @ yBot. 
  thePen lineFrom: (xLft + (meterWidth // 2)) @ yBot 
               to: (xLft + (meterWidth // 2)) @ (yBot - 10). 
  thePen centerText: 'Untitled' 
                 at: ((self width) // 2)  
                      @ ((self height // 2) 
                         + (2 * thePen font height)). 

Using the definition of drawMeter provided in this segment, you produce the following display: