求助,做一个简单的画图程序。
做一个简单的画图程序,画一个圆和椭圆,我增加了一个按钮clear,那要如何清除画的图形,这个命令代码我不会向各位高手请教??还有运行结束后,关闭时只有关闭了整个运行软件,程序才关闭这又是怎么回事?代码如下:
import java.awt.*;
import java.awt.event.*;
public class Draw extends Frame implements ActionListener{
static Draw frm = new Draw();
static Button but1 = new Button("Draw circle");
static Button but2 = new Button("Draw oval");
static Button but3 = new Button("Clear");
int circle = 0;
public static void main (String arg[]){
frm.setTitle("DRAW程序");
frm.setSize(300,250);
frm.setLayout(null);
but1.setBounds(20,215,50,25);
but2.setBounds(100,215,50,25);
but3.setBounds(180,215,50,25);
frm.add(but1);
frm.add(but2);
frm.add(but3);
but1.addActionListener(frm);
but2.addActionListener(frm);
but3.addActionListener(frm);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Button bt = (Button)e.getSource();
Button cl = (Button)e.getSource();
if (bt == but1) circle = 1;
else circle = 2;
Graphics g = getGraphics();
paint(g);
}
public void paint(Graphics g)
{
g.setFont(new Font("楷体",Font.ITALIC,20));
g.setColor(Color.red);
g.drawString("Drwa cricle or oval ",50,50);
if(circle == 1)
g.drawOval(100,90,70,70);
else if (circle ==2)
g.drawOval(80,60,70,120);
}