Java AWT | Canvas Class Last Updated : 02 Jul, 2021 Comments Improve Suggest changes Like Article Like Report Canvas class is a part of Java AWT. Canvas is a blank rectangular area where the user can draw or trap input from the user. Canvas class inherits the Component class.Constructor of the Canvas class are : Canvas(): Creates a new blank canvas.Canvas(GraphicsConfiguration c): Creates a new canvas with a specified graphics configuration. Commonly used Methods in Canvas Class MethodExplanationaddNotify()Creates the peer of the canvas.createBufferStrategy(int n)Creates a new strategy for multi-buffering on this component.createBufferStrategy(int n, BufferCapabilities c)Creates a new strategy for multi-buffering on this component with the required buffer capabilitiesgetBufferStrategy()Returns the BufferStrategy used by this component.paint(Graphics g)paints this component.update(Graphics g)updates this canvas. Below programs illustrate the use of Canvas Class : Program 1: To create a canvas and paint the canvas. Java // Java Program to create a to create // a canvas and paint the canvas import java.awt.*; import javax.swing.*; class canvas extends JFrame { // constructor canvas() { super("canvas"); // create a empty canvas Canvas c = new Canvas() { // paint the canvas public void paint(Graphics g) { // set color to red g.setColor(Color.red); // set Font g.setFont(new Font("Bold", 1, 20)); // draw a string g.drawString("This is a canvas", 100, 100); } }; // set background c.setBackground(Color.black); add(c); setSize(400, 300); show(); } // Main Method public static void main(String args[]) { canvas c = new canvas(); } } Output: Program 2: To create a canvas and add mouse listener to the canvas(a circle of radius 5 will appear at the points where mouse are clicked or dragged on the canvas). Java // Java Program to create a // canvas and mouse listener to the // canvas ( a circle of radius 5 will appear // at the points where mouse are clicked or // dragged on the canvas) import java.awt.*; import javax.swing.*; import java.awt.event.*; class canvas extends JFrame implements MouseListener, MouseMotionListener { // create a canvas Canvas c; // constructor canvas() { super("canvas"); // create a empty canvas c = new Canvas() { public void paint(Graphics g) { } }; // set background c.setBackground(Color.black); // add mouse listener c.addMouseListener(this); c.addMouseMotionListener(this); add(c); setSize(400, 300); show(); } // mouse listener and mouse motion listener methods public void mouseClicked(MouseEvent e) { Graphics g = c.getGraphics(); g.setColor(Color.red); // get X and y position int x, y; x = e.getX(); y = e.getY(); // draw a Oval at the point // where mouse is moved g.fillOval(x, y, 5, 5); } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { Graphics g = c.getGraphics(); g.setColor(Color.red); // get X and y position int x, y; x = e.getX(); y = e.getY(); // draw a Oval at the point where mouse is moved g.fillOval(x, y, 5, 5); } public void mouseExited(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mousePressed(MouseEvent e) { } // main class public static void main(String args[]) { canvas c = new canvas(); } } Output: Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/Canvas.html Comment More infoAdvertise with us Next Article Java AWT | Canvas Class andrew1234 Follow Improve Article Tags : Java Java-AWT Practice Tags : Java Similar Reads JavaFX | Canvas Class Canvas class is a part of JavaFX. Canvas class basically creates an image that can be drawn on using a set of graphics commands provided by a GraphicsContext. Canvas has a specified height and width and all the drawing operations are clipped to the bounds of the canvas. Constructors of the class: Ca 4 min read Java AWT | Color Class The Color class is a part of Java Abstract Window Toolkit(AWT) package. The Color class creates color by using the given RGBA values where RGBA stands for RED, GREEN, BLUE, ALPHA or using HSB value where HSB stands for HUE, SATURATION, BRIcomponents. The value for individual components RGBA ranges f 9 min read Java Applet Class Java Applet is a special type of small Java program embedded in the webpage to generate dynamic content. The specialty of the Java applet is it runs inside the browser and works on the Client side (User interface side). Note:Applets, which are small programs that can be run inside a web page, are no 4 min read Java AWT | GridLayout Class GridLayout class represents a layout manager with a specified number of rows and columns in a rectangular grid. The GridLayout container is divided into an equal-sized of rectangles, and one of the components is placed in each rectangle. Every rectangle cell has the same size therefore, they contain 5 min read Java Applet Basics Java Applets was once a very popular feature of web applications. Java Applets were small programs written in Java that ran inside a web browser. Learning about Applet helps us understand how Java has evolved and how it handles graphics.Note: java.applet package has been deprecated in Java 9 and lat 9 min read Java AWT | Dimension Class Dimension class is a part of Java AWT. It contains the height and width of a component in an integer as well as double precision. The use of Dimension class is that many functions of Java AWT and Swing return dimension object. Constructors of the Dimension class Dimension() : It will create a new Ob 4 min read Java AWT | SpringLayout Class A SpringLayout class in AWT(Abstract Window Toolkit) laid out of the children to its associated container, according to a set of Layout constraints. Each constraint is represented by a Spring object which controls the vertical or horizontal distance between two component edges. The edges can belong 5 min read What is Java AWT Graphics? Graphics is an abstract class provided by Java AWT which is used to draw or paint on the components. It consists of various fields which hold information like components to be painted, font, color, XOR mode, etc., and methods that allow drawing various shapes on the GUI components. Graphics is an ab 6 min read Java AWT Label Abstract Window Toolkit (AWT) was Java's first GUI framework, and it has been part of Java since version 1.0. It contains numerous classes and methods that allow you to create windows and simple controls. Controls are components that allow a user to interact with your application in various ways. Th 7 min read Java AWT | Ellipse2D Ellipse2D class is present in java.awt.geom package and it is used to define an ellipse by stating its framing rectangle. This class is only the abstract superclass for all objects which store a 2D ellipse. Ellipse2D.Double defines an ellipse with double precision. Ellipse2D.Float defines an ellipse 5 min read Like