- AWT - Home
- AWT - Overview
- AWT - Environment
- AWT - Controls
- AWT - Event Handling
- AWT - Event Classes
- AWT - Event Listeners
- AWT - Event Adapters
- AWT - Layouts
- AWT - Containers
- AWT - Menu
- AWT - Graphics
AWT - Useful Resources
AWT Canvas Class
Introduction
Canvas control represents a rectangular area where application can draw something or can receive inputs created by user.
Class declaration
Following is the declaration for java.awt.Canvas class:
public class Canvas
extends Component
implements Accessible
Class constructors
| S.N. | Constructor & Description |
|---|---|
| 1 |
Canvas() Constructs a new Canvas. |
| 2 |
Canvas(GraphicsConfiguration config) Constructs a new Canvas given a GraphicsConfiguration object. |
Class methods
| S.N. | Method & Description |
|---|---|
| 1 |
void addNotify() Creates the peer of the canvas. |
| 2 |
void createBufferStrategy(int numBuffers) Creates a new strategy for multi-buffering on this component. |
| 3 |
void createBufferStrategy(int numBuffers, BufferCapabilities caps) Creates a new strategy for multi-buffering on this component with the required buffer capabilities. |
| 4 |
AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this Canvas. |
| 5 |
BufferStrategy getBufferStrategy() Returns the BufferStrategy used by this component. |
| 6 |
void paint(Graphics g) Paints this canvas. |
| 7 |
void pdate(Graphics g) Updates this canvas. |
Methods inherited
This class inherits methods from the following classes:
java.awt.Component
java.lang.Object
Canvas Example
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AwtControlDemo.java
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
public class AwtControlDemo {
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
public AwtControlDemo(){
prepareGUI();
}
public static void main(String[] args){
AwtControlDemo awtControlDemo = new AwtControlDemo();
awtControlDemo.showCanvasDemo();
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showCanvasDemo(){
headerLabel.setText("Control in action: Canvas");
controlPanel.add(new MyCanvas());
mainFrame.setVisible(true);
}
class MyCanvas extends Canvas {
public MyCanvas () {
setBackground (Color.GRAY);
setSize(300, 300);
}
public void paint (Graphics g) {
Graphics2D g2;
g2 = (Graphics2D) g;
g2.drawString ("It is a custom canvas area", 70, 70);
}
}
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output