Hi everyone, i need your help.. I want to draw a circle,square and rectangle on Jframe on a click of a button.. This program was asked in my test paper, i couldn't get it right though bt i wanna know what my mistake is.. Plz help me..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CRS extends JFrame implements ActionListener
{
JButton b1,b2,b3;
Graphics g;
Container con;
public CRS()
{
super("Draw Circle in JFrame");
con=getContentPane();
con.setLayout(new FlowLayout());
b1=new JButton("Circle");
b2=new JButton("Rectangle");
b3=new JButton("Square");
con.add(b1);
con.add(b2);
con.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void Circle()
{
g.drawOval(50,50,100,100);
}
public void Rectangle()
{
g.drawRect(250,350,80,40);
}
public void Square()
{
g.drawRect(100,200,100,100);
}
public void paint()
{
Circle();
Rectangle();
Square();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
try
{
Circle();
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(this,"Error");
}
}
if(ae.getSource()==b2)
{
try
{
Rectangle();
}
catch(Exception e2)
{
JOptionPane.showMessageDialog(this,"Error");
}
}
if(ae.getSource()==b3)
{
try
{
Square();
}
catch(Exception e3)
{
JOptionPane.showMessageDialog(this,"Error");
}
}
}
public static void main(String args[])
{
CRS aa=new CRS();
aa.setVisible(true);
aa.setSize(400,400);
aa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}