17 Oct 2014

Age Calculator using Java Applet

This program demonstrates:
  • The use of various layouts and panels in Java Applet
  • The use of User defined Exceptions
  • The use of Thread
  • The use of GregorianCalendar Class

Code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/*
<applet code="AgeCalc.class" height=150 width=500>
</applet>
*/

class FieldEmptyException extends Exception
{
    FieldEmptyException()
    {
    }
}

public class AgeCalc extends Applet implements Runnable,ActionListener
{
Choice ch1,ch2;
TextField tf1;
Button b1,b2;
Panel p1,p2,p3,p4;
Label l0,l1,l2;
int x=0;
    public void init()
    {
    Thread t=new Thread(this);
    t.start();
    setLayout(new BorderLayout());
    p1=new Panel(new GridLayout(3,1));
    p2=new Panel(new FlowLayout(FlowLayout.CENTER));
    p3=new Panel(new FlowLayout(FlowLayout.CENTER));
    p4=new Panel(new FlowLayout(FlowLayout.CENTER));
    add("South",p1);
    p1.add(p2);
    p1.add(p3);
    p1.add(p4);
    l1=new Label("The program assumes that every month is of 30 days.");
    l0=new Label("Enter Date of Birth:");
    ch1=new Choice();
        for(int i=1;i<31;i++)
        {
        String str=String.valueOf(i);
        ch1.add(str);
        }
    ch2=new Choice();
    ch2.add("Jan");
    ch2.add("Feb");
    ch2.add("March");
    ch2.add("April");
    ch2.add("May");
    ch2.add("June");
    ch2.add("July");
    ch2.add("Aug");
    ch2.add("Sept");
    ch2.add("Oct");
    ch2.add("Nov");
    ch2.add("Dec");
    tf1=new TextField(4);
    p2.add(l0);
    p2.add(ch1);
    p2.add(ch2);
    p2.add(tf1);
    b1=new Button("Calculate Age");
    b2=new Button("Reset");
    p3.add(b1);
    p3.add(b2);
    l2=new Label(".......................................................................................................",Label.CENTER);
    p4.add(l2);
    b1.addActionListener(this);
    b2.addActionListener(this);
    }
   
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==b1)
        {
        int ds,ms,ys,dg,mg,yg,dans,mans,yans;
        String str1,str2,str3;
        GregorianCalendar gcalendar=new GregorianCalendar();
        str1=ch1.getSelectedItem();
        str3=tf1.getText();
        dg=gcalendar.get(Calendar.DATE);
        try
        {
        if(str3.length()==0)
        {
        throw new FieldEmptyException();
        }
       
        else
        {
        dg=gcalendar.get(Calendar.DATE);
            if(dg==31)
            {
            dg=30;
            showStatus("Today is thirty first but the program will consider thirtieth.");
            }
        ds=Integer.parseInt(str1);
        ms=ch2.getSelectedIndex()+1;
        ys=Integer.parseInt(str3);
        mg=gcalendar.get(Calendar.MONTH)+1;
        yg=gcalendar.get(Calendar.YEAR);
        System.out.println("Your Date of Birth is:"+ds+"/"+ms+"/"+ys);
        System.out.println("Today's Date is:"+dg+"/"+mg+"/"+yg);
            if(ms>mg)
            {
            yans=((yg-1)-ys);
            mans=(12-(ms-mg));
            dans=(ds-dg);
                if(dans<0)
                {
                dans=(dg-ds);
                }
                    if(ds>dg)
                    {
                    mans--;
                    dans=30-dans;
                    }
            l2.setText("Today, your approx. age is "+yans+" year/s,"+mans+" month/s,"+dans+" day/s.");
            }
            else
            {
            yans=yg-ys;
            mans=mg-ms;
            dans=(ds-dg);
                if(dans<0)
                {
                dans=(dg-ds);
                }
                    if(ds>dg)
                    {
                    mans--;
                    dans=30-dans;
                    }
            l2.setText("Today, your approx. age is "+yans+" year/s,"+mans+" month/s,"+dans+" day/s.");
            }
        }
        }
        catch(FieldEmptyException fee)
        {
        showStatus("Year textfield empty.");
        }
       
        }
        else if(ae.getSource()==b2)
        {
        ch1.select(0);
        ch2.select(0);
        tf1.setText("");
        l2.setText(".......................................................................................................");
        }
    }
   
    public void run()
    {
        try
        {
        while(true)
        {
            for(int i=0;i<20;i++)
            {
            Thread.sleep(250);
            repaint();
            }
        x=0;
        }
        }
        catch(Exception e)
        {
        }
    }
   
    public void paint(Graphics g)
    {
    String strHead=l1.getText();
    g.setColor(Color.RED);
    g.drawString(strHead,x,30);
    x+=10;
    }
}

Snapshots:



No comments:

Post a Comment