# How can I use draw line in wxruby?

**URL:** <https://www.ruby-forum.com/t/how-can-i-use-draw-line-in-wxruby/124757>\
**Category:** wxRuby\
**Created:** [January 2, 2008, 9:16am UTC](https://www.ruby-forum.com/t/how-can-i-use-draw-line-in-wxruby/124757 "2008-01-02T09:16:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [January 2, 2008, 9:16am UTC](https://www.ruby-forum.com/t/how-can-i-use-draw-line-in-wxruby/124757/1 "2008-01-02T09:16:50Z")

</div>

I use

dc = DC.new()  
dc.draw\_line(50, 50, 100, 100)

but it can’t be compiled  
I don’t know how to declare DC variable??  
in [http://wxruby.rubyforge.org/doc/dc.html](http://wxruby.rubyforge.org/doc/dc.html)  
it don’t tell me how to declare now variable  
plese tell me 😢

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [January 2, 2008, 10:39am UTC](https://www.ruby-forum.com/t/how-can-i-use-draw-line-in-wxruby/124757/2 "2008-01-02T10:39:17Z")

</div>

Hello Bass Guitar,

Unfortunately, you cannot instantiate the DC (Device Context) directly  
from  
the DC Class. You need to utilize Window#draw or Window#paint to create  
a  
DC. The difference between these two methods, is that when you are in a  
paint event (EG: evt\_paint), you utilize the Window#paint method to  
acquire  
a Device Context. When your outside of the paint event, you utilize  
Window#draw to acquire the Device Context.

An Example of inside an paint event would be this:

evt\_paint() do  
self.paint do |dc|  
dc.draw\_line(50, 50, 100, 100)  
end  
end

And an example of how to draw outside of a paint event:

def run\_draw()  
self.draw do |dc|  
dc.draw\_line(50, 50, 100, 100)  
end  
end

Once you implement this, it will work.

L8ers,

Mario S.

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [January 2, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/how-can-i-use-draw-line-in-wxruby/124757/3 "2008-01-02T16:34:01Z")

</div>

Mario S. wrote:

> Unfortunately, you cannot instantiate the DC (Device Context) directly  
> from the DC Class. You need to utilize Window#draw or Window#paint to  
> create a DC. The difference between these two methods, is that when  
> you are in a paint event (EG: evt\_paint), you utilize the Window#paint  
> method to acquire a Device Context. When your outside of the paint  
> event, you utilize Window#draw to acquire the Device Context.  
> Just a quick follow-up; Mario’s quite right that you use the  
> Window#paint method to acquire a DeviceContext to do drawing. It is  
> recommended that this is within an evt\_paint handler which means that  
> drawing will only be done when necessary. The sample image/images.rb is  
> probably the simplest example to look at.

However, there isn’t a “draw” method as far as I know. “paint” is used  
in all circumstances, but the exact type of DC will be different  
dependent on the, um, context. But there will be no functional  
difference

hth  
alex

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [January 3, 2008, 1:28pm UTC](https://www.ruby-forum.com/t/how-can-i-use-draw-line-in-wxruby/124757/4 "2008-01-03T13:28:01Z")

</div>

thank you 😃
