Ruby Tk Canvas Drawing

Hello all,

I’m trying to draw a network graph in Ruby using Tk. I’ve figured out
how to create the canvas and draw the nodes and edges. I’m in the
process of implementing a spring graph layout algorithm, and I’m
wondering how I can redraw the nodes and edges at new coordinates when
the new coordinates are calculated using the spring algorithm. I had
planned on creating a new thread every half second or so to call the
spring algorithm and repaint the nodes and edges.

Can anyone tell me how to ‘move’ an existing TckOval in Ruby Tk?


Thanks!
Bryan

On Thu, Apr 3, 2008 at 11:09 AM, Bryan R.
[email protected] wrote:

Hello all,

Can anyone tell me how to ‘move’ an existing TckOval in Ruby Tk?


Thanks!
Bryan

Posted via http://www.ruby-forum.com/.

I believe you can tag objects on the canvas, and designate x & y
positions for the tags. If you tag your ‘oval’ you should be able to
move it wherever you want. There are also two very good resources for
Tk development in Ruby. They are both groups on Google. The first
group is called “Ruby and the Tk Toolkit”. The other is called “Tk
Documentation and Resources”. You should be able to get considerable
help between these two groups.

–Jay

From: Bryan R. [email protected]
Subject: Ruby Tk Canvas Drawing
Date: Fri, 4 Apr 2008 00:09:18 +0900
Message-ID: [email protected]

Can anyone tell me how to ‘move’ an existing TckOval in Ruby Tk?

For example,

canvas = TkCanvas.new.pack

tag = TkcTag.new(canvas)

oval = TkcOval.new(canvas, [100,100], [150,150], :tags=>tag)
line = TkcLine.new(canvas, [100,100], [150,150], :tags=>tag)

oval.move(20, -15) # move
line.move(20, -15) # move

tag.move(30, 20) # move items with tag

oval.coords([50,100], [200,140]) # transform
line.coords([50,50], [100,50], [50,100], [200,140]) # transform

Thanks for the help guys. Is it possible to only move one end of a line
without moving the other end?


Bryan

Hidetoshi NAGAI wrote:

From: Bryan R. [email protected]
Subject: Ruby Tk Canvas Drawing
Date: Fri, 4 Apr 2008 00:09:18 +0900
Message-ID: [email protected]

Can anyone tell me how to ‘move’ an existing TckOval in Ruby Tk?

For example,

canvas = TkCanvas.new.pack

tag = TkcTag.new(canvas)

oval = TkcOval.new(canvas, [100,100], [150,150], :tags=>tag)
line = TkcLine.new(canvas, [100,100], [150,150], :tags=>tag)

oval.move(20, -15) # move
line.move(20, -15) # move

tag.move(30, 20) # move items with tag

oval.coords([50,100], [200,140]) # transform
line.coords([50,50], [100,50], [50,100], [200,140]) # transform

From: Bryan R. [email protected]
Subject: Re: Ruby Tk Canvas Drawing
Date: Fri, 4 Apr 2008 06:35:55 +0900
Message-ID: [email protected]

Thanks for the help guys. Is it possible to only move one end of a line
without moving the other end?

Of course. :slight_smile:

def move_tail(line, dx, dy)
coords = line.coords
coords[-2] += dx
coords[-1] += dy
line.coords = coords
end

def set_tail(line, x, y)
coords = line.coords
coords[-2] = x
coords[-1] = y
line.coords = coords
end

def add_tail(line, x, y)
line.coords <<= [x, y]
end

def remove_tail(line)
coords = line.coords
coords.pop; coords.pop
line.coords = coords
end

Ah yes, I see. I guess I should have asked if there was a ‘built-in’
method for moving just one end of a line. Thanks for the code though!
:slight_smile: