Graphics and Animation

Graphics and Animation

The simplest way to create graphics using JLIB is to use the canvas procedure to create a panel for drawing on.
   (define c (canvas 400 500))
The arguments to the canvas method specify the width and height in pixels.

There are several methods available for drawing on a canvas. They all require that you use the "graphics context" of the canvas which you get using (.bufferg$ c)

The graphical operations you can use are:
(define g (.bufferg$ c))
(.setColor g (color R G B))
(.drawLine g x1 y1 x2 y2)
(.drawRect g x1 y1 w h)
(.drawOval g x1 y1 w h)
(.drawArc g x1 y1 w h a d)
(.fillRect g x1 y1 w h)
(.fillOval g x1 y1 w h)
(.fillArc g x1 y1 w h a d)
You can also draw a string S starting at location x,y using:
(.setFont g (Helvetica 12))
(.drawString g x y str)
General Polygons can be drawn by first creating a polygon from points, and then drawing or filling the polygon:
(define p (java.awt.Polygon.))
(.addPoint p x1 y1)
(.addPoint p x2 y2)
      ...
(.drawPolygon g p)
(.fillPolygon g p)


The following program uses many of the methods shown above to draw a face when the user presses the "draw face" button.