Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drawing a line over the image i called

Status
Not open for further replies.

Silentkiller

Technical User
Feb 13, 2003
14
0
0
GB
Hi,
Can anyone enlighten me on how can i draw a straight line across the image i have called. Acutally i have tried to combine two code that is
#Calling out the Image
{
set w .image1
catch {destroy $w}
toplevel $w
wm title $w "Image Display"
wm iconname $w "Image1"

catch {image delete image1}
image create photo image1 -file [file join demos images y:/Filename/PGM/eye.pgm]

button .image1.edit -text Line -command {draw_line} -underline 0
pack .image1.edit -side bottom

label $w.l1 -image image1 -bd 1 -relief sunken
pack $w.l1 -side top -padx 10m -pady 10m
}

Drawing the line :
bind .image1 <ButtonPress-1> {line_start %W %x %y}
bind .image1 <B1-Motion> {line_drag %W %x %y}
bind .image1 <ButtonRelease-1> {line_end %W %x %y}

# The size of the grid divisions

set GRIDSIZE 20


bind .image1 <Control-ButtonPress-1> { }
bind .image1 <Control-B1-Motion> { }
bind .image1 <Control-ButtonRelease-1> { }

proc line_start {canv x y} {
global GRIDSIZE
global line

set line(x0) [expr {round($x/$GRIDSIZE)*$GRIDSIZE}]
set line(y0) [expr {round($y/$GRIDSIZE)*$GRIDSIZE}]

set line(id) [$canv cget line $line(x0) $line(y0) $line(x0) $line(y0)]

$canv bind $line(id) <Control-ButtonPress-1> [list line_move_start $canv $line(id) %x %y]

$canv bind $line(id) <Control-B1-Motion> [list line_move_drag $canv $line(id) %x %y]

$canv bind $line(id) <Control-ButtonRelease-1> [list line_move_end $canv $line(id) %x %y]
}

proc line_drag {canv x y} {
global line
$canv coords $line(id) $line(x0) $line(y0) $x $y
}

proc line_end {canv x y} {
global GRIDSIZE
global line

# Make final vertex on the nearest grid point

set x [expr {round($x/$GRIDSIZE)*$GRIDSIZE}]
set y [expr {round($y/$GRIDSIZE)*$GRIDSIZE}]
line_drag $canv $x $y
$canv itemconfigure $line(id) -fill blue
}

proc line_move_start {canv id x y} {
global line
set line(xs) $x
set line(ys) $y
$canv raise $id
$canv itemconfigure $id -stipple gray50
}

proc line_move_drag {canv id x y} {
global line
$canv move $id [expr {$x-$rect(xs)}] [expr {$y-$line(ys)}]
set line(xs) $x
set line(ys) $y
}

proc line_move_end {canv id x y} {
global GRIDSIZE
line_move_drag $canv $id $x $y


foreach {x0 y0 x1 y1} [$canv coords $id] {break}

$canv move $id [expr {(round($x0/$GRIDSIZE)*$GRIDSIZE)-$x0}] [expr {(round($y0/$GRIDSIZE)*$GRIDSIZE)-$y0}]

$canv itemconfigure $id -stipple &quot;&quot;
}

But i always encounter this error:
bad option &quot;create&quot;: must be cget or configure
what is wrong with my program
 
Here is a beginning:
I modified your code in some ways.
1- I moved the main part after the procs definition.
2- I replaced the label by a canvas.
3- In line_start I replaced [$canv cget line ...] by [$canv create line ...]

And I saw your code beginning to work (I don't tested all).

The modified code:

proc line_start {canv x y} {
global GRIDSIZE
global line

set line(x0) [expr {round($x/$GRIDSIZE)*$GRIDSIZE}]
set line(y0) [expr {round($y/$GRIDSIZE)*$GRIDSIZE}]

set line(id) [$canv create line $line(x0) $line(y0) $line(x0) $line(y0)]

$canv bind $line(id) <Control-ButtonPress-1> [list line_move_start $canv $line(id) %x %y]

$canv bind $line(id) <Control-B1-Motion> [list line_move_drag $canv $line(id) %x %y]

$canv bind $line(id) <Control-ButtonRelease-1> [list line_move_end $canv $line(id) %x %y]
}

proc line_drag {canv x y} {
global line
$canv coords $line(id) $line(x0) $line(y0) $x $y
}

proc line_end {canv x y} {
global GRIDSIZE
global line

# Make final vertex on the nearest grid point

set x [expr {round($x/$GRIDSIZE)*$GRIDSIZE}]
set y [expr {round($y/$GRIDSIZE)*$GRIDSIZE}]
line_drag $canv $x $y
$canv itemconfigure $line(id) -fill blue
}

proc line_move_start {canv id x y} {
global line
set line(xs) $x
set line(ys) $y
$canv raise $id
$canv itemconfigure $id -stipple gray50
}

proc line_move_drag {canv id x y} {
global line
$canv move $id [expr {$x-$rect(xs)}] [expr {$y-$line(ys)}]
set line(xs) $x
set line(ys) $y
}

proc line_move_end {canv id x y} {
global GRIDSIZE
line_move_drag $canv $id $x $y


foreach {x0 y0 x1 y1} [$canv coords $id] {break}

$canv move $id [expr {(round($x0/$GRIDSIZE)*$GRIDSIZE)-$x0}] [expr {(round($y0/$GRIDSIZE)*$GRIDSIZE)-$y0}]

$canv itemconfigure $id -stipple &quot;&quot;
}

#Calling out the Image
set w .image1
catch {destroy $w}
toplevel $w
wm title $w &quot;Image Display&quot;
wm iconname $w &quot;Image1&quot;

catch {image delete image1}
image create photo image1 -file hugelist.gif
# [file join demos images y:/Filename/PGM/eye.pgm]

button .image1.edit -text Line -command {draw_line} -underline 0
pack .image1.edit -side bottom

canvas $w.l1 -bd 1 -relief sunken -width [image width image1] -height [image height image1]
pack $w.l1 -side top -padx 10m -pady 10m
$w.l1 create image 0 0 -anchor nw -image image1

# Drawing the line :
bind $w.l1 <ButtonPress-1> {line_start %W %x %y}
bind $w.l1 <B1-Motion> {line_drag %W %x %y}
bind $w.l1 <ButtonRelease-1> {line_end %W %x %y}

# The size of the grid divisions

set GRIDSIZE 20


bind .image1 <Control-ButtonPress-1> { }
bind .image1 <Control-B1-Motion> { }
bind .image1 <Control-ButtonRelease-1> { }

HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top