Canvas for Drawing (Java AWT)

A Canvas in Java (specifically AWT class) is a black rectangular / rectangle area where the application itself can draw or trap an input events from the user. The canvas inherits Component class.

AWT Canvas Syntax

public class Canvas extends Component implements Accessible  

No more chit-chatting, here’s an example of what you can do in AWT Canvas :

//1st import
  import java.awt.*;

 //2nd creates the method

 import javax.swing.JFrame;
public class CanvasExmpl extends JFrame
{
public CanvasExmpl()
{
JFrame f= new JFrame(“Canvas Example”);
f.add(new MyCanvas());
f.setLayout(null);
f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}



//3rd call and run it!     

public static void main(String args[]) {
new CanvasExmpl();
}
}
class MyCanvases extends Canvas
{
public MyCanvases() {
setBackground (Color.BLACK);
setSize(300, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.WHITE);
g.fillOval(75, 75, 150, 75);
}
}

 

Watch my Youtube video on the same topic : https://www.youtube.com/watch?v=risnqJoa8uI

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

One thought on “Canvas for Drawing (Java AWT)”

  1. After study some of the blog articles with your website now, and i also truly appreciate your technique of blogging. I bookmarked it to my bookmark internet site list and you will be checking back soon. Pls look into my web site as well and told me what you consider.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.