Version 2 of Click me

Updated 2003-02-18 20:39:01

Richard Suchenwirth 2003-02-18 - Reading a Java tutorial in a bored moment, I came upon the ClickMe example at http://java.sun.com/docs/books/tutorial/java/concepts/practical.html , where with only two Java class files, in less than 40 LOC, you can implement a rectangle where a red spot appears when you click on it. After compilation, you have 5 files (one HTML) that perform this feat.

Hm, thought I, how would that look in Tcl?


 package require Tk
 proc drawSpot {w x y r} {
    $w delete spot
    $w create oval [expr {$x-$r}] [expr {$y-$r}] [expr {$x+$r}] [expr {$y+$r}]\
        -fill red -outline red -tag spot
 }
 pack [canvas .c]
 .c create rect 5 5 205 205 -fill white -outline black -tag box
 .c bind box <1> {drawSpot %W %x %y 7}

In order to reduce the ugly heap of expr calls, setok proposed this variation, with "specialist" functions for addition and subtraction:

 package require Tk
 proc + {x y} {expr {$x + $y}}
 proc - {x y} {expr {$x - $y}}
 proc drawSpot {w x y r} {
    $w delete spot
    $w create oval [- $x $r] [- $y $r] [+ $x $r] [+ $y $r]\
        -fill red -outline red -tag spot
 }
 pack [canvas .c]
 .c create rect 5 5 305 155 -fill white -outline black -tag box
 .c bind box <1> {drawSpot %W %x %y 7}

Arts and crafts of Tcl-Tk programming