Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Aviance School is one of the largest web solutions platform in India for developers to learn and share their programming knowledge and build their careers.
The Canvas control represents a blank rectangular area where the user can draw or trap input events from the user.
Canvas class inherits the Component class.
Constructor of the Canvas class:
Canvas()
Canvas(GraphicsConfiguration c)
Methods:
addNotify()
createBufferStrategy(int n)
createBufferStrategy(int n, BufferCapabilities c)
getBufferStrategy()
paint(Graphics g)
update(Graphics g)
Basic Example:
import java.awt.*;
public class MyCanvasExample
{
public MyCanvasExample()
{
Frame f= new Frame("My Canvas Example:");
f.add(new MyCanvas());
f.setLayout(null);
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String args[])
{
new MyCanvasExample();
}
}
class MyCanvas extends Canvas
{
public MyCanvas() {
setBackground (Color.BLACK);
setSize(250, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(60, 60, 120, 60);
}
}