Monday 2 September 2013

FileDialog box in java


/**
 * @(#)Menu_FileDialog.java
 *
 *
 * @author - Anant Mahale
 * @version 1.00 2013/8/25
 *program for file dialog box
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Menu_FileDialog
{
    public static void main(String args[])
    {
        myframe m=new myframe();
        m.setTitle("Open & Save");
        m.setSize(400,400);
        m.setVisible(true);
        m.setLocation(100,100);
        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
    {
        JTextArea txta_show;
        JMenu file;
        JMenuItem open,save;
        JMenuBar jmb=new JMenuBar();
        mypanel()
        {
            setLayout(new BorderLayout());
            file=new JMenu("File");
           
            open=new JMenuItem("Open");
            open.addActionListener(this);
           
            save=new JMenuItem("save");
            save.addActionListener(this);
           
            file.add(open);
            file.add(save);
           
            jmb.add(file);
            setJMenuBar(jmb);
           
            txta_show=new JTextArea();
            add(txta_show);
           
        }
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==open)
            {
               
                JFileChooser jfc=new JFileChooser();
                jfc.showOpenDialog(this);
            }
            if(ae.getSource()==save)
            {
                JFileChooser jfc=new JFileChooser();
                jfc.showSaveDialog(this);
                   
            }   
        }
    }
}

Demonstration of jlist


/**
 * @(#)Change_Bg.java
 *
 *
 * @author    - Anant Mahale
 * @version 1.00 2013/8/2
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class ChangBackgroundWithList
{
    public static void main(String args[])
    {
        myframe f=new myframe();
        f.setTitle("Jlist program");
        f.setSize(500,500);
        f.setVisible(true);
        f.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 ListSelectionListener
    {
        JList lst;
        String[] lstColorNamess={ "black", "blue", "green", "yellow","white" };
        mypanel()
        {
           
            lst=new JList(lstColorNamess);
            lst.addListSelectionListener(this);
            add(lst);
        }
        public void valueChanged(ListSelectionEvent lse)
        {
            int i=lst.getSelectedIndex();
            if(i==0)
                setBackground(Color.black);   
                   
            if(i==1)
                setBackground(Color.blue);
               
            if(i==2)
                setBackground(Color.green);   
                   
            if(i==3)
                setBackground(Color.yellow);
               
            if(i==4)
                setBackground(Color.white);       
        }
    }
}

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);
        }
    }
}

Thursday 25 July 2013

Program for conversion of binary to decimal

import java.io.*;
class BintoDec
{
    public static void main(String args[])
    {
        int a,dec=0;
        try
        {
            DataInputStream cin=new DataInputStream(System.in);
            System.out.println("Enter Binary Number to Convert");
            int bin=Integer.parseInt(cin.readLine());
            for(int i=0;bin>0;i++)
            {
                a=bin%10;
                dec=dec+a*((int)Math.pow(2,i));
                bin=bin/10;
            }
            System.out.println("Given Binary number conversion in Decimal is = "+dec);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Thursday 4 July 2013

Print Pascal Triangle

/**
 * @(#)PascalTriangle.java
 *
 *
 * @author -> Anant Mahale
 * @version 1.00 2013/7/3
 */
import java.io.*;
class PascalTriangle
{
    public static void main(String []args)
    {
        try
        {
                DataInputStream cin=new DataInputStream(System.in);
            System.out.print("Enter number of rows for pascal triangle:");
    
                  int n = Integer.parseInt(cin.readLine());
                  for (int y = 0; y < n; y++)
                  {
                        int c = 1;
                        for(int q = 0; q < n - y; q++)
                        {
                              System.out.print("   ");
                        }
                        for(int x = 0; x <= y; x++)
                        {
                              System.out.print("   ");
                              System.out.print(c); // 3 digits
                              System.out.print(" ");
                              c = c * (y - x) / (x + 1);
                        }
                        System.out.println();
                        System.out.println();
                  }   
                  System.out.println();
           
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Saturday 29 June 2013

Print alphabets in java

class Alphabets
{
   public static void main(String args[])
   {
      char ch;
 
      for( ch = 'a' ; ch <= 'z' ; ch++ )
         System.out.println(ch);
   }
}

First program in java



class First
 {
  public static void main(String[] arguments)
  {
        System.out.println("Welcome to Java technology.");
  }
}

Friday 28 June 2013

Changing the Cursor in Java awt


/**
 * @(#)Text3.java
 *
 *
 * @author
 * @version 1.00 2013/6/28
 */
import java.awt.*;
import java.awt.event.*;
class Cursorch extends Frame implements ActionListener
{
    Button btn_1,btn_2;
    public Cursorch()
    {
        setLayout(new FlowLayout());
        setTitle("Cursor");
        setSize(500,500);
        setLocation(100,100);
       
        btn_1=new Button("Get Cursor 1");
        btn_1.addActionListener(this);
        add(btn_1);
       
        btn_2=new Button("Get Cursor 1");
        btn_2.addActionListener(this);
        add(btn_2);
       
        Cursor cur = btn_1.getCursor();
          Cursor cur1 = btn_2.getCursor();
          btn_1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
          btn_2.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
         
          addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
           
    }
    public void actionPerformed(ActionEvent ae)
    {
    }
}
class ChangeCursor
{
    public static void main(String []args)
    {
        new Cursorch().setVisible(true);
    }
}

Image on Frame in Java AWT


/**
 * @(#)DisplayImage.java
 *
 *
 * @author - Anant Mahale
 * @version 1.00 2013/6/28
 */
import java.awt.*;
import java.awt.event.*;
class image extends Frame
{
    Image img;
    public image()
    {
        setTitle("Image");
        setSize(300,300);
        setLocation(100,100);
       
        MediaTracker mt=new MediaTracker(this);
        img = Toolkit.getDefaultToolkit().getImage("post.jpg");
        mt.addImage(img,0);
       
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });       
    }
    public void paint(Graphics g)
    {
        if(img != null)
              g.drawImage(img, 0, 0, this);
          else
              g.clearRect(0, 0, getSize().width, getSize().height);
    }
}
class DisplayImage
{
    public static void main(String []args)
    {
        new image().setVisible(true);
    }
}

Create java .bat OR bat file

1. type java program in your java editor like notepad,textpad or jcreator ...
2. Save that java program with .java. Example "Example.java"
3. Open run then type "cmd" prem ok OR open command prompt set you directory
if you save the program in another drive then type like this ...

C:\Users\Swati>f:

here "C:\Users\Swati>" is defoult path and with "f:" it will change the driver ...
Now you will get your destination driver like

F:\>

As show in image ...











4. Then set the path of java bin folder as show in image ...

5. Then complie program like "javac first.java" ... as show in image

6. In the last run program like "java first" ...  as show in image

7. Now you get the out put of your java program ...
8. Now for creating you java bat file, click on the left-top conrer of command prompt and then click on edit section and then click on the "select all"... it will display as show in image

9. Then again , click on the left-top conrer of command prompt and then click on edit section and then click on the "copy" ...
10. Now open "notepad" and paste that matter in note pad ... it will seen like this as show in image ...

11. Now Delete unwanted data and make new look of data as show in image ...

12. now type "pause" in last of data as shown in image ...

13. If you save your java file with any name like "Example.java" so take that name like "example" ... Here I'm save my file with "first.java" so I'll take name "first" and save file as "fist.bat" ... it means you have to save that notepad file like "Example.bat" ... as show in image


14. Your .bat file is ready ... double clicking on .bat file java program will compile and run ...

Sunday 23 June 2013

How to handle String in switch case of JAVA

public class Test{

 public enum Day
 {
     SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
     THURSDAY, FRIDAY, SATURDAY;
 }
 public static void main(String args[]) {
 
  String str="SUNDAY";
 
  switch (Day.valueOf(str))
  {
      case MONDAY: 
       System.out.println("Day is Monday");
       break;
     
      case TUESDAY:
       System.out.println("Day is Tuesday");
       break;
     
      case WEDNESDAY:
       System.out.println("Day is Wednesday");
       break;
     
      case THURSDAY:
       System.out.println("Day is Thursday");
       break;
     
      case FRIDAY:
       System.out.println("Day is Friday");
       break;
     
      case SATURDAY:
       System.out.println("Day is Saturday");
       break;
     
      case SUNDAY:
       System.out.println("Day is Sunday");
       break;
  }
 }
}

Saturday 22 June 2013

Draw rectangle in java frame using Mouse Even


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

class mousedrag extends JFrame implements  MouseListener,MouseMotionListener
{
    int x,y,x1,y1;
    mousedrag()
    {
        addMouseListener(this);
        addMouseMotionListener(this);

    }

    public void mouseEntered(MouseEvent me)
    {

    }

    public void mousePressed(MouseEvent me)
    {
        x=me.getX();
        y=me.getY();

    }

    public void mouseClicked(MouseEvent me)
    {

    }


    public void mouseReleased(MouseEvent me)
    {

    }
    public void mouseMoved(MouseEvent me)

    {

    }
    public void mouseDragged(MouseEvent me)
    {
        x1=me.getX();
        y1=me.getY();
        repaint();
    }


    public void mouseExited(MouseEvent me)
    {

    }
    public void paint(Graphics g)
    {
        super.paint(g);
        g.drawRect(x,y,Math.abs(x-x1),Math.abs(y-y1));
    }

    public static void main(String args[])
    {
        mousedrag f=new mousedrag();
        f.setTitle("demo of mouse event");
        f.setSize(400,400);
        f.setVisible(true);
    }
}




demonstration of mouse event


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

class mousemove extends JFrame implements  MouseListener,MouseMotionListener
{
    int x,y,X,Y;
    mousemove()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
        x=0;
        y=0;

    }

    public void mouseEntered(MouseEvent me)
    {

    }

    public void mousePressed(MouseEvent me)
    {


    }

    public void mouseClicked(MouseEvent me)
    {

    }


    public void mouseReleased(MouseEvent me)
    {

    }
    public void mouseMoved(MouseEvent me)

    {
        x=me.getX();
        y=me.getY();
        repaint();

    }
    public void mouseDragged(MouseEvent me)
    {

    }


    public void mouseExited(MouseEvent me)
    {

    }
    public void paint(Graphics g)
    {
        super.paint(g);
        g.drawString("x="+x,50,50);
        g.drawString("Y="+y,90,50);

    }

    public static void main(String args[])
    {        mousemove f=new mousemove();
        f.setTitle("demo of mouse event");
        f.setSize(400,400);
        f.setVisible(true);
    }
}




Java swing program for multicasting


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

class multicast extends JFrame implements ActionListener
{
    JButton b,b1;
    int c=2,x=50;

    multicast()
    {
        setLayout(new FlowLayout());
        b=new JButton("click one time o/p multicast");
        b.addActionListener(this);
        add(b);

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

    }

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

        if(str=="click one time o/p multicast")
        {
            myframe f=new myframe();
            f.setSize(300,300);
            f.setLocation(20*c,20*c);
            f.setVisible(true);
            c=c+2;
        }


        if(str=="cancel")
        {
            System.exit(0);
        }
    }

    public static void main(String args[])
    {
        multicast m=new multicast();
        m.setSize(400,400);
        m.setVisible(true);
    }
}