require 'fltk' class Board < FLTK::Widget def initialize(w,h) size(w,h) @row = 0 @col = 0 end # x, y, w and h are local variables, since those are declared in this method. # You should not confuse Widget's method such as w() with local variable w. :) def resize(x, y, w, h) @unit = w/8 w = 8*@unit h = 8*@unit super(x,y,w,h) end def draw() super() drawGrid drawPiece(@row,@col, FLTK::BLACK) end def handle(code) if(code == FLTK::ENTER) return true end if(code == FLTK::MOVE) @row = FLTK::event_y / @unit @col = FLTK::event_x / @unit redraw() end return super(code) end def drawGrid FLTK::color(FLTK::BLUE) FLTK::rectf(x,y,w,h) FLTK::color(FLTK::WHITE) 0.step(w, @unit) do | col | FLTK::line(x+col, y, x+col, y+h) end 0.step(h, @unit) do | row | FLTK::line(x, y+row, x+w, y+row) end end def drawPiece(row,col, color) FLTK::color(color) thisX = col*@unit thisY = row*@unit FLTK::pie(thisX, thisY, @unit, @unit,0,360) end end class MyWindow < FLTK::DoubleWindow def initialize super(300, 300, 'Drawing'){|win| b = Board.new(w,h) resizable(self) yield(win) } end end MyWindow.new{|win| win.show() } FLTK::run()