我不得不使用小程序来编写一个程序,它应该有3个按钮线,矩形,他们circle.Upon单击所需的形状应绘制。的Java绘制形状误差
我写了下面的代码,但它显示出图形未初始化错误。 现在该怎么办?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Drawshapes extends Applet implements ActionListener
{
Button line,rect,circle;
public void init()
{
line=new Button("Line");
rect=new Button("Rectangle");
circle=new Button("Circle");
add(line);
add(circle);
add(rect);
line.addActionListener(this);
rect.addActionListener(this);
circle.addActionListener(this);
}
public void paint(Graphics g)
{
}
public void actionPerformed(ActionEvent ae)
{
Graphics g;
if(ae.getSource()==line)
{
g.drawLine(0,100,100,10);
}
else if(ae.getSource()==rect)
{
g.drawRect(10,10,60,50);
}
else
{
g.drawOval(10,10,50,50);
}
}
}
确定我会努力做到这一点使用油漆methos – Amol 2013-03-15 22:43:12
我一定要显示按钮,所以我需要使用ActionListener.How我可以绘制它使用paint方法的单击事件的形状? – Amol 2013-03-15 22:46:44
设置一个标志(最好是'enum'),然后调用'repaint'。上述 – Reimeus 2013-03-15 22:47:57