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:



10 Oct 2014

Program demonstrating Java Applet

This program will help you to understand:
  • Using various Layouts in Applet
  • Use of Threads to do simple 2D animations
  • User defined Exception Handling (Exception generated when user clicks the button and the required text fields are kept empty)

Code:

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

/*
<applet code="BizzApp1.class" height=250 width=500>
</applet>
*/

class FieldZeroException extends Exception
{
String strE2;
    FieldZeroException()
    {
    }
    FieldZeroException(String strE1)
    {
    strE2=strE1;
    }
   
    public String toString()
    {
    return strE2+" TextField/s empty.";
    }
}

public class BizzApp1 extends Applet implements Runnable,ActionListener,ItemListener
{
Panel p2,pn,ps;
Choice ch;
Label l1,l2,l3,l4,l5,l6,fl;
Button b1,b2,b3,b4,b5;
TextField tf1,tf2,tf3,tf4;
Frame f;
int x=-190,a=0,b=0,d=0;
double pm=0.15,tm=0.1,wm=0.05;
String str1,str2,str3,str4,str5,str6;
double sal,fsal;
    public void init()
    {
    Thread t=new Thread(this);
    t.start();
    setLayout(new BorderLayout());
    Color c1=new Color(253,210,154);
    setBackground(c1);
    Color c2=new Color(81,44,30);
    setForeground(c2);
    f=new Frame("BizzApp Version");
    f.setSize(250,100);
    Color cf1=new Color(225,186,143);
    Color cf2=new Color(220,96,22);
    f.setBackground(cf1);
    f.setForeground(cf2);
    fl=new Label("BizzApp v1.1");
    Font flabel=new Font("Times New Roman",Font.BOLD,30);
    fl.setFont(flabel);
    f.setLayout(new FlowLayout(FlowLayout.CENTER));
    f.add(fl);
    p2=new Panel(new BorderLayout());
    pn=new Panel(new GridLayout(5,2,0,10));
    ps=new Panel(new FlowLayout(FlowLayout.CENTER));
    ch=new Choice();
    l1=new Label("BizzApp");
    l2=new Label("ID:");
    l3=new Label("Name:");
    l4=new Label("Designation:");
    l5=new Label("Salary:");
    l6=new Label("Calculated Salary:");
    b1=new Button("Calculate");
    b2=new Button("Save");
    b3=new Button("Reset");
    b4=new Button("View");
    b5=new Button("About");
    tf1=new TextField(20);
    tf2=new TextField(20);
    tf3=new TextField(20);
    tf4=new TextField(20);
    tf4.setText("--------auto--------");
    add("South",p2);
    p2.add("North",pn);
    p2.add("South",ps);
    pn.add(l2);
    pn.add(tf1);
    pn.add(l3);
    pn.add(tf2);
    pn.add(l4);
    pn.add(ch);
    ch.add("Project Manager");
    ch.add("Team Member");
    ch.add("Work Man");
    pn.add(l5);
    pn.add(tf3);
    pn.add(l6);
    pn.add(tf4);
    ps.add(b1);
    ps.add(b2);
    ps.add(b3);
    ps.add(b4);
    ps.add(b5);
        try
        {
        File file1=new File("BizzApp.xls");
            if(!file1.exists())
            {
            file1.createNewFile();
            FileWriter fw1=new FileWriter("BizzApp.xls",true);
            String header="ID\tName\tDesignation\tSalary\tBonus(%)\tCalculated Salary\n";
            fw1.write(header);
            fw1.close();
            }
        }
        catch(Exception e)
        {
        }
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    ch.addItemListener(this);
    f.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent we)
        {
        f.dispose();
        }
    });
    }
   
    public void itemStateChanged(ItemEvent ie)
    {
    }
   
    public void run()
    {
    while(true)
    {
    for(int i=0;i<70;i++)
    {
        try
        {
        repaint();
        Thread.sleep(250);
        }
        catch(Exception e)
        {
        }
    }
    x=-190;
    }
    }
    public double getBonus()
    {
    Double bonus=0.0;
            if(str3=="Project Manager")
                bonus=(pm*100);
            else if(str3=="Team Member")
                bonus=(tm*100);
            else if(str3=="Work Man")
                bonus=(wm*100);
    return bonus;
    }
   
   
    public void actionPerformed(ActionEvent ae)
    {
       
        if(ae.getSource()==b1)
        {
        str3=ch.getSelectedItem();
        str4=tf3.getText();
        d=str4.length();
            try
            {
                if(d==0)
                {
                String salEmpty="Salary";
                throw new FieldZeroException(salEmpty);
                }
                else
                {
                sal=Double.parseDouble(str4);
                    if(str3=="Project Manager")
                    {
                    fsal=sal+(sal*pm);
                    }
                    else if(str3=="Team Member")
                    {
                    fsal=sal+(sal*tm);
                    }
                    else if(str3=="Work Man")
                    {
                    fsal=sal+(sal*wm);
                    }
                String ans=String.valueOf(fsal);
                tf4.setText(ans);
                showStatus("Final Salary calculated with bonus of "+Math.round(((fsal-sal)/sal)*100)+"%.");
                }
            }
            catch(FieldZeroException fze1)
            {
            showStatus(fze1.toString());
            }
        }
       
        else if(ae.getSource()==b2)
        {
        str1=tf1.getText();
        str2=tf2.getText();
        str3=ch.getSelectedItem();
        str4=tf3.getText();
        str5=String.valueOf(this.getBonus());
        str6=tf4.getText();
        a=str1.length();
        b=str2.length();
        d=str4.length();
            try
            {
                if(a==0 && b==0 && d==0)
                {
                String all="All";
                throw new FieldZeroException(all);
                }
                else if(a==0 || b==0 || d==0)
                {
                String any="Any";
                throw new FieldZeroException(any);
                }
                else
                {
                    try
                    {
                    FileWriter fw2=new FileWriter("BizzApp.xls",true);
                    String row=str1+"\t"+str2+"\t"+str3+"\t"+str4+"\t"+str5+"\t"+str6+"\n";
                    fw2.write(row);
                    fw2.close();
                    showStatus("Saved Successfully.");
                    }
                    catch(Exception e)
                    {
                    }
                }
            }
            catch(FieldZeroException fze2)
            {
            showStatus(fze2.toString());
            }
       
        }
       
        else if(ae.getSource()==b3)
        {
        tf1.setText("");
        tf2.setText("");
        tf3.setText("");
        tf4.setText("--------auto--------");
        }
       
        else if(ae.getSource()==b4)
        {
            try
            {
            Runtime.getRuntime().exec("cmd /c start BizzApp.xls");
            }
            catch(Exception e)
            {
            }
        }
       
        else if(ae.getSource()==b5)
        {
        f.setVisible(true);
        }
    }
   
    public void paint(Graphics g)
    {
    String strHead=l1.getText();
    Font f=new Font("Courier",Font.BOLD+Font.ITALIC,45);
    g.setFont(f);
    Color cg=new Color(91,74,38);
    g.setColor(cg);
    g.drawString(strHead,x,40);
    x+=10;
    }
}

7 Oct 2014

Resume Builder [Java Applet]

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

/*
<applet code="ResumeBuilder.class" height=600 width=500>
</applet>
*/

public class ResumeBuilder extends Applet implements ActionListener
{
Button b1,b2;
Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15;
TextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
TextArea ta1,ta2,ta3,ta4;
Panel p1,p2;
String str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14;
    public void init()
    {
    Color bg=new Color(242,189,125);
    Color fg=new Color(162,65,44);
    Color tt=new Color(255,82,0);
    setBackground(bg);
    setForeground(fg);
    Font f1=new Font("Courier",Font.BOLD,30);
    p1=new Panel(new GridLayout(14,2,15,15));
    p2=new Panel(new FlowLayout(FlowLayout.CENTER));
    setLayout(new BorderLayout());
    l1=new Label("Resume Builder",Label.CENTER);
    l1.setFont(f1);
    l1.setForeground(tt);
    l2=new Label("Name:");
    l3=new Label("Address:");
    l4=new Label("Telephone No.");
    l5=new Label("E-mail:");
    l6=new Label("Date of Birth:");
    l7=new Label("HSC passing year:");
    l8=new Label("HSC Board:");
    l9=new Label("HSC result:");
    l10=new Label("College passing year:");
    l11=new Label("University name:");
    l12=new Label("CGPA:");
    l13=new Label("Achievements:");
    l14=new Label("Area of Interest:");
    l15=new Label("Skill & Hobby:");
    b1=new Button("Create");
    b2=new Button("View");
    tf1=new TextField(20);
    tf2=new TextField(20);
    tf3=new TextField(20);
    tf4=new TextField(20);
    tf5=new TextField(20);
    tf6=new TextField(20);
    tf7=new TextField(20);
    tf8=new TextField(20);
    tf9=new TextField(20);
    tf10=new TextField(20);
    ta1=new TextArea(1,20);
    ta2=new TextArea(1,20);
    ta3=new TextArea(1,20);
    ta4=new TextArea(1,20);
    add("North",l1);
    add("Center",p1);
    add("South",p2);
    p1.add(l2);
    p1.add(tf1);
    p1.add(l3);
    p1.add(ta1);
    p1.add(l4);
    p1.add(tf2);
    p1.add(l5);
    p1.add(tf3);
    p1.add(l6);
    p1.add(tf4);
    p1.add(l7);
    p1.add(tf5);
    p1.add(l8);
    p1.add(tf6);
    p1.add(l9);
    p1.add(tf7);
    p1.add(l10);
    p1.add(tf8);
    p1.add(l11);
    p1.add(tf9);
    p1.add(l12);
    p1.add(tf10);
    p1.add(l13);
    p1.add(ta2);
    p1.add(l14);
    p1.add(ta3);
    p1.add(l15);
    p1.add(ta4);
    p2.add(b1);
    p2.add(b2);
    b1.addActionListener(this);
    b2.addActionListener(this);
    }
   
    public void transfer()
    {
    str1=tf1.getText();
    str2=ta1.getText();
    str3=tf2.getText();
    str4=tf3.getText();
    str5=tf4.getText();
    str6=tf5.getText();
    str7=tf6.getText();
    str8=tf7.getText();
    str9=tf8.getText();
    str10=tf9.getText();
    str11=tf10.getText();
    str12=ta2.getText();
    str13=ta3.getText();
    str14=ta4.getText();
    }
   
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==b1)
        {
        this.transfer();
            try
            {
            File file=new File("Resume.doc");
                if(file.exists()==true)
                {
                file.delete();
                }
            file.createNewFile();   
            FileWriter fw1=new FileWriter("Resume.doc",true);
            fw1.write("Name:"+str1+"\n\n");
            fw1.write("Address:"+str2+"\n\n");
            fw1.write("Telephone No.:"+str3+"\n\n");
            fw1.write("E-mail:"+str4+"\n\n");
            fw1.write("Date of Birth:"+str5+"\n\n");
            fw1.write("HSC passing year:"+str6+"\n\n");
            fw1.write("HSC Board:"+str7+"\n\n");
            fw1.write("HSC result:"+str8+"\n\n");
            fw1.write("College passing year:"+str9+"\n\n");
            fw1.write("University name:"+str10+"\n\n");
            fw1.write("CGPA:"+str11+"\n\n");
            fw1.write("Achievements:"+str12+"\n\n");
            fw1.write("Area of Interest:"+str13+"\n\n");
            fw1.write("Skill & Hobby:"+str14);
            fw1.close();
            }
            catch(Exception e)
            {
            }
        }
       
        else if(ae.getSource()==b2)
        {
            try
            {
            Process p=Runtime.getRuntime().exec("cmd /c start Resume.doc");
            }
            catch(Exception e)
            {
            }
        }
    }
}

Snapshots:


Demonstrating opening of an Applet from an Applet on button click

Code for CmdDemoWithApplet.class file:

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

/*
<applet code="CmdDemoWithApplet.class" height=200 width=400>
</applet>
*/

public class CmdDemoWithApplet extends Applet implements ActionListener
{
Button b1,b2,b3;
    public void init()
    {
    setLayout(new GridLayout(3,1,35,35));
    Color c=new Color(187,224,148);
    Color c1=new Color(98,167,15);
    Color c2=new Color(225,0,0);
    setBackground(c);
    setForeground(c2);
    b1=new Button("Open Applet1");
    b2=new Button("Open Applet2");
    b3=new Button("Open Applet3");
    add(b1);
    b1.setBackground(c1);
    add(b2);
    b2.setBackground(c1);
    add(b3);
    b3.setBackground(c1);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    }
   
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==b1)
        {
            try
            {
            Process p1=Runtime.getRuntime().exec("appletviewer Applet1.java");
            }
            catch(Exception e)
            {
            }
        }
        else if(ae.getSource()==b2)
        {
            try
            {
            Process p2=Runtime.getRuntime().exec("appletviewer Applet2.java");
            }
            catch(Exception e)
            {
            }
        }
        else if(ae.getSource()==b3)
        {
            try
            {
            Process p3=Runtime.getRuntime().exec("appletviewer Applet3.java");
            }
            catch(Exception e)
            {
            }
        }
    }
}

Code for Applet1.class file:


import java.applet.*;
import java.awt.*;

/*
<applet code="Applet1.class" height=50 width=475>
</applet>
*/

public class Applet1 extends Applet
{
Label l1;
    public void init()
    {
    setLayout(new BorderLayout());
    Color c1=new Color(255,191,151);
    Color c2=new Color(230,84,0);
    setBackground(c1);
    setForeground(c2);
    Font f=new Font("Courier",Font.BOLD,50);
    l1=new Label("This is Applet1.");
    l1.setFont(f);
    add("Center",l1);
    }
}

Code for Applet2.class file:

import java.applet.*;
import java.awt.*;

/*
<applet code="Applet2.class" height=50 width=475>
</applet>
*/

public class Applet2 extends Applet
{
Label l1;
    public void init()
    {
    setLayout(new BorderLayout());
    Color c1=new Color(255,191,151);
    Color c2=new Color(230,84,0);
    setBackground(c1);
    setForeground(c2);
    Font f=new Font("Courier",Font.BOLD,50);
    l1=new Label("This is Applet2.");
    l1.setFont(f);
    add("Center",l1);
    }
}

Code for Applet3.class file:

import java.applet.*;
import java.awt.*;

/*
<applet code="Applet3.class" height=50 width=475>
</applet>
*/

public class Applet3 extends Applet
{
Label l1;
    public void init()
    {
    setLayout(new BorderLayout());
    Color c1=new Color(255,191,151);
    Color c2=new Color(230,84,0);
    setBackground(c1);
    setForeground(c2);
    Font f=new Font("Courier",Font.BOLD,50);
    l1=new Label("This is Applet3.");
    l1.setFont(f);
    add("Center",l1);
    }
}


The snapshot shows you the sequence of executing the commands in Command Prompt and shows the applets.

6 Oct 2014

Activating features of .NET Framework 3.5 in Windows 8 offline

Whenever you try to install any software which requires .NET Framework 3.5 features for installation purpose, it will prompt you to install first the features. You would be aware that .NET Framework 3.5 comprises the features of .NET Framework 2.0 and 3.0.

You need not to download setup to install .NET Framework 3.5 features. You need to simply have Windows 8 installer CD and should know two commands.

Firstly, insert the CD and type the following command in the command prompt.

xcopy e:\sources\sxs\*.* c:\dotnet35 /s 
 
This command will copy the files from CD to your entered destination (here, c:\dotnet35).

Secondly, type given below command with administrator privileges.

Dism.exe /online /enable-feature /featurename:NetFX3 /All /Source:c:\dotnet35 /LimitAccess
 
The above snapshot demonstrates how the files are copied from CD to your system by executing the first command and how the features are enabled by executing second command with administrator privileges.

To know more follow the given link:
http://msdn.microsoft.com/en-us/library/windows/desktop/hh848079%28v=vs.85%29.aspx