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

Monday, 17 June 2013

Program for key event to draw various shapes


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

class keydraw extends JFrame implements KeyListener
{
    Label lbl_title;

    int ch;
    int a;

    keydraw()
    {
        a=0;
        addKeyListener(this);
    }

    public void keyPressed(KeyEvent ke)
    {
        ch=ke.getKeyCode();

    }
    public void keyTyped(KeyEvent ke)
    {

    }
    public void keyReleased(KeyEvent ke)
    {
        if(ch==ke.VK_LEFT)
           a=1;

        if(ch==ke.VK_RIGHT)
           a=2;

        if(ch==ke.VK_UP)
           a=3;

        if(ch==ke.VK_DOWN)
           a=4;

        repaint();
    }

    public void paint(Graphics g)
    {
        super.paint(g);

        if(a==1)
        g.drawLine(200,100,300,200);

        if(a==2)
        g.drawOval(200,100,200,100);

        if(a==3)
        g.drawRoundRect(200,100,50,50,20,20);

        if(a==4)
        g.drawRect(300,200,50,50);
    }

    public static void main(String arge[])
    {
        keydraw f=new keydraw();
        f.setTitle("Press direction keys");
        f.setVisible(true);
        f.setSize(400,400);
    }
}

draw various shape in java swing with JComboBox


//WAP to check combobox in panel

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

class mypanel extends JPanel implements ItemListener
{
    JComboBox cb;
    int a=0;

    mypanel()
    {
        setLayout(null);

        cb=new JComboBox();
        cb.addItem("Rect");
        cb.addItem("RoundRect");
        cb.addItem("Circle");
        cb.setBounds(50,50,100,30);
        cb.addItemListener(this);
        add(cb);
    }
    public  void itemStateChanged(ItemEvent ie)
    {
        String str=(String)cb.getSelectedItem();


        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(120,100,50,10);
        }
    }
}
class myframe extends JFrame
{
    Container c;
    myframe()
    {
        c=getContentPane();
        mypanel p=new mypanel();
        c.add(p);
    }
}

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


Program in java for JComboBox in swing


//WAP to check the combobox

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

class combo extends JFrame implements ItemListener
{
    JComboBox cb;
    Container c;

    combo()
    {
        c=getContentPane();
        c.setLayout(null);

        cb=new JComboBox();
        cb.addItem("Red");
        cb.addItem("Blue");
        cb.addItem("Pink");
        cb.setBounds(50,50,100,30);
        cb.addItemListener(this);
        c.add(cb);
    }
    public void itemStateChanged(ItemEvent ie)
    {
        String str=(String)cb.getSelectedItem();

        if(str=="Red")
        {
            c.setBackground(Color.red);
        }
        if(str=="Blue")
        {
            c.setBackground(Color.blue);
        }

        if(str=="Pink")
        {
            c.setBackground(Color.pink);

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

Java program for typing on frame


//WAP to handle Key event

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

class key1 extends JFrame implements KeyListener
{
    String msg=" ";
    char ch;

    key1()
    {
        addKeyListener(this);
    }

    public void keyPressed(KeyEvent ke)
    {
        ch=ke.getKeyChar();
        msg=msg+ch;
    }
    public void keyTyped(KeyEvent ke)
    {

    }
    public void keyReleased(KeyEvent ke)
    {
        Graphics g=getGraphics();
        g.drawString(msg,100,100);
    }
    public static void main(String args[])
    {
        key1 k=new key1();
        k.setTitle("Key event");
        k.setSize(400,400);
        k.setVisible(true);
    }
}

Friday, 14 June 2013

reverse program in java swing


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

class myframe1 extends JFrame implements ActionListener
{
    JButton b1,b2;
    JTextField tx,tx1;
    JLabel l1,l2,l3;
    Container c;
    myframe1()
    {
        c=getContentPane();
        c.setBackground(Color.pink);
        c.setLayout(null);

        l1=new JLabel("Enter any number");
        l1.setBounds(50,50,100,50);
        l1.setForeground(Color.red);
        c.add(l1);

        l3=new JLabel("Application for Reverse");
        l3.setBounds(125,0,225,50);
        l3.setForeground(Color.blue);
        c.add(l3);

        tx=new JTextField();
        tx.setBounds(200,50,150,30);
        c.add(tx);

        l2=new JLabel("Answer");
        l2.setBounds(50,150,50,50);
        l2.setForeground(Color.red);
        c.add(l2);

        tx1=new JTextField();
        tx1.setBounds(200,150,150,30);
        c.add(tx1);

        b1=new JButton("Check");
        b1.addActionListener(this);
        b1.setBounds(100,200,90,50);
        c.add(b1);

        b2=new JButton("Cancel");
        b2.addActionListener(this);
        b2.setBounds(250,200,90,50);
        c.add(b2);

    }
    public void actionPerformed(ActionEvent ae)
    {
        String str=ae.getActionCommand();
        int no,rev=0,rem;
        if(str=="Check")
        {
            no=Integer.parseInt(tx.getText());
            while(no!=0)
            {
                rem=no%10;
                rev=rev*10+rem;
                no=no/10;
            }
            tx1.setText(String.valueOf(rev));
        }
        if(str=="Cancel")
        {
            System.exit(0);
        }
    }

}
class reverse
{
    public static void main(String args[])
    {
        myframe1 f=new myframe1();

        f.setTitle("Reverse");
        f.setSize(400,400);
        f.setVisible(true);
    }
}

program in java swing - use button with swing and change background color while click on buttons


// wap in javat ot place 3 buttton red,green,blueto click red button to
//set the background of frameand so on.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class button extends JFrame implements ActionListener
{
    JButton b1,b2,b3;
    Container c=getContentPane();
    button()
    {
        c.setLayout(new FlowLayout());

        b1=new JButton("Red");
        b1.addActionListener(this);
        c.add(b1);

   
        b2=new JButton("Green");
        b2.addActionListener(this);
        c.add(b2);
       
        b3=new JButton("Blue");
        b3.addActionListener(this);
        c.add(b3);
    }
    public void actionPerformed(ActionEvent ae)
    {
        String str=ae.getActionCommand();
        if(str=="Red")
        {
            c.setBackground(Color.red);
        }
        if(str=="Green")
        {
            c.setBackground(Color.green);
        }
        if(str=="Blue")
        {
            c.setBackground(Color.blue);
        }
    }
    public static void main(String args[])
    {
        button b=new button();

        b.setTitle("Button");
        b.setSize(500,500);
        b.setVisible(true);

        b.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    }
}








Wednesday, 12 June 2013

More Applications in single frame



/**
 * @(#)AllinOne.java
 *
 *
 * @author -> Anant Mahale
 * @version 1.00 2013/5/11

  You can perform various operation with this program
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class AllinOne extends JFrame implements ActionListener
{
    private Button btn_add,btn_subtract,btn_mul,btn_div,btn_arm_strong_number,btn_palindrom,btn_prime,btn_factorial,btn_sum_digit;
    public AllinOne()
    {
        setLayout(null);
       
        setTitle("Operation");
        setSize(500,500);
        setLocation(200,200);
       
        btn_add=new Button("Addition");
        btn_add.setBounds(50,50,150,30);
        btn_add.addActionListener(this);
        add(btn_add);
       
        btn_subtract=new Button("Subtract");
        btn_subtract.setBounds(50,100,150,30);
        btn_subtract.addActionListener(this);
        add(btn_subtract);
       
        btn_mul=new Button("Multiplication");
        btn_mul.setBounds(50,150,150,30);
        btn_mul.addActionListener(this);
        add(btn_mul);
       
        btn_div=new Button("Division");
        btn_div.setBounds(50,200,150,30);
        btn_div.addActionListener(this);
        add(btn_div);
       
        btn_arm_strong_number=new Button("ArmStrong Number");
        btn_arm_strong_number.setBounds(250,50,150,30);
        btn_arm_strong_number.addActionListener(this);
        add(btn_arm_strong_number);
       
        btn_palindrom=new Button("Palindrom");
        btn_palindrom.setBounds(250,100,150,30);
        btn_palindrom.addActionListener(this);
        add(btn_palindrom);
           
        btn_prime=new Button("Prime");
        btn_prime.setBounds(250,150,150,30);
        btn_prime.addActionListener(this);
        add(btn_prime);
       
        btn_factorial=new Button("Factorial");
        btn_factorial.setBounds(250,200,150,30);
        btn_factorial.addActionListener(this);
        add(btn_factorial);
       
        btn_sum_digit=new Button("Sum Of Digit");
        btn_sum_digit.setBounds(150,250,150,30);
        btn_sum_digit.addActionListener(this);
        add(btn_sum_digit);       
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_add)
        {
            String fn=JOptionPane.showInputDialog(this,"Enter the first number");
            String sn=JOptionPane.showInputDialog(this,"Enter the Second number");
            int fno=Integer.parseInt(fn);
            int sno=Integer.parseInt(sn);
            int sum=fno+sno;
            JOptionPane.showMessageDialog(this,"Addition - "+sum);           
        }
        if(ae.getSource()==btn_subtract)
        {
            String fn=JOptionPane.showInputDialog(this,"Enter the first number");
            String sn=JOptionPane.showInputDialog(this,"Enter the Second number");
            int fno=Integer.parseInt(fn);
            int sno=Integer.parseInt(sn);
            int sub=fno-sno;
            JOptionPane.showMessageDialog(this,"Subtract - "+sub);           
        }
        if(ae.getSource()==btn_mul)
        {
            String fn=JOptionPane.showInputDialog(this,"Enter the first number");
            String sn=JOptionPane.showInputDialog(this,"Enter the Second number");
            int fno=Integer.parseInt(fn);
            int sno=Integer.parseInt(sn);
            int mul=fno*sno;
            JOptionPane.showMessageDialog(this,"Multiplication - "+mul);           
        }
        if(ae.getSource()==btn_div)
        {
            String fn=JOptionPane.showInputDialog(this,"Enter the first number");
            String sn=JOptionPane.showInputDialog(this,"Enter the Second number");
            int fno=Integer.parseInt(fn);
            int sno=Integer.parseInt(sn);
            int div=fno/sno;
            JOptionPane.showMessageDialog(this,"Division - "+div);           
        }
        if(ae.getSource()==btn_arm_strong_number)
        {
            int rem,n,no,div,arm=0;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Armstrong or not");       
            n=Integer.parseInt(str_num);
            no=n;
            while(n!=0)
            {
                rem=n%10;
                div=n/10;
                arm=arm+(rem*rem*rem);
                n=div;
            }
            if(arm==no)
            {
                JOptionPane.showMessageDialog(this,no+" is armstromg");   
            }
            else
            {
                JOptionPane.showMessageDialog(this,no+" is not armstromg");   
            }
        }
        if(ae.getSource()==btn_palindrom)
        {
            int n,no,rem,rev=0;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Palindrom or not");
            n=Integer.parseInt(str_num);
            no=n;
            while(no!=0)
            {
                rem=no%10;
                rev=rev*10+rem;
                no=no/10;
            }
            if(rev==n)
            {
                JOptionPane.showMessageDialog(this,n+" is palindrom ");   
            }
            else
            {
                JOptionPane.showMessageDialog(this,n+" is not palindrom ");   
            }
        }
        if(ae.getSource()==btn_prime)
        {
            int i,no,a=1;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Palindrom or not");       
            no=Integer.parseInt(str_num);
            for(i=2;i<no;i++)
            {
                if(no%i==0)
                {
                    a=0;
                    break;
                }
                else
                {
                    a=1;
                }
            }
            if(a==1)
            {
                JOptionPane.showMessageDialog(this,no+" is prime");   
            }
            else
            {
                JOptionPane.showMessageDialog(this,no+" is not prime");   
            }
        }
        if(ae.getSource()==btn_factorial)
        {
            int n,no,f=1;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Palindrom or not");
       
            n=Integer.parseInt(str_num);
            no=n;

            while(n>0)
            {
                f=f*n;
                n--;
            }
            JOptionPane.showMessageDialog(this,"factorial of "+no+" is = "+f);
        }
        if(ae.getSource()==btn_sum_digit)
        {
            int sum=0,n,no,rem;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Palindrom or not");
            n=Integer.parseInt(str_num);
            no=n;
            while(no!=0)
            {
                rem=no%10;
                sum=sum+rem;
                no=no/10;
            }
            JOptionPane.showMessageDialog(this,n+" of sum of digit is "+sum);
        }       
    }
}
class AllinOnePro
{
    public static void main(String args[])
    {
        new AllinOne().setVisible(true);
    }
   
}