Version 2 of Canvas pixel painting

Updated 2005-12-13 03:08:07

"Elfo" <[email protected]> wrote in the comp.lang.tcl newsgroup:

Here you have a sample... it's a very simple application for paint. I used an image in a canvas widget... I'm not sure it's the best way to do it... but it works (I use 8.4a2 version of Tcl/Tk and Win2000).


 global _paint

 set _paint(alto) 600
 set _paint(ancho) 800
 set _paint(color) black

 ### Buttons
 frame .barra

 pack .lienzo.c -fill both
 pack .lienzo -fill both -side right

create photo _paint(capa1) -width $_paint(ancho) -height $_paint(alto) -palette 256/256/256] -anchor nw

 _paint(fondo) put {white} -to 0 0 $_paint(ancho) $_paint(alto)

 bind .lienzo.c <1> "Paint %x %y"
 bind .lienzo.c <B1-Motion> "Paint %x %y"

 proc Paint {x y} {
    global _paint

    if {$x >= 0 && $y >= 0} {
        _paint(fondo) put $_paint(color) -to $x $y [expr $x + $_paint(grosorx)] [expr $y + $_paint(grosory)]
    }
 }

 proc ChangeColor {} {
    global _paint

    set aux $_paint(color)

    set _paint(color) [tk_chooseColor]
    if {$_paint(color) != ""} {
        .barra.bcolor configure -bg $_paint(color)
    } else  {
        set _paint(color) $aux
    }
 }

Sorry about the spanish words in my code... Good luck!!

 Asier Burgoa González
 [email protected]

 Éniac, Sistemas Informáticos S.A.
 LOGROÑO (La Rioja) SPAIN
 +34 941 28 28 28

Arts and crafts of Tcl-Tk programming