Showing posts with label java animation using thread. Show all posts
Showing posts with label java animation using thread. Show all posts

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