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:

  1. public class Canvas
  2. extends Component
  3. 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 AccessibleContextgetAccessibleContext() Gets the AccessibleContext associated with this Canvas.
5 BufferStrategygetBufferStrategy() 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

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtControlDemo {
  7.  
  8. private Frame mainFrame;
  9. private Label headerLabel;
  10. private Label statusLabel;
  11. private Panel controlPanel;
  12.  
  13. public AwtControlDemo(){
  14. prepareGUI();
  15. }
  16.  
  17. public static void main(String[] args){
  18. AwtControlDemo awtControlDemo = new AwtControlDemo();
  19. awtControlDemo.showCanvasDemo();
  20. }
  21.  
  22. private void prepareGUI(){
  23. mainFrame = new Frame("Java AWT Examples");
  24. mainFrame.setSize(400,400);
  25. mainFrame.setLayout(new GridLayout(3, 1));
  26. mainFrame.addWindowListener(new WindowAdapter() {
  27. public void windowClosing(WindowEvent windowEvent){
  28. System.exit(0);
  29. }
  30. });
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showCanvasDemo(){
  47. headerLabel.setText("Control in action: Canvas");
  48.  
  49. controlPanel.add(new MyCanvas());
  50. mainFrame.setVisible(true);
  51. }
  52.  
  53. class MyCanvas extends Canvas {
  54.  
  55. public MyCanvas () {
  56. setBackground (Color.GRAY);
  57. setSize(300, 300);
  58. }
  59.  
  60. public void paint (Graphics g) {
  61. Graphics2D g2;
  62. g2 = (Graphics2D) g;
  63. g2.drawString ("It is a custom canvas area", 70, 70);
  64. }
  65. }
  66. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtControlDemo

Verify the following output

awt_canvas.jpg