Home Segments Index Top Previous Next

681: Mainline

Whenever you make a change to the value of a graphics context variable, you should first obtain the current value, and you should restore that value later.

Suppose, for example, that you want to change the color of the lines, but not to change the text. You use the foreColor method to obtain the current color associated with the graphics context. Then, you use foreColor: to change to another color temporarily. Finally, you use foreColor: again to restore the graphics context to the original state:

MeterGraphPane method definition • instance
drawMeter
  | thePen meterWidth xLft yBot oldColor oldWidth |
  meterWidth := self width * 3 // 4.
  xLft := self width - meterWidth // 2.
  yBot := self height // 2.
  thePen := self pen.
  "Remember old values"                                         
  oldColor := thePen foreColor.                                 
  oldWidth := thePen width.                                     
  "Set new values"                                              
  thePen setLineWidth: 5.                                       
  thePen foreColor: ClrRed.  	                                
  thePen lineFrom: xLft @ yBot to: (xLft + meterWidth) @ yBot.
  thePen lineFrom: (xLft + (meterWidth // 2)) @ yBot
               to: (xLft + (meterWidth // 2))
                   @ (yBot - 10).
  "Reset to old values"                                         
  thePen setLineWidth: oldWidth.                                
  thePen foreColor: oldColor.                                   
  thePen centerText: 'Untitled' 
                 at: ((self width) // 2)  
                      @ ((self height // 2) 
                         + (2 * thePen font height)).