ext/tk/sample/tkline.rb


DEFINITIONS

This source file includes following functions.


   1  
   2  require "tkclass"
   3  
   4  $tkline_init = FALSE
   5  def start_random
   6    return if $tkline_init
   7    $tkline_init = TRUE
   8    if defined? Thread
   9      Thread.start do
  10        loop do
  11          sleep 2
  12          Line.new($c, rand(400), rand(200), rand(400), rand(200))
  13        end
  14      end
  15    end
  16  end
  17  
  18  $c = Canvas.new
  19  $c.pack
  20  $start_x = start_y = 0
  21  
  22  def do_press(x, y)
  23    $start_x = x
  24    $start_y = y
  25    $current_line = Line.new($c, x, y, x, y)
  26    start_random
  27  end
  28  def do_motion(x, y)
  29    if $current_line
  30      $current_line.coords $start_x, $start_y, x, y
  31    end
  32  end
  33  
  34  def do_release(x, y)
  35    if $current_line
  36      $current_line.coords $start_x, $start_y, x, y
  37      $current_line.fill 'black'
  38      $current_line = nil
  39    end
  40  end
  41  
  42  $c.bind("1", proc{|e| do_press e.x, e.y})
  43  $c.bind("B1-Motion", proc{|x, y| do_motion x, y}, "%x %y")
  44  $c.bind("ButtonRelease-1", proc{|x, y| do_release x, y}, "%x %y")
  45  Tk.mainloop