The Canvas class is a member of the Java AWT. A Canvas is essentially what its name implies: a blank surgace which can be used for drawing. The advantages of using a Canvas over just drawing directly onto the background of an applet are twofold. Frist, a Canvas is a GUI component and can be positioned within an applet by using a layout manager. Therefore, two or more Canvases can be used within a single applet. Secondly all components drawn onto a Canbas are automatically clipped at the boundaries of the Canvas. It is therefore easy to guarantee that drawings will not overlap other GUI components within an applet. Each Canvas has its own graphics context, which is independent of the graphics context for the applet as a whole. It is therefore still possible to draw onto the applet's background even if the applet contains a Canvas. A Canvas has its own paint() method which, just as with the paint() method for an applet, does nothing in its default form. In order for a Canvas to actually show anything, it must be inherited by a user-defined class. The following applet illustrates using a Canvas.

The graphics contexts of MyCanvas and its parent applet are independent. The line drawn on the canvas is clipped at the edges of the canvas so it does not extend onto the background applet. The line drawn on the applet passes 'underneath' the canvas. Although a Canvas can be used to display non-interactive graphics within an applet, it is probably more useful as a mini-window in which graphics can be displayed in response to input from the user. The possiblities are, of course, limited only by the programer's imagination. Animation