Could somebody please tell me what is the general function of the Canvas class under the java.awt package? Under what situation do I have to call this class for use? Thanx.
Printable View
Could somebody please tell me what is the general function of the Canvas class under the java.awt package? Under what situation do I have to call this class for use? Thanx.
It is generally subclassed and used as an area to paint things on.
:)
The canvas class is pretty fun to play with
even if your not really into graphic
programming. Below is a pretty
simple example that i whipped up. :)
Code:
import java.awt.*;
import java.awt.event.*;
public class Shape{
public static void main(String[] args){
new Circle();
}
}
class Circle extends Canvas{
public Circle(){
JFrame jf = new JFrame("Circle!");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Dimension size = jf.getSize();
Insets i = jf.getInsets();
int height = size.height - i.top - i.bottom;
int width = size.width - i.left - i.right;
this.setSize(height,width);
jf.getContentPane().add(this);
jf.setSize(400,300);
jf.setVisible(true);
}
public void paint(Graphics g){
g.setColor(Color.red);
g.fillOval(50,50,50,50);
}
}
Thanx buddy, I appreciate the example given.