Saturday 31 August 2013

Simple Notepad editor where user can open files, create and save files in text format.


/**
 * @(#)Open_Save_JMenuBar.java
 *
 *
 * @author -> Anant Mahale
 * @version 1.00 2013/8/27
 *
 *write a java program to implement a simple Notepad editor
 * where user can open files,
 * create and save files in text format
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class Open_Save_JMenuBar
{
    public static void main(String []args)
    {
        myframe m=new myframe();
        m.setTitle("Simple JMenu");
        m.setSize(500,500);
        m.setVisible(true);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
    class mypanel extends JPanel implements ActionListener
    {
        JMenu file;        
        JMenuItem new1,open,save;                                     
        JTextArea txta_show;   
        String msg="";
        JMenuBar mb=new JMenuBar();
        mypanel()
        {
            setLayout(new BorderLayout());
            file=new JMenu("File");
           
            new1=new JMenuItem("New");
            new1.addActionListener(this);
           
            open=new JMenuItem("Open");
            open.addActionListener(this);
           
            save=new JMenuItem("Save");
            save.addActionListener(this);
           
            file.add(new1);
            file.add(open);
            file.add(save);
           
            mb.add(file);
            setJMenuBar(mb);
           
            txta_show=new JTextArea();
            JScrollPane pane=new JScrollPane(txta_show);
            add(pane);           
        }
        public void actionPerformed(ActionEvent ae)
        {   
            String str="";
            String temp="";
            if(ae.getSource()==new1)
            {
                txta_show.setText("");
            }
            if(ae.getSource()==open)
            {
                try
                {
                    JFileChooser jfc1=new JFileChooser();
                    jfc1.showOpenDialog(this);
                    File fname1=jfc1.getSelectedFile();
                    FileReader fr=new FileReader(fname1.getPath());
                    BufferedReader bf=new BufferedReader(fr);
                    while((str=bf.readLine())!=null)
                    {
                        temp=temp+str+"\n";
                    }
                    fr.close();
                    txta_show.setText(temp);
                    setTitle(fname1.getPath());
                   
                }
                catch(Exception e)
                {
                    System.out.println(e);
                }
            }   
            if(ae.getSource()==save)
            {
                String str_gettext=txta_show.getText();       
                JFileChooser chooser = new JFileChooser();
                chooser.setCurrentDirectory( new File( "./") );
                int actionDialog = chooser.showSaveDialog(this);
                if (actionDialog == JFileChooser.APPROVE_OPTION)
                {
                    File fileName = new File(chooser.getSelectedFile( ) + "" );
                    if(fileName == null)
                        return;
                    if(fileName.exists())
                    {
                        actionDialog = JOptionPane.showConfirmDialog(this,"Replace existing file?");
                        if (actionDialog == JOptionPane.NO_OPTION)
                            return;
                    }
                    try
                    {
                        BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
                            out.write(str_gettext);
                            out.close();
                    }
                    catch(Exception e)
                    {
                         System.err.println("Error: " + e.getMessage());
                    }
                }
            }
        }
    }
}

Application for User name and password in java


/**
 * @(#)User_password.java
 *
 *
 * @author -> anant mahale
 * @version 1.00 2013/8/29
 *
 *write a program that displays a dialog box and accept username and password from the user. the
 *information entered by the user should be displayed on the screen on the client area.
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class User_password
{
    public static void main(String args[])
    {
        myframe m=new myframe();
        m.setTitle("User Password Program");
        m.setSize(400,400);
        m.setLocation(150,150);
        m.setVisible(true);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
}
class mypanel extends JPanel implements ActionListener
{
    mydialog d;
    JButton btn_clik;
    JLabel lbl_usr,lbl_pass,lbl_msg1,lbl_msg2;
    mypanel()
    {
        setLayout(null);
       
        btn_clik=new JButton("Click Here for LogIn");
        btn_clik.setBounds(100,50,200,30);
        btn_clik.addActionListener(this);
        add(btn_clik);
       
        lbl_msg1=new JLabel();
        lbl_msg1.setForeground(Color.green);
        lbl_msg1.setFont(new Font("Arial",Font.BOLD,20));
        lbl_msg1.setBounds(100,100,325,30);
        add(lbl_msg1);
       
        lbl_msg2=new JLabel();
        lbl_msg2.setForeground(Color.red);
        lbl_msg2.setFont(new Font("Arial",Font.BOLD,20));
        lbl_msg2.setBounds(100,100,325,30);
        add(lbl_msg2);
       
        lbl_usr=new JLabel();
        lbl_usr.setForeground(Color.green);
        lbl_usr.setFont(new Font("Arial",Font.BOLD,20));
        lbl_usr.setBounds(100,150,325,30);
        add(lbl_usr);
       
        lbl_pass=new JLabel();
        lbl_pass.setForeground(Color.green);
        lbl_pass.setFont(new Font("Arial",Font.BOLD,20));
        lbl_pass.setBounds(100,200,325,30);
        add(lbl_pass);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_clik)
        {
            d=new mydialog();
            d.setVisible(true);
           
            String str_usr=d.usrval();
            String str_pas=d.passval();
           
            if(str_usr.equals("Anant")&&str_pas.equals("mahale"))
            {
                lbl_msg2.setText("");
                lbl_msg1.setText("LogIn Successful");
                lbl_usr.setText("User Name - "+str_usr);
                lbl_pass.setText("Password - "+str_pas);
            }
            else
            {
                lbl_msg1.setText("");
                lbl_usr.setText("");
                lbl_pass.setText("");
                lbl_msg2.setText("Wrong User...");
            }
        }
    }
}
class mydialog extends JDialog implements ActionListener
{
    JButton btn_login,btn_exit;
    JLabel lbl_usr,lbl_pass,lbl_pass_agn,lbl_heads,lbl_err;
    JTextField txt_usr;
    JPasswordField txt_pass,txt_pass_agn;
    String str_usr="",str_pass="";
    mydialog()
    {
        super(new myframe(),"Enter User Name & Password",true);

        setLayout(null);
       
       
        setSize(400,350);
        setLocation(450,200);
       
        lbl_heads=new JLabel("Enter User Name & Password");
        lbl_heads.setBounds(100,10,200,30);
        add(lbl_heads);
       
        lbl_usr=new JLabel("User Name");
        lbl_usr.setBounds(50,50,100,30);
        add(lbl_usr);
       
        txt_usr=new JTextField();
        txt_usr.setBounds(175,50,150,30);
        add(txt_usr);       
       
        lbl_pass=new JLabel("Password");
        lbl_pass.setBounds(50,100,100,30);
        add(lbl_pass);
       
        txt_pass=new JPasswordField();
        txt_pass.setBounds(175,100,150,30);
        add(txt_pass);
       
        lbl_pass_agn=new JLabel("Password Again");
        lbl_pass_agn.setBounds(50,150,100,30);
        add(lbl_pass_agn);
       
        txt_pass_agn=new JPasswordField();
        txt_pass_agn.setBounds(175,150,150,30);
        add(txt_pass_agn);
       
        btn_login=new JButton("Login");
        btn_login.setBounds(100,200,75,30);
        btn_login.addActionListener(this);
        add(btn_login);
       
        btn_exit=new JButton("Exit");
        btn_exit.setBounds(200,200,75,30);
        btn_exit.addActionListener(this);
        add(btn_exit);
       
        lbl_err=new JLabel();
        lbl_err.setForeground(Color.red);
        lbl_err.setFont(new Font("Arial",Font.BOLD,20));
        lbl_err.setBounds(25,250,325,30);
        add(lbl_err);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_login)
        {
            String str_pas1=txt_pass.getText();
            String str_pas2=txt_pass_agn.getText();
            if(str_pas1.equals(str_pas2))
            {
                str_usr=txt_usr.getText();
                str_pass=txt_pass.getText();
                dispose();
            }
            else
            {
                lbl_err.setText("Both Passwords are not match...");
                txt_pass.setText("");
                txt_pass_agn.setText("");
                txt_usr.setText("");
            }
        }
        if(ae.getSource()==btn_exit)
        {
            System.exit(0);
        }
    }
    public String usrval()
    {
        return str_usr;
    }
    public String passval()
    {
        return str_pass;
    }
}

Monday 19 August 2013

Java program to draw 20 circles with the help of random()


/**
 * @(#)Random_Circles.java
 *
 *
 * @author - Anant Mahale
 * @version 1.00 2013/8/19
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Random_Circles
{
    public static void main(String []args)
    {
        myframe m=new myframe();
        m.setTitle("20 Circles");
        m.setSize(500,500);
        m.setVisible(true);
        m.setLocation(150,150);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
    class mypanel extends JPanel
    {
        Color randomColor;
        mypanel()
        {
        }
        public void paint(Graphics g)
        {
            super.paint(g);           
              for (int idx = 1; idx <= 20; ++idx)
              {
                  Random randomGenerator = new Random();
                  int R = (int) (Math.random( )*256);
                   int G = (int)(Math.random( )*256);
                int B= (int)(Math.random( )*256);
                randomColor = new Color(R, G, B);
                g.setColor(randomColor);
                int randomInt = randomGenerator.nextInt(100);
                g.drawOval(R,G,randomInt,randomInt);
   
            }
        }
    }
}

Sunday 18 August 2013

Simple Calculator in java


/**
 * @(#)Calculater.java
 *
 *
 * @author - Anant
 * @version 1.1 2013/8/11
 */
 package cal1_1;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 class Calculater
 {
     public static void main(String args[])
     {
         myframe m=new myframe();
         m.setTitle("Calculater Version - 1.1");
         m.setSize(500,150);
         m.setVisible(true);
         m.setLocation(150,150);
         m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
 }
 class myframe extends JFrame
 {
     mypanel p;
     myframe()
     {
         p=new mypanel();
         Container c=getContentPane();
         c.add(p);
     }
     class mypanel extends JPanel implements ActionListener
     {
         JTextField txt_show;
        JButton btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0,btn_add,btn_sub,btn_mul,btn_div,btn_eql;
        String str_get,str_set,str_final,Str_null="";
         int load_add,load_sub,load_mul,load_div,getval;
        int i=0;
        mypanel()
        {
            setLayout(null);
            setTitle("Calculater");
            setSize(500,150);
            setLocation(150,150);
            setResizable(false);
           
            txt_show=new JTextField();
            txt_show.setBounds(0,0,500,30);
            add(txt_show);
           
            btn_add=new JButton("+");
            btn_add.setBounds(0,30,100,30);
            btn_add.addActionListener(this);
            add(btn_add);
           
            btn_sub=new JButton("-");
            btn_sub.setBounds(100,30,100,30);
            btn_sub.addActionListener(this);
            add(btn_sub);
           
            btn_mul=new JButton("*");
            btn_mul.setBounds(200,30,100,30);
            btn_mul.addActionListener(this);
            add(btn_mul);
           
            btn_div=new JButton("/");
            btn_div.setBounds(300,30,100,30);
            btn_div.addActionListener(this);
            add(btn_div);
           
            btn_eql=new JButton("=");
            btn_eql.setBounds(400,30,100,30);
            btn_eql.addActionListener(this);
            add(btn_eql);
           
            btn_1=new JButton("1");
            btn_1.setBounds(0,60,100,30);
            btn_1.addActionListener(this);
            add(btn_1);
           
            btn_2=new JButton("2");
            btn_2.setBounds(100,60,100,30);
            btn_2.addActionListener(this);
            add(btn_2);
           
            btn_3=new JButton("3");
            btn_3.setBounds(200,60,100,30);
            btn_3.addActionListener(this);
            add(btn_3);
           
            btn_4=new JButton("4");
            btn_4.setBounds(300,60,100,30);
            btn_4.addActionListener(this);
            add(btn_4);
           
            btn_5=new JButton("5");
            btn_5.setBounds(400,60,100,30);
            btn_5.addActionListener(this);
            add(btn_5);
           
            btn_6=new JButton("6");
            btn_6.setBounds(0,90,100,30);
            btn_6.addActionListener(this);
            add(btn_6);
           
            btn_7=new JButton("7");
            btn_7.setBounds(100,90,100,30);
            btn_7.addActionListener(this);
            add(btn_7);
           
            btn_8=new JButton("8");
            btn_8.setBounds(200,90,100,30);
            btn_8.addActionListener(this);
            add(btn_8);
           
            btn_9=new JButton("9");
            btn_9.setBounds(300,90,100,30);
            btn_9.addActionListener(this);
            add(btn_9);
           
            btn_0=new JButton("0");
            btn_0.setBounds(400,90,100,30);
            btn_0.addActionListener(this);
            add(btn_0);
         }
         public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==btn_0)
            {
                str_get=txt_show.getText();
                str_set="0";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);   
            }
            else if(ae.getSource()==btn_1)
            {
                str_get=txt_show.getText();
                str_set="1";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);       
            }
            else if(ae.getSource()==btn_2)
            {
                str_get=txt_show.getText();
                str_set="2";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);       
            }
            else if(ae.getSource()==btn_3)
            {
                str_get=txt_show.getText();
                str_set="3";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);       
            }
            else if(ae.getSource()==btn_4)
            {
                str_get=txt_show.getText();
                str_set="4";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);       
            }
            else if(ae.getSource()==btn_5)
            {
                str_get=txt_show.getText();
                str_set="5";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);       
            }
            else if(ae.getSource()==btn_6)
            {
                str_get=txt_show.getText();
                str_set="6";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);       
            }
            else if(ae.getSource()==btn_7)
            {
                str_get=txt_show.getText();
                str_set="7";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);       
            }
            else if(ae.getSource()==btn_8)
            {
                str_get=txt_show.getText();
                str_set="8";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);       
            }
            else if(ae.getSource()==btn_9)
            {
                str_get=txt_show.getText();
                str_set="9";
                str_final=str_get.concat(str_set);
                txt_show.setText(str_final);       
            }
            else if(ae.getSource()==btn_add)
            {
                load_add=Integer.parseInt(txt_show.getText());
                txt_show.setText(Str_null);   
                    i=1;   
            }
            else if(ae.getSource()==btn_sub)
            {
                load_sub=Integer.parseInt(txt_show.getText());
                txt_show.setText(Str_null);   
                    i=2;   
            }
            else if(ae.getSource()==btn_mul)
            {
                load_mul=Integer.parseInt(txt_show.getText());
                txt_show.setText(Str_null);       
                    i=3;
            }       
            else if(ae.getSource()==btn_div)
            {
                load_div=Integer.parseInt(txt_show.getText());
                txt_show.setText(Str_null);   
                    i=4;   
            }
            else if(ae.getSource()==btn_eql)
            {
                String str1_get=txt_show.getText();
                if(i==1)
                {
                   
                    int    load_add1=Integer.parseInt(txt_show.getText());
                    int add=load_add+load_add1;
                    txt_show.setText(""+add);
                }
                else if(i==2)
                {
                    int    load_sub1=Integer.parseInt(txt_show.getText());
                    int sub=load_sub-load_sub1;
                    txt_show.setText(""+sub);
                }
                else if(i==3)
                {
                    int    load_mul1=Integer.parseInt(txt_show.getText());
                    int mul=load_mul*load_mul1;
                    txt_show.setText(""+mul);           
                }
                else if(i==4)
                {
                    int    load_div1=Integer.parseInt(txt_show.getText());
                    int div=load_div/load_div1;
                    txt_show.setText(""+div);
                }
                else if(str1_get.equals(""))
                {
                    new mydialong().setVisible(true);
               
                }
            }       
        }
     }
 }
 class mydialong extends JDialog implements ActionListener
 {
     myframe mf;
     JButton btn_yes;
     JLabel lbl_msg;
     Panel north_panel,south_panel;
     mydialong()
     {
         super(new myframe(),"Error Massage",true);
         setSize(250,150);
         setLocation(100,100);
         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         north_panel=new Panel();
         south_panel=new Panel();
         setLayout(new FlowLayout());
        
         lbl_msg=new JLabel("Field is Empty ... Enter some value ...");
         north_panel.add(lbl_msg);
        
         btn_yes=new JButton("Yes");
         btn_yes.addActionListener(this);
         south_panel.add(btn_yes);
        
         add(north_panel,"North");
         add(south_panel,"South");
     }
     public void actionPerformed(ActionEvent ae)
     {
         if(ae.getSource()==btn_yes)
         {
             dispose();
         }
     }
 }

Java ColorDialogBox program



/**
 * @(#)ColorDialog_ChangeBackground.java
 *
 *
 * @author -Anant Mahale
 * @version 1.00 2013/8/17
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class ColorDialog_ChangeBackground
{
    public static void main(String args[])
    {
        myframe m=new myframe();
        m.setTitle("Color DialogBox Demo");
        m.setSize(500,500);
        m.setLocation(100,100);
        m.setVisible(true);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
    class mypanel extends JPanel implements ActionListener
    {
        JButton btn_click;
        Color c;
        mypanel()
        {
            btn_click=new JButton("Click");
            btn_click.addActionListener(this);
            add(btn_click);
        }
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==btn_click)
            {
                try
                {
                    c=JColorChooser.showDialog(this,"Choose Your Color",Color.pink);
                    setBackground(c);
                }   
                catch(Exception e)
                {
                    System.out.println(e);
                }
            }
        }
    }
   
}

Sunday 11 August 2013

Program in java Decimal to Binary conversion

import java.io.*;
class DectoBin
{
    public static void main(String args[])
    {
        int i=0;
        int bin[]=new int[10];
        try
        {
            DataInputStream cin=new DataInputStream(System.in);
            System.out.println("Enter Decimal Number");
            int dec=Integer.parseInt(cin.readLine());
            while(dec>0)
            {
                bin[i]=dec%2;
                dec=dec/2;
                i++;
            }
            i--;
            for(int j=i;j>=0;j--)
            System.out.print(bin[j]);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}