AWT Canvas

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java AWT Canvas

The Canvas control represents a blank rectangular area where the application can draw or trap input events from the user. It inherits the Component class.

AWT Canvas class Declaration

  1. public class Canvas extends Component implements Accessible  

Java AWT Canvas Example

  1. import java.awt.*;  
  2. public class CanvasExample  
  3. {  
  4.   public CanvasExample()  
  5.   {  
  6.     Frame f= new Frame("Canvas Example");  
  7.     f.add(new MyCanvas());  
  8.     f.setLayout(null);  
  9.     f.setSize(400400);  
  10.     f.setVisible(true);  
  11.   }  
  12.   public static void main(String args[])  
  13.   {  
  14.     new CanvasExample();  
  15.   }  
  16. }  
  17. class MyCanvas extends Canvas  
  18. {  
  19.         public MyCanvas() {  
  20.         setBackground (Color.GRAY);  
  21.         setSize(300200);  
  22.      }  
  23.   public void paint(Graphics g)  
  24.   {  
  25.     g.setColor(Color.red);  
  26.     g.fillOval(757515075);  
  27.   }  
  28. }