Controlling canvas size
Posted: Sat Feb 23, 2008 4:33 pm
Is there a way to specify a canvas size, other than by making the entire window un-resizable?
Friends and Fans of newLISP
http://www.newlispfanclub.alh.net/forum/
http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=10&t=2160
I tried it in my script :gs:set-size
syntax: (gs:set-size sym-id int-width int-height)
parameter: sym-id - The name of the component of which a preferred size is set.
parameter: int-width - The preferred width of the component.
parameter: int-height - The preferred height of the component.
Note that not all layouts allow setting the size of a component. The grid and border layouts will size the component to its maximum possible in the layout.
Code: Select all
#!/usr/bin/newlisp
;=======================================
; Turtle graphics (NewLISP + GUI-server)
; mandala.lsp
; NewLISP v9.2.5 - Bertrand Carette (nov. 2007)
;=======================================
;; initialization GUI-server
(load (append (env "NEWLISPDIR") "/guiserver.lsp"))
(gs:init)
;; constants & variables
(constant 'PI2 (acos 0) 'DP2 (div PI2 90)) ; degre -> radian
(set 'WIDTH 400 'HEIGHT 400) ; size of the window
(set 'coorX 0 'coorY 0) ; coordinates of the "turtle"
(set 'dir 0) ; orientation (heading to the north)
(set 'back-color '(1 1 1)) ; backcolor = white
(set 'pen-color '(0 0 0)) ; pen = black
(set 'pen true) ; pen is down
;; building the GUI
(gs:frame 'WIN 100 100 WIDTH (add HEIGHT 32) "NewLISP Turtle")
(gs:canvas 'Canvas)
(gs:set-size 'Canvas WIDTH HEIGHT)
(gs:set-background 'Canvas back-color)
(gs:window-resized 'WIN 'resize-action)
(gs:add-to 'WIN 'Canvas)
(gs:set-visible 'WIN true)
;; graphic procedures (commands for the turtle)
(define (home)
; center of the screen, heading to north
(pen-up)
(set 'coorX 0 'coorY 0)
(set 'dir PI2)
(pen-down))
(define (clear-screen)
(gs:delete-tag 'L)
(home))
(define (pen-down)
(set 'pen true))
(define (pen-up)
(set 'pen nil))
(define (pendown?)
pen)
(define (set-pos x y)
; set the new position of the turtle
(set 'coorX (add (div WIDTH 2) coorX) 'coorY (sub (div HEIGHT 2) coorY))
(set 'newX (add (div WIDTH 2) x) 'newY (sub (div HEIGHT 2) y))
(if (pendown?)
; draw if pen is down
(gs:draw-line 'L (int coorX) (int coorY) (int newX) (int newY) pen-color ))
(set 'coorX x 'coorY y))
(define (pos)
; return current position
(cons coorX coorY))
(define (forward dist)
(set-pos (add coorX (mul dist (cos dir)))
(add coorY (mul dist (sin dir)))))
(define (backward dist)
(forward (- dist)))
(define (set-heading angle)
; set the orientation
(set 'dir (mod angle 360)))
(define (orientation)
; return current heading
dir)
(define (right degre)
; set orientation to the right (in degre)
(set-heading (sub dir (mul degre DP2))))
(define (left degre)
; set orientation to the left (in degre)
(set-heading (add dir (mul degre DP2))))
(define (resize-action id w h , x y)
(set 'x (int (sub (div w 2) (div WIDTH 2))) 'y (int (sub (div h 2) (div HEIGHT 2))))
(gs:move-tag 'L x y)
(set 'WIDTH (int w) 'HEIGHT (int h)))
;; main program (a Turtle Graphics program : mandala.lsp)
(define (get-vectors)
; create an array of vectors
(home)
(pen-up)
(set 'nsom 20) ; amount of vertices
(set 'sommets (array (+ nsom 1)))
(set 'rayon 280) ; radius
(set 'angle (/ 360 nsom))
(for (i 0 nsom)
(home)
(right (* i angle))
(forward rayon)
(nth-set (sommets i) (pos)))
) ; end define
(define (draw-vectors , a b)
; pen down and draw the mandala
(for (i 0 nsom)
(set 'a i)
(for (j 0 a)
(set 'b j)
(pen-up)
(set-pos (first (sommets a)) (last (sommets a)))
(pen-down)
(set-pos (first (sommets b)) (last (sommets b)))))
) ; end define
(get-vectors)
(draw-vectors)
(gs:update)
;; event loop
(gs:listen)
P.S.: but if you find a solution for your problem, it interests me :)newBert wrote:Sorry, I am still not quite at ease with 'grid-layout' ;)