Ruby Forum wxRuby > Transparent line in Frame

Posted by Andreas Warberg (awarberg)
on 21.04.2008 14:39
Hi

Thanks for a great GUI toolkit!

I want to draw transparent lines in a Frame. I have the following setup
but my lines are not transparent.

My colour object is created using:
   transparent_blue = Colour.new(0,0,255,100)

and I draw lines using a pen with this colour:
   @pen = Pen.new(transparent_blue)

In evt_paint of frame I have:
   dc.set_pen(@pen)
   dc.draw_line(x1,y1,x2,y2)

The line is blue but there is no transparency.

I set the alpha level to 100 where 255 is supposed to be completely
opaque.

I'm on Windows XP SP2 and

C:\>ruby --version
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Please help!

Regards, Andreas
Posted by Alex Fenton (Guest)
on 21.04.2008 15:13
(Received via mailing list)
Andreas Warberg wrote:
> I want to draw transparent lines in a Frame. I have the following setup
> but my lines are not transparent.
The standard DC doesn't support transparency (except on OS X I think).
But you can instead use wxGraphicsContext which fully supports
transparency, antialiasing and other advanced drawing tools like splines
and transforms. You create one from an ordinary DC like this:

paint.do | dc |
  gdc = Wx::GraphicsContext.create(dc)
  gdc.stroke_line(0, 0, 500, 500)
  # etc
end

Unfortunately the wxWidgets developers decided to make the API to
GraphicsContext different to DC, but the results are worth it for
graphical apps.

alex
Posted by Andreas Warberg (awarberg)
on 21.04.2008 16:20
Alex Fenton wrote:
> The standard DC doesn't support transparency (except on OS X I think).
> But you can instead use wxGraphicsContext which fully supports
> transparency, antialiasing and other advanced drawing tools like splines
> and transforms. You create one from an ordinary DC like this:
> 
> paint.do | dc |
>   gdc = Wx::GraphicsContext.create(dc)
>   gdc.stroke_line(0, 0, 500, 500)
>   # etc
> end

Thanks! I'm having a bit of trouble, though. Here is a small example I 
use:

require 'wx'
include Wx

App.run do
  frame = Frame.new(nil, :title => 'GraphicsContext example', :width => 
[300,400])

  frame.evt_paint do
    frame.paint do |dc|
      gdc = GraphicsContext.create(dc)
      siz = dc.size
      h, w = siz.height, siz.width
      #dc.draw_line(0,0,w,h)
      gdc.stroke_line(0,0,w,h)
    end
  end

  frame.show
end

It should draw / stroke a diagonal line in the frame. This works if I 
use draw_line.

What am I missing? :)

Andreas
Posted by Alex Fenton (Guest)
on 21.04.2008 16:35
(Received via mailing list)
Andreas Warberg wrote:
>   frame.evt_paint do
>     frame.paint do |dc|
>       gdc = GraphicsContext.create(dc)
>       siz = dc.size
>       h, w = siz.height, siz.width
>       #dc.draw_line(0,0,w,h)
>   
      gdc.pen = Wx::Pen.new( Wx::Colour.new(255, 0, 0, 128), 20)
> What am I missing? :)
It seems with GraphicsContext, you need to set some sort of pen before
starting drawing - there is no default pen when it's first created.

alex
Posted by Andreas Warberg (awarberg)
on 21.04.2008 16:47
Alex Fenton wrote:
> Andreas Warberg wrote:
>>   frame.evt_paint do
>>     frame.paint do |dc|
>>       gdc = GraphicsContext.create(dc)
>>       siz = dc.size
>>       h, w = siz.height, siz.width
>>       #dc.draw_line(0,0,w,h)
>>   
>       gdc.pen = Wx::Pen.new( Wx::Colour.new(255, 0, 0, 128), 20)
>> What am I missing? :)
> It seems with GraphicsContext, you need to set some sort of pen before
> starting drawing - there is no default pen when it's first created.
> 
> alex

Thanks!