Java AWT | Canvas Class

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 :

  1. Canvas(): Creates a new blank canvas.
  2. Canvas(GraphicsConfiguration c): Creates a new canvas with a specified graphics configuration.

Commonly used Methods in Canvas Class

Method Explanation
addNotify() 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 capabilities
getBufferStrategy() 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 :

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 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); g.setFont( new Font( "Bold" , 1 , 20 )); // draw a string g.drawString( "This is a canvas" , 100 , 100 ); // set background c.setBackground(Color.black); setSize( 400 , 300 ); // Main Method public static void main(String args[]) canvas c = new 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 // constructor 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 ); setSize( 400 , 300 ); // 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 // 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 // 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(); Like Article -->

Please Login to comment.

Similar Reads

Java.awt.image.RescaleOp Class in Java with Examples

RescaleOp is a class in java.awt.image package which implements BufferedImageOp and RasterOp interface. This class performs a pixel-by-pixel rescaling of the data in the source image by multiplying the sample values for each pixel by a scale factor and then adding an offset. The scaled sample values are clipped to the minimum/maximum representation

5 min read Java AWT vs Java Swing vs Java FX

Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ

11 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 a component, which fills the entire cell. When th

5 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 from 0 to 255 or 0.0 to 0.1. The value of alpha det

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 Object with height and width set to zero.Dimension(D

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 to any child of the container, or to the container

5 min read Java AWT | GridBagLayout Class

GridBagLayout class is a flexible layout manager. It is used to aligns the components horizontally, vertically, or along their baseline. It doesn't require the components of the same size. Each GridBagLayout object manages a rectangular grid of cells, dynamic with each component occupying one or more cells, called its display area. GridBagLayout co

6 min read Java AWT | BoxLayout Class

The BoxLayout class is used to arrange the components either vertically (along Y-axis) or horizontally (along X-axis). In BoxLayout class, the components are put either in a single row or a single column. The components will not wrap so, for example, a horizontal arrangement of components will stay horizontally arranged when the frame is resized. C

5 min read Java AWT | MenuShortcut Class

MenuShortcut class is a part of JavaAWT. MenuShortcut class implements menu shortcuts which are implemented using virtual keycodes. The MenuShortcut class represents a keyboard accelerator for a MenuItem. Constructors of the class: MenuShortcut(int k): Creates a MenuShortcut object with specified key. MenuShortcut(int k, boolean b): Constructs a ne

5 min read Robot Class in Java AWT

The Robot class in the Java AWT package is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations. In simple terms, the class provide

4 min read Java AWT | MouseInfo and PointerInfo

MouseInfo and PointerInfo is a part of Java AWT. MouseInfo provides information about the location of the pointer and the number of buttons on the mouse. PointerInfo provides information returns the information about the location of the pointer and the graphics device. Methods of MouseInfo Method Explanation getNumberOfButtons() Returns the number

2 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 with float precision. Constructor of the class ar

5 min read Java AWT | WindowStateListener

WindowStateListener is a part of java.awt.event package. It is an interface to handle window state events. This interface returns an event when the frame or window associated with it is iconified or maximized.Abstract function of the class: windowStateChanged(WindowEvent e) : invoked when a state of the window is changed. Below programs illustrate

2 min read Java AWT | FlowLayout

FlowLayout is used to arrange components in a sequence one after the other. The default layout of applet and panel is FlowLayout. Constructors : FlowLayout(): It will Construct a new FlowLayout with centered alignment.The horizontal and vertical gap will be 5 pixels. FlowLayout(int align) : It will Construct a new FlowLayout with given alignment.Th

5 min read Java program to convert Currency using AWT

Swing is a part of the JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components which allow a high level of customization and provide rich functionalities, and is used to create window-based applications. Java swing components are lightweight, platform-in

4 min read Java program to store a Student Information in a File using AWT

Swing is a part of the JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components which allow a high level of customization and provide rich functionalities, and is used to create window-based applications. Java swing components are lightweight, platform-in

4 min read Difference between AWT and Swing in Java

Java is one of the most in-demand programming languages for developing a variety of applications. The popularity of Java can be attributed to its versatility as it can be used to design customized applications that are light and fast and serve a variety of purposes ranging from web services to android applications. Java is fast, reliable, and secur

2 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 abstract class and thus cannot be initialized direct

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. The AWT supports the following types of controls: La

7 min read Java AWT Toolkit

The Abstract Window Toolkit (AWT) is a Java package that provides a platform-indepеndеnt sеt of tools for creating graphical usеr intеrfacеs (GUIs). AWT is part of thе Java Foundation Classеs (JFC), which also includes Swing for morе advancеd GUI dеvеlopmеnt. In this rеsponsе, I'll providе an ovеrviеw of thе kеy componеnts and concеpts in AWT, alon

6 min read Java AWT CheckboxGroup

Java AWT CheckboxGroup is a class that is used to create checkboxes and group a set of checkbox buttons together. Many components in the CheckboxGroup separate, which serves as a means to group a set of checkboxes. In this clause, we will delve into the CheckboxGroup class, and its methods, and demo its usage through a very simple example. Class De

3 min read Java AWT Checkbox

Java AWT (Abstract Window Toolkit) provides a really various set of tools for edifice in writing user interfaces (GUIs), and among these tools is the Checkbox class. Checkboxes are necessary components for user interactions, allowing users to work binary choices easily. In this clause, we'll search the Checkbox class, its constructors, and methods,

4 min read Java ItemListener in AWT

The Java ItemListener user interface in Java's Abstract Window Toolkit (AWT) is a crucial component for managing user interactions with elements like checkboxes and option lists. In this article, we wish to search how the ItemListener interface is old in AWT to respond to exploiter input. The ItemListener interface is divided into the java.awt.even

3 min read Java MouseMotionListener in AWT

Java AWT (Abstract Window Toolkit) provides varied event listeners to wield exploiter interactions. One of these is the Java MouseMotionListener, which is secondhand to track sneak out motion events. This hearer allows you to respond to the mouse's front, such as sleuthing when the mouse is dragged or moved within a component. In this clause, we wi

3 min read Java KeyListener in AWT

The Java KeyListener in the Abstract Window Toolkit (AWT) is a fundamental tool for achieving this. The KeyListener Interface is found in "java.awt.event" package. In this article, we'll explore what the KeyListener is, and its declaration methods, and supply examples with explanatory comments. Java KeyListener in AWTThe KeyListener port in Java AW

3 min read Java AWT Panel

In Java's Abstract Window Toolkit (AWT), the Panel class is a fundamental component for creating graphical user interfaces. It offers a straightforward way to organize and group various GUI elements. This article explores the Panel class in Java AWT, covering its essential aspects, methods, and constructors, and demonstrating its practical use thro

2 min read Java AWT PopupMenu

In Java, AWT (Abstract Window Toolkit) provides a PopupMenu course that allows you to make pop-up menus in your graphical user port (GUI) applications. A PopupMenu is a setting menu that appears when you right-click on a component, such as a button or a venire. It provides a set of options or actions that the user can select from. In this clause, w

3 min read Java MouseListener in AWT

The Abstract Window Toolkit (AWT) in Java provides a collection of user interface components and event-handling features for creating graphical user interfaces (GUIs). User interaction is an important component of GUI programming, and the MouseListener interface in AWT is an important tool for managing mouse events in Java applications. In this art

3 min read Java AWT MenuItem & Menu

Java Abstract Window Toolkit (AWT) provides a comprehensive set of classes and methods to create graphical user interfaces. In this article, we will explore the MenuItem and Menu classes, which are essential for building menus in Java applications. Java AWT MenuItemMenuItem is a class that represents a simple labeled menu item within a menu. It can

3 min read Fonts Available in Java AWT

We know that AWT stands for ABSTRACT WINDOW TOOLKIT. For, Example when we want to draw a string in the AWT in Frame we use the method Graphics.drawString(String str, int x_coordinate, int y_coordinate). Now use the Font class for the string to display in the type of font you want. As it is difficult to remember all the font names we can get the fon