Tuesday, April 11, 2017

Currency Converter Java

Code::

import javax.swing.*;
import java.awt.event.*;
public class CurrencyConverter extends JFrame
{    JTextField t1=new JTextField();
     JTextField t2=new JTextField();
     JTextField t3=new JTextField();
     JLabel l1=new JLabel("Rupees:");
     JLabel l2=new JLabel("US Dollars:");
     JLabel l3=new JLabel("Euros:");
     CurrencyConverter()
     {
        setLayout(null);
        setTitle("Converter");
        setVisible(true);
        setSize(400,200);
        l1.setBounds(20,30,70,30);
        l2.setBounds(20,70,70,30);
        l3.setBounds(20,110,70,30);
        t1.setBounds(120,30,70,30);
        t2.setBounds(120,70,70,30);
        t3.setBounds(120,110,70,30);
        add(t1);
        add(l1);
        add(l2);
        add(t2);
        add(t3);
        add(l3);
        t1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                double a=Double.parseDouble(t1.getText());
                t2.setText(String.format("%.2f",a*0.015));
                t3.setText(String.format("%.2f",a*0.015));
                
            }
        });
        t2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                double a=Double.parseDouble(t2.getText());
                t1.setText(String.format("%.2f",a*64.68));
                t3.setText(String.format("%.2f",a*0.94));
                
            }
        });
        t3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                double a=Double.parseDouble(t3.getText());
                t2.setText(String.format("%.2f",a*1.06));
                t1.setText(String.format("%.2f",a*68.71));
                
            }
        });
     }
      public static void main(String[] nt) 
    {
        new CurrencyConverter();
    }
    
}

Output::


Temperature Converter Java

Code::

import javax.swing.*;
import java.awt.event.*;
public class Converter extends JFrame 
{
    
    JTextField t1 =new JTextField();
    JTextField t2 =new JTextField();
    JLabel l1=new JLabel("Celsius:");
    JLabel l2=new JLabel("Fahrenheit:");
    Converter()
    {
        setLayout(null);
        setTitle("Converter");
        setVisible(true);
        setSize(400,200);
        l1.setBounds(20,30,70,30);
        l2.setBounds(20,70,70,30);
        t1.setBounds(120,30,70,30);
        t2.setBounds(120,70,70,30);
        add(t1);
        add(l1);
        add(l2);
        add(t2);
        t1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {  double a=(Double.parseDouble(t1.getText()))*1.8+32;
                t2.setText(String.format("%.1f",a));
            }
        });
        t2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {  double a=(((Double.parseDouble(t2.getText()))-32)/1.8);
                t1.setText(String.format("%.1f",a));
            }
        });
    }
   
    public static void main(String[] nt) {
      new Converter();
    }
    
}

Output::

Monday, April 10, 2017

Puzzle Game Java

Code::

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Puzzle extends Applet implements ActionListener
{   Button b[]=new Button[9];
    Button r=new Button("Reset");

    public void init()
    {  setLayout(null);
       setSize(400,400);
       JOptionPane.showMessageDialog(this,"Make sequence 1,2,3... by Clicking on buttons...");
        int x=50;
        int y=50;
       
        for (int i=0;i<9;i++)
        {  b[i]=new Button(String.valueOf(i+1));
           b[i].setBounds(x+50,y+50,30,30);
           x+=50;
           if((i+1)%3==0)
           {
               y+=50;
               x=50;
           }
           add(b[i]);
           b[i].addActionListener(this);
        }
        b[8].setLabel("2");
        b[1].setLabel("");
       r.setBounds(140,250,50,50);
       r.addActionListener(this);
       add(r);
    }

   public void actionPerformed(ActionEvent e)
   {
         if(e.getSource()==b[0])
         {
             if(b[1].getLabel()=="")
             {
                 b[1].setLabel(b[0].getLabel());
                 b[0].setLabel("");
             }
             if(b[3].getLabel()=="")
             {
                 b[3].setLabel(b[0].getLabel());
                 b[0].setLabel("");
             }
         }
         if(e.getSource()==b[8])
         {
           
             if(b[5].getLabel()=="")
             {
                 b[5].setLabel(b[8].getLabel());
                 b[8].setLabel("");
             }
             if(b[7].getLabel()=="")
             {
                 b[7].setLabel(b[8].getLabel());
                 b[8].setLabel("");
             }
         }
         if(e.getSource()==b[1])
         {
             if(b[0].getLabel()=="")
             {
                 b[0].setLabel(b[1].getLabel());
                 b[1].setLabel("");
             }
             if(b[2].getLabel()=="")
             {
                 b[2].setLabel(b[1].getLabel());
                 b[1].setLabel("");
             }
             if(b[4].getLabel()=="")
             {
                 b[4].setLabel(b[1].getLabel());
                 b[1].setLabel("");
             }
         }
         if(e.getSource()==b[2])
         {
             if(b[1].getLabel()=="")
             {
                 b[1].setLabel(b[2].getLabel());
                 b[2].setLabel("");
             }
             if(b[5].getLabel()=="")
             {
                 b[5].setLabel(b[2].getLabel());
                 b[2].setLabel("");
             }
         }
         if(e.getSource()==b[3])
         {
             if(b[0].getLabel()=="")
             {
                 b[0].setLabel(b[3].getLabel());
                 b[3].setLabel("");
             }
             if(b[4].getLabel()=="")
             {
                 b[4].setLabel(b[3].getLabel());
                 b[3].setLabel("");
             }
             if(b[6].getLabel()=="")
             {
                 b[6].setLabel(b[3].getLabel());
                 b[3].setLabel("");
             }
         }
         if(e.getSource()==b[4])
         {
             if(b[1].getLabel()=="")
             {
                 b[1].setLabel(b[4].getLabel());
                 b[4].setLabel("");
             }
             if(b[3].getLabel()=="")
             {
                 b[3].setLabel(b[4].getLabel());
                 b[4].setLabel("");
             }
             if(b[5].getLabel()=="")
             {
                 b[5].setLabel(b[4].getLabel());
                 b[4].setLabel("");
             }
             if(b[7].getLabel()=="")
             {
                 b[7].setLabel(b[4].getLabel());
                 b[4].setLabel("");
             }
         }
         if(e.getSource()==b[5])
         {
             if(b[2].getLabel()=="")
             {
                 b[2].setLabel(b[5].getLabel());
                 b[5].setLabel("");
             }
             if(b[4].getLabel()=="")
             {
                 b[4].setLabel(b[5].getLabel());
                 b[5].setLabel("");
             }
             if(b[8].getLabel()=="")
             {
                 b[8].setLabel(b[5].getLabel());
                 b[5].setLabel("");
             }
         }
         if(e.getSource()==b[7])
         {
             if(b[4].getLabel()=="")
             {
                 b[4].setLabel(b[7].getLabel());
                 b[7].setLabel("");
             }
             if(b[6].getLabel()=="")
             {
                 b[6].setLabel(b[7].getLabel());
                 b[7].setLabel("");
             }
             if(b[8].getLabel()=="")
             {
                 b[8].setLabel(b[7].getLabel());
                 b[7].setLabel("");
             }
         }
         if(e.getSource()==b[6])
         {
             if(b[3].getLabel()=="")
             {
                 b[3].setLabel(b[6].getLabel());
                 b[6].setLabel("");
             }
             if(b[7].getLabel()=="")
             {
                 b[7].setLabel(b[6].getLabel());
                 b[6].setLabel("");
             }
         }
     if(b[0].getLabel().equals("1")&&b[1].getLabel().equals("2")&&b[2].getLabel()  
            .equals("3")&&b[3].getLabel().equals("4")&&b[4].getLabel().equals("5")  
            &&b[5].getLabel().equals("6")&&b[6].getLabel().equals("7")&&b[7].getLabel()  
            .equals("8")&&b[8].getLabel().equals(""))
     {    
            JOptionPane.showMessageDialog(this,"Congratulations! You won.");  
     }
     if(e.getSource()==r)
     { for(int i=0;i<9;i++)
     {
         b[i].setLabel(String.valueOf(i+1));
     }
    int  j=(int)(7*Math.random());
    if(j==5)
    {
        j=(int)(7*Math.random());
    }
     b[8].setLabel(b[j].getLabel());
     b[j].setLabel("");
     }
   }
   public void paint(Graphics g)
   {
       g.drawImage(getImage(getDocumentBase(),"23.jpg"),0,0,this);
       g.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,25));
       g.drawString("Puzzle Game",105,40);
       g.drawString("By Dishant",125,350);
   }

}

Output::

Thursday, April 6, 2017

Calculator in Java

Following Code will make a Calculator:

Code::

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Calci extends Applet implements ActionListener {
     Button [] b=new Button[14];
     TextField t=new TextField();
     String Op="";
     int n=0,d=0;
     Button eq=new Button("=");
     Button cl=new Button("CE");
     
     public void init() 
    {
        
        setLayout(null);
        t.setBounds(50,20,110,20);
        int x=0;
        int y=0;
        for(int i=0;i<10;i++)
        {  if(i%4==0 && i!=0)
            {
                y+=50;
                x=0;
            }
            b[i]=new Button(String.valueOf(i));
            b[i].setBounds(x+50,y+50,20,20);
            x +=30;
            
            add(b[i]);
            b[i].addActionListener(this);
        }
        String[] s={"+","-","*","/"};
        int j=0;
        int u=0;
        int v=150;
        for(int i=10;i<14;i++)
        {
            b[i]=new Button(s[j++]);
            b[i].setBounds(u+50,v+50,20,20);
            add(b[i]);
            u+=30;
            b[i].addActionListener(this);
        }
        eq.addActionListener(this);
        eq.setBounds(110,150,20,20);
        add(eq);
        cl.addActionListener(this);
        cl.setBounds(140,150,20,20);
        add(cl);
        add(t);
        setSize(300,300);
        
    }
    public void actionPerformed(ActionEvent e)
    {
        
        for(int i=0;i<10;i++)
        {
            if(e.getSource()==b[i])
            {
                t.setText(t.getText()+b[i].getLabel());
              
            }
        }
        for(int i=10;i<14;i++)
        {
        if(e.getSource()==b[i])
        {   
         Op=b[i].getLabel();
         d=Integer.parseInt(t.getText());
         t.setText("");
        }
        }
        if(e.getSource()==eq)
        { int r=0;
        n=Integer.parseInt(t.getText());
           switch(Op)
           {
               case "+":  r=d+n;
                         break;
               case "-":  r=d-n;
                         break;
               case "*":  r=d*n;
                         break;
               case "/": r=d/n;
                         break;
           }
            t.setText(String.valueOf(r));
        }
        if(e.getSource()==cl)
        {
            t.setText("");
        }
    }
    public void paint(Graphics g)
    {  Image p=getImage(getDocumentBase(),"23.jpg");
        g.drawImage(p, 0, 0, this);
        g.setFont(new Font("",Font.TYPE1_FONT,25));
        g.drawString("By Dishant",50, 280);
    }

}

Output::





Tuesday, April 4, 2017

TicTacToe Java

Following Code extends Applet and Allows the users to play the TicTacToe Game.

Code::

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class TicTacToe extends Applet implements ActionListener {
    
    Button b[][]=new Button[3][3];
    int c=1,w=0;
    String p1="",p2="";
    public void init() 
    { setLayout(null);
     Frame f=new Frame("D");
        Dialog d=new Dialog(f,"Dialog Example",true);
        d.setLayout(null);
        Button s=new Button("Submit");
        s.setBounds(50,150,50,50);
        Label l1=new Label("Player1:");
        Label l2=new Label("Player2:");
        TextField t1=new TextField("");
        TextField t2=new TextField("");
        l1.setBounds(50,50,50,50);
        l2.setBounds(50,100,50,50);
        t1.setBounds(120,70,100,30);
        t2.setBounds(120,120,100,30);
       s.addActionListener ( new ActionListener()  
        {  
            public void actionPerformed( ActionEvent e )  
            {  
                p1=t1.getText();
                p2=t2.getText();
                d.setVisible(false);  
            }  
        }); 
         d.add(s);
         d.add(l1);
         d.add(l2);
         d.add(t1);
         d.add(t2);
         d.setSize(300,300);
         d.setVisible(true);  
      int x=100;
      int y=100;
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {   b[i][j]=new Button();
                b[i][j].setBounds(x+100,y+100,50,50);
                add(b[i][j]);
                x+=100;
            }
            y+=100;
            x=100;
        }
        
      for(int i=0;i<3;i++)
      {
          for(int j=0;j<3;j++)
          {
              b[i][j].addActionListener(this);
          }
      }
        Button R=new Button("Reset");
        R.setBounds(290,500,70,50);
        add(R);
        R.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                for(int i=0;i<3;i++)
                {
                    for(int j=0;j<3;j++)
                    {
                        b[i][j].setLabel("");
                    }
                }
                c=1;w=0;
            }
        });
      

    }
    public void actionPerformed(ActionEvent e)
    {  for(int i=0;i<3;i++)
    {
        
    for(int j=0;j<3;j++)
    {
        if(e.getSource()==b[i][j])
        {
            if(c%2!=0)
        {
            b[i][j].setLabel("X");
        }
            else
            {
                b[i][j].setLabel("0");
            }
         b[i][j].setEnabled(false);
        }
    }
    
    }
    c++;
   
    for(int i=0;i<3;i++)
    {    int j=0;
        if(b[i][j].getLabel().equals("X")&&b[i][j+1].getLabel().equals("X")&&b[i][j+2].getLabel().equals("X"))
        {
            JOptionPane.showMessageDialog(null,p1+" Wins");
            w=1;
        }
        
    }
    for(int i=0;i<3;i++)
    {    int j=0;
        if(b[i][j].getLabel().equals("0")&&b[i][j+1].getLabel().equals("0")&&b[i][j+2].getLabel().equals("0"))
        {
            JOptionPane.showMessageDialog(null,p2+" Wins");
            w=1;
        }
    }
    for(int j=0;j<3;j++)
    {    int i=0;
        if(b[i][j].getLabel().equals("X")&&b[i+1][j].getLabel().equals("X")&&b[i+2][j].getLabel().equals("X"))
        {
            JOptionPane.showMessageDialog(null,p1+" Wins");
            w=1;
        }
    }
    for(int j=0;j<3;j++)
    {    int i=0;
        if(b[i][j].getLabel().equals("0")&&b[i+1][j].getLabel().equals("0")&&b[i+2][j].getLabel().equals("0"))
        {
            JOptionPane.showMessageDialog(null,p2+" Wins");
            w=1;
        }
    }
    int i=0,j=0;
    if(b[i][j].getLabel().equals("0")&&b[i+1][j+1].getLabel().equals("0")&&b[i+2][j+2].getLabel().equals("0"))
        {
            JOptionPane.showMessageDialog(null,p2+" Wins");
            w=1;
        }
    
    i=0;
    j=0;
    if(b[i][j].getLabel().equals("X")&&b[i+1][j+1].getLabel().equals("X")&&b[i+2][j+2].getLabel().equals("X"))
        {
            JOptionPane.showMessageDialog(null,p1+" Wins");
            w=1;
        }
    i=0;
    j=2;
    if(b[i][j].getLabel().equals("X")&&b[i+1][j-1].getLabel().equals("X")&&b[i+2][j-2].getLabel().equals("X"))
        {
            JOptionPane.showMessageDialog(null,p1+" Wins");
            w=1;
        }
    i=0;
    j=2;
    if(b[i][j].getLabel().equals("0")&&b[i+1][j-1].getLabel().equals("0")&&b[i+2][j-2].getLabel().equals("0"))
        {
            JOptionPane.showMessageDialog(null,p2+" Wins");
            w=1;
        }
    if(c>9 && w==0)
    {
        JOptionPane.showMessageDialog(null,"Draw");
    }
    
    }
    public void paint(Graphics g)
    {   Image i=getImage(getDocumentBase(),"23.jpg");
        g.setColor(Color.BLACK);
        g.setFont(new Font("",Font.BOLD,50));
        g.drawImage(i,0, 0, this);
        g.drawString("TicTacToe",200,100);
        
        g.drawLine(270, 180,270,480);
        g.drawLine(375, 180,375,480);
        g.drawLine(180,270,480,270);
        g.drawLine(180,375,480,375);
        g.setColor(Color.BLUE);
        g.drawString("By Dishant",200,650);
    }
}

Output::




Monday, April 3, 2017

Digital Clock Java Using Applet

Following class extends Applet and implements Runnable.

Code::

import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.text.*;
public class DigitalClock extends Applet implements Runnable {
    Thread t;
    String Time="";
    
    public void init() 
    {       setSize(400,200);
            setBackground(Color.CYAN);
            t=new Thread(this);
            t.start();
    }

    public void run()
    {
       try
       {
           while(true)
           {
               Calendar c=Calendar.getInstance();
               SimpleDateFormat f=new SimpleDateFormat("hh:mm:ss "+" dd/MM/yyyy");
               
               Date d=c.getTime();
               Time=f.format(d);
               repaint();
               t.sleep(1000);
           }
       }catch(Exception e)
       {
           System.out.println("Error Occured");
       }
    }
    
    public void paint(Graphics g)
    {
        g.setColor(Color.RED);
        g.setFont(new Font("Dialog",Font.BOLD,50));
        g.drawString(Time, 100,100);
    }
}


Output::

Thursday, March 16, 2017

Exception Handling Using Try Catch Java

The statements which can throw exception are placed in try block and after the exception is thrown the catch block following the try block are executed and the program is not abnormally terminated.

Code::

import java.util.*;
public class Exception 
{

    
    public static void main(String[] nt) 
    {
        Scanner in =new Scanner(System.in);
        System.out.print("Enter any Number:");
        try
        {
        int a=in.nextInt();
        int b=12345/a;
        System.out.println("Result is :"+b);
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception: " +e);
        }
        
    }

}

Output::