Canvas:
A Canvas class gives the ability to construct generic GUI components. A class does not have any default graphical representation, or any event handlers of its own. It inherits these capabilities from its super class Component. A Canvas class is commonly subclassed to construct customized GUI components consisting of drawing or images, and could handle user input events relating to mouse and keyboard actions. Its paint ( ) method is commonly overridden to render graphics in the component
// Illustrating Canvas
import java.awt.*;
import java.applet.*;
public class CanvasApplet extends Applet
{
public void init( )
{
DrawingRegion region = new DrawingRegion ( );
add(region);
}
}
class DrawingRegion extends Canvas
{
public DrawingRegion
{
setSize(150,150);
}
public void paint (Graphics g)
{
g.drawRect(0,0,149,149); //draw border around region
g.drawstring("A 150 x 150 Canvas", 20,20); //draw string
}
}
The given code draws a rectangular region on a canvas.