Using Gtk::DrawingArea#queue_draw to refresh the area

#! /usr/bin/env ruby

# file: rectangle.rb

require 'gtk2'

area = Gtk::DrawingArea.new

x1 = 150
area.signal_connect("expose_event") do

  y1 = 30; x2 = 200; y2 = 70
  area.window.draw_rectangle(area.style.fg_gc(area.state), 1, x1, y1, x2, y2)

end

Gtk::Window.new.add(area).show_all

Thread.new do
  300.times { x1 -= 1; area.queue_draw; sleep 0.01}
end

Gtk.main

In the above example the Gtk::DrawingArea#queue_draw method is used to repaint, refresh, or redraw the DrawingArea which contains an instruction to draw a rectangle. The x1 position is changed, making the rectangle move closer to the left every time the queue_draw method is called.

Note: The application can automatically instruct itself to redraw without using queue_draw, however that's only when the viewing area changes from a windowing event (e.g. The main application window is moved by a window drag event).

Resources