Question about flushing Display on Canvas

Milan Zimmermann milan.zimmermann at sympatico.ca
Wed May 4 06:49:55 UTC 2005


Bert, Thanks for the code sugggestions.

Before, I have implemented  (in a clumsy way that still needs cleanup but 
seems basically work) Lex's suggestion of extending the PluggableCanvas, and 
implementing the PluggableCanvas>>apply:aBlock method:

====
PluggableCanvas subclass: #DrawingCanvas
	instanceVariableNames: 'wrappedCanvas parentDrawingMorph'

DrawingCanvas>>apply: aBlock
	"Implements abstract in super PluggableCanvas, this method is called 
	from all drawing methods and allows the parent DrawingMorph to be 
	notified and refreshed by sending #changed."

	"Call whatever method apply: is wraping for the underlying wrappedCanvas."
	aBlock value: wrappedCanvas.

	" send #changed to the displayed DrawingMorph so it is refreshed."
	parentDrawingMorph changed.
====
then:
	drawingMorph := DrawingMorph new openInWorld.
	drawingCanvas := drawingMorph getDrawingCanvas.
	drawingCanvas line:5 at 5 to: 30 at 30 color:(Color red).
	...
====

But your code is simpler and instructive for me to learn from (the  
translated:clipping:during: part ... and more), and does not require two 
classes, as the stuff I did (DrawingMorph extending ImageMorph and  
DrawingCanvas extending PluggableCanvas).

Thanks both of you for help and code, Milan


On May 3, 2005 05:53 am, Bert Freudenberg wrote:
> Am 02.05.2005 um 23:13 schrieb Lex Spoon:
> > It would be great if there were a way that was as easy to draw in
> > Morphic as Milan is seeking.  If you draw onto the Display directly,
> > Squeak is smart enough to flush things immediately as you draw.  It's
> > not fast, but it's easy to use and understand.  On the other hand, if
> > you play by Morphic's rules, then what can you do?
>
> Implement a drawOn: method ;-)
>
> > It would be great if there were some sort of CanvasMorph.  It could be
> > used like this:
> >
> >     morph := CanvasMorph new.
> >     morph openInWorld.
> >
> >     canvas := morph canvas.
> >     canvas fillColor: (Color yellow).
> >
> > The response to #canvas, here, would be some kind of canvas that
> > automatically sends #changed to the morph whenever a message is
> > sent to
> > it.  You could easily implement such a canvas as subclass of
> > PluggableCanvas....
>
> Or, far simpler, just use a block:
>
> ============
> Morph subclass: #CanvasMorph
>      instanceVariableNames: 'drawBlock'
>      classVariableNames: ''
>      poolDictionaries: ''
>      category: 'Bert-Bla'
>
> initialize
>      super initialize.
>      self color: Color white.
>      self extent: 400 at 400.
>
> drawBlock: aBlock
>      drawBlock := aBlock.
>      self changed.
>
> drawOn: aCanvas
>      super drawOn: aCanvas.
>      drawBlock ifNotNil: [
>          aCanvas
>              translateBy: self bounds origin
>              clippingTo: self bounds
>              during: [:canvas | drawBlock value: canvas]]
> ============
>
> Then use like
>
>      m := CanvasMorph new openInWorld.
>
> to draw:
>
>      m drawBlock: [:c |
>          c line: 10 at 10 to: 100 at 100 color: Color red
>      ]
>
> or
>
>      m drawBlock: [:c |
>          c line: 0 at 0 to: m extent width: 10 color: Color green.
>          c line: 0 at m height to: m width at 0 width: 10 color: Color green.
>      ]
>
> With the latter you can even resize the morph ...
>
> - Bert -




More information about the Squeak-dev mailing list