ruby tk drawing a line

E

Ed Redman

I have a question concerning ruby/tk and drawing lines on a canvas.
I tried to convert from a tcl program.
Everything works fine except that the line when its thickness is large
appears to be a series of short line segments rather than a solid line.

I have binded the "1" mouse button to start the line with the following

canvas.bind( '1', proc { |e|

$startx = e.x
$starty = e.y

})

and the motion of the mouse

canvas.bind('B1-Motion', proc { |e|
$endx = e.x
$endy = e.y

$a = TkcLine.new(canvas, $startx, $starty, $endx, $endy)
$a.width option.value
$a.fill "#{color}"
})

Can anyone see why this does not give me a solid line
 
J

Joel VanderWerf

Ed said:
I have a question concerning ruby/tk and drawing lines on a canvas.
I tried to convert from a tcl program.
Everything works fine except that the line when its thickness is large
appears to be a series of short line segments rather than a solid line.

I have binded the "1" mouse button to start the line with the following

canvas.bind( '1', proc { |e|

$startx = e.x
$starty = e.y

})

and the motion of the mouse

canvas.bind('B1-Motion', proc { |e|
$endx = e.x
$endy = e.y

$a = TkcLine.new(canvas, $startx, $starty, $endx, $endy)
$a.width option.value
$a.fill "#{color}"
})

Can anyone see why this does not give me a solid line

Does the same thing happen without the fill param?

A TkcLine has a "dash" parameter, and that might be causing this. But it
would be surprising if the default is a dashed line. Anyway, you could try

$a.dash ""

or

$a.dash nil

See

http://www.tcl.tk/man/tcl8.4/TkCmd/canvas.htm#M27

for details.
 
H

Hidetoshi NAGAI

From: Ed Redman <[email protected]>
Subject: ruby tk drawing a line
Date: Fri, 12 Oct 2007 12:50:10 +0900
Message-ID: said:
canvas.bind('B1-Motion', proc { |e|
$endx = e.x
$endy = e.y

$a = TkcLine.new(canvas, $startx, $starty, $endx, $endy)

You write so many new lines at here.
Each short line is made at each 'B1-Motion' callback.

The following may be an example which you want.
-----------------------------------------------------------------
require 'tk'

line = nil
first = nil
last = nil

canvas = TkCanvas.new:)relief=>:ridge, :borderwidth=>3).pack

f = TkFrame.new.pack:)fill=>:x)

color = 'black'
TkButton.new(f, :text=>'color', :command=>proc{
color = Tk.chooseColor:)initialcolor=>color)
}).pack:)side=>:right, :padx=>[10,3])

option = TkSpinbox.new(f, :from=>1, :to=>1000,
:width=>4).pack:)side=>:right, :padx=>0)
TkLabel.new(f, :text=>'width').pack:)side=>:right, :padx=>[10,0])

canvas.bind('1', '%x', '%y'){|*first|
line = TkcLine.new(canvas, first, first,
:width=>option.value, :fill=>color)
}

canvas.bind('B1-Motion', '%x', '%y'){|*last| line.coords(first, last)}

Tk.mainloop
-----------------------------------------------------------------

Ruby/Tk accepts all of the following coords pattern for a canvas item.

(1) x1, y1, x2, y2, x3, y3, ...

(2) [x1, y1], [x2, y2], [x3, y3], ...

(3) [ [x1, y1], [x2, y2], [x3, y3], ... ]

In my example, I use the pattern (2).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top