Tuesday, 18 March 2014

Java Program for Delete File

//delete file

import java.io.File;

class DeleteFile
{
    public static void main(String args[])
    {
        File f=new File("c:\\ram.txt");
        if(f.exists())
        {
            f.delete();
        }
    }
}

Saturday, 18 January 2014

Simple Text editor in java


/**
 * @(#)Text_Editor.java
 *
 *
 * @author anant
 * @version 1.00 2013/9/17
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Text_Editor
{
    public static void main(String []args)
    {
        myframe m=new myframe();
        m.setTitle("Text Editor");
        m.setSize(500,500);
        m.setVisible(true);
        m.setLocation(500,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,ItemListener
    {
        JTextArea txta_show;
        JComboBox cmb_font;
        JLabel lbl_size;
        JTextField txt_size;
        JCheckBox cb_bold,cb_italic;
        int i=0;
        String font;
        JButton btn_ok;
        mypanel()
        {
            setLayout(null);
           
            txta_show=new JTextArea();
            JScrollPane pane=new JScrollPane(txta_show);
            pane.setBounds(50,10,400,300);
            add(pane);
           
            cmb_font=new JComboBox();           
            cmb_font.addItem("Curlz MT");
            cmb_font.addItem("Forte");
            cmb_font.addItem("Freestyle Script");
            cmb_font.addItem("French Script MT");
            cmb_font.addItem("Gigi");
            cmb_font.addItem("Harrington");
            cmb_font.addItem("Informal Roman");
            cmb_font.addItem("Jokerman");
            cmb_font.setBounds(50,350,100,30);
            cmb_font.addItemListener(this);
            add(cmb_font);
           
            lbl_size=new JLabel("Size");
            lbl_size.setBounds(175,350,75,30);
            add(lbl_size);
           
            txt_size=new JTextField();
            txt_size.setBounds(210,350,50,30);
            add(txt_size);   
               
            cb_bold=new JCheckBox("Bold");
            cb_bold.setBounds(275,350,75,30);
            cb_bold.addActionListener(this);
            add(cb_bold);
           
            cb_italic=new JCheckBox("Italic");
            cb_italic.setBounds(350,350,100,30);
            cb_italic.addActionListener(this);
            add(cb_italic);   
               
            btn_ok=new JButton("Ok");
            btn_ok.setBounds(50,400,100,30);
            btn_ok.addActionListener(this);
            add(btn_ok);           
        }
        public void itemStateChanged(ItemEvent ie)
        {
            if(ie.getSource()==cmb_font)
            {
                i=cmb_font.getSelectedIndex();
                if(i==0)
                    font="Curlz MT";
                else if(i==1)
                    font="Forte";
                else if(i==2)
                    font="Freestyle Script";
                else if(i==3)
                    font="French Script MT";
                else if(i==4)
                    font="Gigi";
                else if(i==5)
                    font="Harrington";
                else if(i==6)
                    font="Informal Roman";
                else if(i==7)
                    font="Jokerman";
            }
        }
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==btn_ok)
            {
                int size=Integer.parseInt(txt_size.getText());
                if(cb_bold.isSelected()&&cb_italic.isSelected())
                    txta_show.setFont(new Font(font,Font.BOLD+Font.ITALIC,size));
                else if(cb_bold.isSelected())
                    txta_show.setFont(new Font(font,Font.BOLD,size));
                else if(cb_italic.isSelected())
                    txta_show.setFont(new Font(font,Font.ITALIC,size));
                else
                    txta_show.setFont(new Font(font,Font.PLAIN,size));                   
            }
        }
    }
}

Bouncing Rectangle using thread


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
class Rect_bounce
{
    public static void main(String []args)
    {
        myframe m=new myframe();
        m.setTitle("Bouncing Rectangle");
        m.setSize(500,500);
        m.setVisible(true);
        m.setLocation(300,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,Runnable
{
    Thread animation;
    JButton btn_fast,btn_slow;
    int x=150,y=50,dx=11,dy=7,r=50,slp=500;
    mypanel()
    {
        animation=new Thread(this,"Bounce rect");
        animation.start();
       
        btn_fast=new JButton("Fast");
        btn_fast.addActionListener(this);
        add(btn_fast);
       
        btn_slow=new JButton("Slow");
        btn_slow.addActionListener(this);
        add(btn_slow);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_fast)
        {
            slp=slp-50;
        }
        if(ae.getSource()==btn_slow)
        {
            slp=slp+50;
        }
    }
    public void run()
    {
        try
        {
            while(true)
            {
                Rectangle bounds=getBounds();
                if((x-r+dx<0)||(x+r+dx>bounds.width))
                    dx=-dx;
                if((y-r+dy<0)||(y+r+dy>bounds.height))
                    dy=-dy;
                   
                x+=dx;
                y+=dy;
               
                repaint();
                animation.sleep(slp);
            }               
        }
        catch(Exception e)
        {
        }
    }
    public void paint(Graphics g)
    {
            super.paint(g);
        Graphics2D g2=(Graphics2D)g;
       
        Rectangle2D.Double r1=new Rectangle2D.Double(x-r,y-r,r*2,r*2);
        g2.draw(r1);
        g2.setPaint(Color.red);
        g2.fill(r1);
    }
}

Play wav file in java

/**
 * @(#)play_wav .java
 *
 *
 * @author anant
 * @version 1.00 2013/12/28
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.applet.*;
class play_wav
{
    public static void main(String args[])
    {
        myframe m=new myframe();
        m.setTitle("Play Sound");
        m.setSize(500,500);
        m.setVisible(true);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    myframe()
    {
        try
        {
            File WaveFile=new File("Phoolon Jaisi.wav");
            AudioClip Sound;
            Sound=Applet.newAudioClip(WaveFile.toURL());
            Sound.play();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

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