Up North

Personal blog of Mikael Hallendal
  • Home
  • About

Drawing with Cairo in Ruby-Gtk2

Published by Mikael Hallendal on February 5, 2008 01:20 am under Programming

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
Share This

4 Comments so far

  1. Janne on February 5th, 2008

    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.

  2. Mikael Hallendal on February 6th, 2008

    Janne, glad to hear it was of use to you!

  3. HappyCoder on April 15th, 2008

    Thanks Mikael! Your post actually helped me.

  4. Mikael Hallendal on April 15th, 2008

    @HappyCoder, really glad to hear that!

Posting your comment.


  • Categories

    • Community (3)
    • Imendio (26)
    • Life (13)
    • Photography (1)
    • Press (1)
    • Programming (36)
    • Software (15)
    • Tips (8)
    • Uncategorized (180)
  • Most Popular Posts

    • Removing a remote branch in Git
    • Gtk+ natively on Mac OS X
    • GTK+ 3.0, enabling incrementalism
    • GIMP on Mac OS X with integrated menubar
    • Eclipse for C programming
  • Popular Tags

    c conference git gnome gossip gtk+ guadec Imendio linux loudmouth macosx ruby screencast web xmpp
  • My latest tweets

    • Thanks @tannermenard for posting the interview with me http://ping.fm/8nmVp I am not a guru however...just layman ;) 2009-04-02
    • 哈哈。。。 2009-04-01
    • No censorship, tag freely though :) If someone needs censored reality, filter out certain tags they must. 2009-04-01
    • More updates...
  • Feeds

    • Entries
    • Comments

Copyright © 2010 Mikael Hallendal
WordPress Theme based on Light Theme

Close
  • Social Web
  • E-mail
  • del.icio.us
  • Digg
  • Furl
  • Yahoo! My Web
  • StumbleUpon
  • Google Bookmarks
  • Technorati
  • BlinkList
  • Newsvine
  • ma.gnolia
  • reddit
  • Tailrank
  • 9rules Clips
E-mail It