# Drawing in a GUI

**URL:** <https://rubytalk.org/t/drawing-in-a-gui/14835>\
**Category:** ruby-talk\
**Created:** [16 November 2004 23:33 UTC](https://rubytalk.org/t/drawing-in-a-gui/14835 "2004-11-16T23:33:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Van\_Dyk](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@Joe\_Van\_Dyk](https://rubytalk.org/u/Joe_Van_Dyk)\
**Post date:** [16 November 2004 23:33 UTC](https://rubytalk.org/t/drawing-in-a-gui/14835/1 "2004-11-16T23:33:19Z")

</div>

So, I have a bunch of objects that are responsible for drawing on something  
on the screen (in this case, a GnomeCanvas object).

Say I need to have a grid drawn on this canvas. In order to draw a line, I  
need to call Gnome::CanvasLine with a reference to the GnomeCanvas I want to  
draw on (and various other details about the line).

If I have a Grid class

class Grid  
&nbsp;&nbsp;def draw\_grid  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end  
end

How should I tell Grid what GnomeCanvas to draw on? Pass it in when it's  
initialized? For some reason, that doesn't seem like the best thing to do.

Any ideas?

---

<div class="post-metadata">

**Author:** ![Lyndon\_Samson](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@Lyndon\_Samson](https://rubytalk.org/u/Lyndon_Samson)\
**Post date:** [17 November 2004 01:17 UTC](https://rubytalk.org/t/drawing-in-a-gui/14835/2 "2004-11-17T01:17:22Z")

</div>

> How should I tell Grid what GnomeCanvas to draw on? Pass it in when it's  
> initialized? For some reason, that doesn't seem like the best thing to do.

Using components have something like

paint(paintContext)

> **···**
>
> On Wed, 17 Nov 2004 08:33:19 +0900, Joe Van Dyk \<joe.vandyk@boeing.com\> wrote:
> 
> > Any ideas?

---

<div class="post-metadata">

**Author:** ![Brian\_SchrA\_der](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@Brian\_SchrA\_der](https://rubytalk.org/u/Brian_SchrA_der)\
**Post date:** [17 November 2004 11:43 UTC](https://rubytalk.org/t/drawing-in-a-gui/14835/3 "2004-11-17T11:43:44Z")

</div>

As far as I know, a gnome-canvas remembers its objects. So why don't you subclass the Grid from a CanvasGroup and pass it the canvas on creation?  
Ok, I see that this has the disadvantage of coupling you a lot faster to GnomeCanvas, but otherwise you will have problems when changing the grid, because you can't simple draw a new grid (the line-objects on the canvas), but have to delete the old grid first.

I hope this is not totally confusing or unhelpfull.

Regards,

Brian

> **···**
>
> On Wed, 17 Nov 2004 08:33:19 +0900 "Joe Van Dyk" \<joe.vandyk@boeing.com\> wrote:
> 
> > So, I have a bunch of objects that are responsible for drawing on something  
> > on the screen (in this case, a GnomeCanvas object).
> > 
> > Say I need to have a grid drawn on this canvas. In order to draw a line, I  
> > need to call Gnome::CanvasLine with a reference to the GnomeCanvas I want to  
> > draw on (and various other details about the line).
> > 
> > If I have a Grid class
> > 
> > class Grid  
> > &nbsp;&nbsp;def draw\_grid  
> > &nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;end  
> > end
> > 
> > How should I tell Grid what GnomeCanvas to draw on? Pass it in when it's  
> > initialized? For some reason, that doesn't seem like the best thing to do.
> > 
> > Any ideas?
> 
> --  
> Brian Schröder  
> [http://www.brian-schroeder.de/](http://www.brian-schroeder.de/)
