Drawing with Cairo in Ruby-Gtk2
With my rekindled love for Ruby I started hang around in #ruby-lang again and got the question how you use Cairo to draw inside a Ruby-Gtk2 program. I cooked up a small example and figured that sharing it here might be useful for others as well.
Otherwise you can just enjoy the cleanness of Ruby
#!/usr/bin/env ruby
require 'gtk2'
window = Gtk::Window.new
window.signal_connect("delete-event") do
Gtk.main_quit
true
end
area = Gtk::DrawingArea.new
area.set_size_request(100,100)
area.signal_connect("expose-event") do
cairo = area.window.create_cairo_context
cairo.rectangle(10, 10, 50, 50)
cairo.fill
true
end
window.add(area)
window.show_all
Gtk.main
And just in time too! I installed the ruby-gnome bindings just yesterday, thinking I want to use cairo or similar for visualization. Thanks for short-circuiting that initial learning phase for me so neatly.
Janne, glad to hear it was of use to you!
Thanks Mikael! Your post actually helped me.
@HappyCoder, really glad to hear that!