Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
AWT Canvas
3.
AWT Canvas Class Declaration
4.
AWT Canvas Class Constructors
5.
AWT Canvas Class Methods
6.
Java AWT Canvas Example
6.1.
Example 1
6.2.
Example 2
7.
Frequently Asked Questions
7.1.
What is AWT?
7.2.
Which library package needs to be imported to use awt canvas?
7.3.
If we want to display ellipses and rectangles on a screen, should we use a canvas or a JPanel?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

AWT Canvas

Author SHIVAM SINHA
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The Canvas class in AWT inherits the Component class. Canvas class is a part of Java AWT. A canvas is a blank rectangular area where the user can draw or trap input from the user. We will learn more about AWT Canvas in upcoming sections.

Read More About, Basics of Java and Hashcode Method in Java

AWT Canvas

A Canvas class represents a blank rectangular screen area where the application can draw or where the app can trap input events from the user.

The app must be under the Canvas class to get useful functionality like creating a custom component.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

AWT Canvas Class Declaration

Following is the declaration for java. awt.canvas class:

public class Canvas extends Component implements Accessible 

AWT Canvas Class Constructors

Now, let’s see AWT Canvas Class Constructors. We have the following constructor for the AWT Canvas Class:-

Canvas(): This constructor creates a new empty or blank canvas.

Canvas(GraphicsConfiguration c): This constructor creates a new canvas with certain graphics configuration

AWT Canvas Class Methods

To perform the operations on the canvas, we have so many methods. Some of the important and most used methods are:

You can also read about the Multiple Inheritance in Java.

Java AWT Canvas Example

Example 1

In the following example, we are creating a Canvas in the Frame and painting a red-colored oval inside it.

import java.awt.*;  

public class CanvasExample1   
{    
  // class constructor   
  public CanvasExample1()    
  {    
  
    // creating a frame  
    Frame f = new Frame("Canvas Example");   
    // adding canvas to frame   
    f.add(new MyCanvas());    
  
    // setting layout, size and visibility of frame  
    f.setLayout(null);    
    f.setSize(400, 400);    
    f.setVisible(true);    
  }    
  
  // main method  
  public static void main(String args[])    
  {    
    new CanvasExample1();    
  }    
}    
  
// class which inherits the Canvas class  
// to create Canvas  
class MyCanvas extends Canvas    
{    
        // class constructor  
        public MyCanvas() {    
        setBackground (Color.GRAY);    
        setSize(300, 200);    
     }    
  
     // paint() method to draw inside the canvas  
  public void paint(Graphics g)    
  {    
  
    // adding specifications  
    g.setColor(Color.red);    
    g.fillOval(75, 75, 150, 75);    
  }    
}       

 

OUTPUT :

            

Example 2

In this example, we are creating an empty canvas and writing some text, and setting some color to the text. We have also set the size and color of the canvas.

import java.awt.*;
import javax.swing.*;
class canvas extends JFrame {
 
   
    canvas()
    {
        super("canvas");
 
        //creating an empty canvas
        Canvas canvasobj = 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", 150, 150);
            }
        };


        canvasobj.setBackground(Color.black);
 
        add(canvasobj);
        setSize(400, 300);
        show();
    }
 
    // Main Method
    public static void main(String args[])
    {
        canvas canvasobj = new canvas();
    }
}

 

OUTPUT :
      

Try it by yourself on java online compiler.

Frequently Asked Questions

What is AWT?

It stands for Abstract Window Toolkit. It is an API that Java programmers use to create GUI(Graphical User Interface) objects such as scroll bars, windows, and buttons.

Which library package needs to be imported to use awt canvas?

We need to import class java.awt.Canvas

If we want to display ellipses and rectangles on a screen, should we use a canvas or a JPanel?

It depends on whether we are using Swing or AWT. If we are using swing, then it is recommended to use only swing components, so we should use Jpanel, but if we are using awt then canvas should be used. Mixing swing and AWT components in the same GUI lead to unexpected results.

Conclusion

In this article, we have discussed the AWT Canvas through different examples. We also discussed the constructors and methods of the AWT Canvas.

Enhance your skills in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you are just a beginner and want to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

You may consider our paid courses curated by experts to give your career an edge over others!

Do Upvote and Happy Learning!

Previous article
AWT List
Next article
AWT Scrollbar
Live masterclass