JavaUser Code: A Color Calculator

User Code: A Color Calculator

Compile the Java code for a color calculator.


//This is a program for a calculator designed in Java by  D. RAMESH BABU
//   E-mail: hollyrame@rediffmail.com

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

public class calculatorcolor extends Applet implements ActionListener 
{
int flag;
int flag1=1;
int i;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,b10,b11,b12,b13,b14,b15,b16;
Label l,l1;
TextField tf;
String msg= " ";
String s1,s2,c;
Random r;
double a,b,d;
Color c1,c2,c3;
Font f,f1,f2;
public void init()
{
try
{
r=new Random();
l=new Label("CALCULATOR");
          l1=new Label("Done by D.Ramesh Babu");
//l.setBackground(Color.black);
setLayout(null);
f=new Font("Times New Roman",Font.BOLD,18);
setFont(f);
setBackground(Color.gray);
setForeground(Color.blue);
tf=new TextField(10);
//b0.setBackground(Color.green);
b0=new Button("0"); //creating instances
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b10=new Button(".");
b11=new Button("+");
b12=new Button("-");
b13=new Button("*");
b14=new Button("/");
b15=new Button("=");
b16=new Button("A/C");
l.setBounds(90,15,200,40);
l1.setBounds(90,350,200,40);
tf.setBounds(80,60,200,40);
b0.setBounds(40,120,80,50);
b1.setBounds(90,120,80,50);
b2.setBounds(140,120,80,50);
b3.setBounds(190,120,80,50);
b4.setBounds(240,120,80,50);
b5.setBounds(40,170,80,50);
b6.setBounds(90,170,80,50);
b7.setBounds(140,170,80,50);
b8.setBounds(190,170,80,50);
b9.setBounds(240,170,80,50);
b11.setBounds(40,220,80,50);
b12.setBounds(90,220,80,50);
b13.setBounds(140,220,80,50);
b14.setBounds(190,220,80,50); //using setbounds for perfect alignment
b10.setBounds(270,220,50,50);
b16.setBounds(40,270,80,50);
b15.setBounds(120,270,200,50);
b1.setEnabled(false);b2.setEnabled(false);b3.setEnabled(false);b4.setEnabled(false);
b5.setEnabled(false);b6.setEnabled(false);b7.setEnabled(false);b8.setEnabled(false);
b9.setEnabled(false);b10.setEnabled(false);b11.setEnabled(false);b12.setEnabled(false);
b13.setEnabled(false);b14.setEnabled(false);b15.setEnabled(false);b0.setEnabled(false);
f1=new Font("Times New Roman",Font.BOLD,25);
l.setFont(f1);
f2=new Font("Times New Roman",Font.ITALIC,14);
l1.setFont(f2);
add(tf);add(l);add(l1);
add(b0);add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);
add(b9);add(b10);add(b11);add(b12);add(b13);add(b14);add(b15);add(b16);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
thread t=new thread();
t.start();
thread1 t1=new thread1();
t1.start();
}catch(Exception e){}
}
                    class thread extends Thread
                    {
                              public void run()
                              {
                      while(true)
                      {
                       try
                       {
                       Color c2=new Color(r.nextFloat(),r.nextFloat(),r.nextFloat()); //to throw Random Colours
                       l.setForeground(c2);
                       sleep(100);
                       }catch(Exception e){}
                       }
                              }
                        }

           class thread1 extends Thread
           {
                 public void run()
                 {
              while(true)
              {
              try{
              Color c3=new Color(r.nextFloat(),r.nextFloat(),r.nextFloat());
              l1.setForeground(c3);
              sleep(100);
              }catch(Exception e){}
                 }
              }
         }
    public void actionPerformed(ActionEvent e)
    {    try{
            if(e.getSource()==b0)
            {
            msg+="0";
            tf.setText(msg);
            }

        if(e.getSource()==b1)
        {
        msg+="1";  //to append 
        tf.setText(msg);
        }

            if(e.getSource()==b2)
            {
            msg+="2";
            tf.setText(msg);
            }

        if(e.getSource()==b3)
        {
        msg+="3";
        tf.setText(msg);
        }

            if(e.getSource()==b4)
            {
            msg+="4";
            tf.setText(msg);
            }

        if(e.getSource()==b5)
        {
        msg+="5";
        tf.setText(msg);
        }
        
            if(e.getSource()==b6)
            {
            msg+="6";
            tf.setText(msg);
            }
            
        if(e.getSource()==b7)
        {
        msg+="7";
        tf.setText(msg);
        }

            if(e.getSource()==b8)
            {
            msg+="8";
            tf.setText(msg);
            }

        if(e.getSource()==b9)
        {
        msg+="9";
        tf.setText(msg);
        }
        
            if(e.getSource()==b10)
            {
            msg+=".";
            tf.setText(msg);
            }

            if(e.getSource()==b11)
            {                              
                flag=1;
                s1=tf.getText();
            
               // tf.setText(" ");
                msg=" ";

             }
                   
                               
        if(e.getSource()==b12)
        {
        flag=2;
        s1=tf.getText();
        
      //  tf.setText(" ");
        msg=" ";
        }

            if(e.getSource()==b13)
            {
            flag=3;
            s1=tf.getText();
        
        //    tf.setText(" ");
            msg=" ";
            }

        if(e.getSource()==b14)
        {
        flag=4;
        s1=tf.getText();
       
       // tf.setText(" ");
        msg=" ";
        }

               if(e.getSource()==b16)
               {
                  if(flag==1)
                  {
b0.setEnabled(true);b1.setEnabled(true);b2.setEnabled(true);b3.setEnabled(true);
b4.setEnabled(true);b5.setEnabled(true);b6.setEnabled(true);b7.setEnabled(true);
b8.setEnabled(true);b9.setEnabled(true);b10.setEnabled(true);b11.setEnabled(true);
b12.setEnabled(true);b13.setEnabled(true);b14.setEnabled(true);b15.setEnabled(true);
               tf.setText(" ");
               msg=" ";
               tf.setText("0");
               flag=2;
                   }
                   else
                   {
tf.setEnabled(false);
b1.setEnabled(false);b2.setEnabled(false);b3.setEnabled(false);b4.setEnabled(false);
b5.setEnabled(false);b6.setEnabled(false);b7.setEnabled(false);b8.setEnabled(false);
b9.setEnabled(false);b10.setEnabled(false);b11.setEnabled(false);b12.setEnabled(false);
b13.setEnabled(false);b14.setEnabled(false);b15.setEnabled(false);b0.setEnabled(false);
                   flag=1;
                   }
             }


        if(e.getSource()==b15)
        {
        try
        {
                if(flag==1)
                {
                        String s2=tf.getText();
                        if(s2.equals(" "))
                        {
       
                        a=Double.parseDouble(s1.trim());//trim for omitting white spaces
                        a=a*2;   //and converting string to double for calculation
                        tf.setText(" "+a);
                        }
                else
                {
                s2=tf.getText();
       
                tf.setText(" ");
                a=Double.parseDouble(s1.trim());
       
                b=Double.parseDouble(s2.trim());
       
                d=a+b;
       
                tf.setText(""+d);
                }       

                }
                if(flag==2)
                {
                String s3=tf.getText();
                        if(s3.equals(" "))
                        {
                   
                        a=Double.parseDouble(s1.trim());
                        double w=a;
                        double v=w-a;
                        w=v;
                        tf.setText(" "+v);
                        }
                        else
                        {

                s2=tf.getText();
                
                tf.setText(" ");
                a=Double.parseDouble(s1.trim());
                System.out.println("The value of a is"+a);
                b=Double.parseDouble(s2.trim());
                
                d=a-b;
                
                tf.setText(""+d);
                }
                }
                if(flag==3)
                {
                        String s4=tf.getText();
                        if(s4.equals(" "))
                        {
                
                        a=Double.parseDouble(s1.trim());
                        double j=a,b=a;
                        double k=j*b;
                        j=k;
                        tf.setText(" "+k);
                        }
                                         
                       else
                       {
                s2=tf.getText();
                
                tf.setText(" ");
                a=Double.parseDouble(s1.trim());
                
                b=Double.parseDouble(s2.trim());
                
                d=a*b;
                
                tf.setText(""+d);
                }
                }

                if(flag==4)
                {
                  String s5=tf.getText();
                        if(s5.equals(" "))
                        {
                        a=Double.parseDouble(s1.trim());
                        double q=a;
                        double n=q/a;
                        q=n;
                        tf.setText(""+q);
                        }
                        else
                        {
                        s2=tf.getText();
                
                        tf.setText(" ");
                        a=Double.parseDouble(s1.trim());
                
                        b=Double.parseDouble(s2.trim());
                        
                        d=a/b;
                        
                        tf.setText(""+d);
                        }
               }
            
        }catch(Exception z){}
     }
 }catch(Exception se){}
repaint();      
}
}

Downloads


calculatorcolor.java



Download source = 10.9 K

About the Author

Ramesh Babu has a B.Sc. in mathematics from Loyola College in Chennai, India, and worked there as a faculty member for 10 months. He is presently in search of programming work in the private sector.

[To contribute a code article to Gamelan, please contact kmurphy@internet.com.]

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories