Canvas in Java AWT


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

Constructor Used for
Canvas() Creates a new blank canvas.
Canvas(GraphicsConfiguration c) Creates a new canvas with a specified graphics configuration.

Source Code

package awtDemo;
 
import java.awt.*;
import java.awt.event.*;
 
//Canvas in Java AWT
 
class MyCanvas extends Canvas    
{    
    public MyCanvas() {    
        setBackground (Color.GRAY);    
        setSize(300, 200);    
     }     
  public void paint(Graphics g)    
  {    
    g.setColor(Color.red);    
    g.fillOval(75, 75, 150, 75);    
  }    
}  
 
 
class MyApp extends Frame{
 
	public MyApp() {
		super("Tutor Joes");
		setSize(1000, 600);// w,h
		setLayout(null);
		setVisible(true);
 
		add(new MyCanvas());
 
 
 
		// Close Button Code
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
	}
 
}
 
public class app {
	public static void main(String[] args) {
		MyApp frm = new MyApp();
	}
 
}
 
To download raw file Click Here

Basic Programs



Learn All in Tamil © Designed & Developed By Tutor Joes