(* A demo of how to use the simple widgets *) open SimpleWidget (* Create some simple label widgets *) let l1 = label "Hello" let l2 = label "World" (* Compose them horizontally, adding some borders *) let h = border (hpair (border l1) (hpair (space (10,10)) (border l2))) (* This paint function draws a fractal tree -- Google "L-systems" for *) (* more explanation / inspiration *) let paint (g:Gctx.t) : unit = let gnew = Gctx.set_color g Gctx.green in let rec fractal ((x,y):int*int) (angle:int) (len:int) = if len <= 1 then () else let lf = float_of_int len in let af = ((float_of_int angle) *. 3.14159) /. 180.0 in let nx = x + (int_of_float (lf *. (cos af))) in let ny = y + (int_of_float (lf *. (sin af))) in begin Gctx.draw_line gnew (x,y) (nx, ny); fractal (nx, ny) (angle + 20) (len - 2); fractal (nx, ny) (angle - 10) (len - 1) end in fractal (75,100) 270 15 (* Create a canvas widget that draws a fractal tree *) let c = border (canvas (150, 120) paint) (* Put h next to c, with some space and more borders, just for fun.*) let w = border (hpair h (hpair (space (10, 10)) c)) let run () :unit = (* open the window *) Graphics.open_graph ""; let g = Gctx.create () in (* draw the widget *) w.repaint g; (* infinite loop so we can see the window. *) let rec loop () : unit = loop () in loop () ;; run ()