Showing posts with label radio button. Show all posts
Showing posts with label radio button. Show all posts

Saturday, 22 June 2013

Draw Various shapes with radio button in Java swing


//WAP to check JRadioButton in panel

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class mypanel extends JPanel implements ActionListener
{
    JRadioButton red,blue,green;
    ButtonGroup color;
    int a;

    mypanel()
    {
        setLayout(null);

        red=new JRadioButton("Rect");
        red.addActionListener(this);
        red.setBounds(50,50,100,30);
        add(red);

        blue=new JRadioButton("RoundRect");
        blue.addActionListener(this);
        blue.setBounds(50,100,100,30);
        add(blue);

        green=new JRadioButton("Circle");
        green.addActionListener(this);
        green.setBounds(50,150,100,30);
        add(green);

        color=new ButtonGroup();

        color.add(red);
        color.add(blue);
        color.add(green);
    }

    public  void actionPerformed(ActionEvent ae)
    {
        String str=ae.getActionCommand();


        if(str=="Rect")
        a=1;

        if(str=="RoundRect")
        a=2;

        if(str=="Circle")
        a=3;
        repaint();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(a==1)
        {
          g.drawRect(300,200,50,50);
        }
        if(a==2)
        {
          g.drawRoundRect(100,250,50,50,20,20);
        }
        if(a==3)
        {
          g.drawOval(200,100,50,100);
        }
    }
}
class myframe extends JFrame
{
    Container c;
    myframe()
    {
        c=getContentPane();
        mypanel p=new mypanel();
        c.add(p);
    }
}

class rbpanel
{
    public static void main(String args[])
    {
        myframe f=new myframe();
        f.setTitle("RadioButton panel");
        f.setSize(500,500);
        f.setVisible(true);
    }
}