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.fillOval(75, 75, 150, 75);    
class MyApp extends Frame{
	public MyApp() {
		super("Tutor Joes");
		setSize(1000, 600);// w,h
		add(new MyCanvas());
		// Close Button Code
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
public class app {
	public static void main(String[] args) {
		MyApp frm = new MyApp();
To download raw file Click Here


Java AWT

Basic Programs