Showing posts with label draw oval. Show all posts
Showing posts with label draw oval. 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);
    }
}


Friday, 21 June 2013

Creat Menu bar in Java Swing and draw various shapes




// TO create Menu Bar

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

class menu extends JFrame implements ActionListener
{
    JMenuBar mb;
    JMenu draw;
    JMenuItem rect,line,oval;
    //Container c;
    menu()
    {
        //c=getContentPane();
        setLayout(null);
        mb=new JMenuBar();

        draw=new JMenu("draw");
        draw.setMnemonic('d');
        mb.add(draw);

        rect=new JMenuItem("rect");
        rect.addActionListener(this);
        rect.setMnemonic('r');
        draw.add(rect);

        line=new JMenuItem("line");
        line.addActionListener(this);
        line.setMnemonic('l');
        draw.add(line);

        oval=new JMenuItem("oval");
        oval.addActionListener(this);
        oval.setMnemonic('o');
        draw.add(oval);

        setJMenuBar(mb);
    }

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

        if(str=="rect")
        g.drawRect(100,100,50,50);

        if(str=="line")
        g.drawLine(300,50,400,350);

        if(str=="oval")
        g.drawOval(400,50,50,50);
    }

    public static void main(String args[])
    {
        menu f=new menu();
        f.setTitle("my frame");
        f.setSize(500,500);
        f.setVisible(true);
    }
}

Friday, 14 June 2013

program in java - draw oval with awt

import java.awt.*;
class a2 extends Frame
{
    public static void main(String args[])
    {
        a2 a=new a2();
        a.setTitle("oval");
        a.setSize(500,500);
        a.setVisible(true);
        //a.show();
    }
    public void paint(Graphics g)
    {
        g.drawOval(300,100,50,50);

        g.drawOval(40,250,100,50);

    }
}