home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / degrees.java < prev    next >
Text File  |  1998-10-28  |  4KB  |  152 lines

  1. import java.awt.*;
  2.  
  3. public class degrees extends java.applet.Applet {
  4.  
  5.     static boolean FSelected;
  6.  
  7.     public void init() {
  8.  
  9.         //{{INIT_CONTROLS
  10.         setLayout(new GridLayout(2,2,0,0));
  11.         setSize(398,185);
  12.         edit1.setBounds(0,0,199,92);
  13.         add(edit1);
  14.         CheckboxGroup group1 = new CheckboxGroup();
  15.         Fahrenheit.setState(true);
  16.         Fahrenheit.setLabel("Fahrenheit");
  17.         Fahrenheit.setBounds(199,0,199,92);
  18.         add(Fahrenheit);
  19.         clearbutton.setActionCommand("button");
  20.         clearbutton.setLabel("Clear");
  21.         clearbutton.setBounds(0,92,199,92);
  22.         clearbutton.setBackground(java.awt.Color.gray);
  23.         add(clearbutton);
  24.         Celcius.setLabel("Celcius");
  25.         Celcius.setBounds(199,92,199,92);
  26.         add(Celcius);
  27.         //}}
  28.  
  29.         FSelected = true;
  30.         edit1.requestFocus();
  31.  
  32.     
  33.         //{{REGISTER_LISTENERS
  34.         SymAction lSymAction = new SymAction();
  35.         clearbutton.addActionListener(lSymAction);
  36.         SymItem lSymItem = new SymItem();
  37.         Fahrenheit.addItemListener(lSymItem);
  38.         Celcius.addItemListener(lSymItem);
  39.         //}}
  40.  
  41.         validate();
  42.     }
  43.  
  44.     //{{DECLARE_CONTROLS
  45.     java.awt.TextField edit1 = new java.awt.TextField(15);
  46.     java.awt.Checkbox Fahrenheit = new java.awt.Checkbox();
  47.     java.awt.Button clearbutton = new java.awt.Button();
  48.     java.awt.Checkbox Celcius = new java.awt.Checkbox();
  49.     //}}
  50.  
  51.  
  52.  
  53.     class SymAction implements java.awt.event.ActionListener
  54.     {
  55.         public void actionPerformed(java.awt.event.ActionEvent event)
  56.         {
  57.             Object object = event.getSource();
  58.             if (object == clearbutton)
  59.                 clearbutton_ActionPerformed(event);
  60.         }
  61.     }
  62.  
  63.     void clearbutton_ActionPerformed(java.awt.event.ActionEvent event)
  64.     {
  65.         // to do: code goes here.
  66.              
  67.         clearbutton_ActionPerformed_Interaction1(event);
  68.     }
  69.  
  70.  
  71.     void clearbutton_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
  72.     {
  73.         try {
  74.             // edit1 Clear the text for TextField
  75.             edit1.setText("");
  76.         } catch (Exception e) {
  77.         }
  78.     }
  79.  
  80.  
  81.     class SymItem implements java.awt.event.ItemListener
  82.     {
  83.         public void itemStateChanged(java.awt.event.ItemEvent event)
  84.         {
  85.             Object object = event.getSource();
  86.             if (object == Fahrenheit)
  87.                 Fahrenheit_ItemStateChanged(event);
  88.             else if (object == Celcius)
  89.                 Celcius_ItemStateChanged(event);
  90.         }
  91.     }
  92.  
  93.     void Fahrenheit_ItemStateChanged(java.awt.event.ItemEvent event)
  94.     {
  95.         // to do: code goes here.
  96.              
  97.         Fahrenheit_ItemStateChanged_Interaction1(event);
  98.     }
  99.  
  100.  
  101.     void Fahrenheit_ItemStateChanged_Interaction1(java.awt.event.ItemEvent event)
  102.     {
  103.     float degree;
  104.  
  105.         if(FSelected == true) return;
  106.  
  107.         try {
  108.             degree = (Float.valueOf(edit1.getText())).floatValue();
  109.         } catch(NumberFormatException e) {FSelected = true; return; }
  110.  
  111.         if( degree <= 0 )
  112.             edit1.setBackground(Color.cyan);
  113.         else if( degree >= 100 )
  114.                 edit1.setBackground(Color.red);
  115.              else edit1.setBackground(Color.white);
  116.  
  117.         edit1.setText(String.valueOf(1.8*degree+32));
  118.  
  119.         FSelected = true;
  120.     }
  121.  
  122.  
  123.     void Celcius_ItemStateChanged(java.awt.event.ItemEvent event)
  124.     {
  125.         // to do: code goes here.
  126.              
  127.         Celcius_ItemStateChanged_Interaction1(event);
  128.     }
  129.  
  130.  
  131.     void Celcius_ItemStateChanged_Interaction1(java.awt.event.ItemEvent event)
  132.     {
  133.              float degree;
  134.  
  135.         if(FSelected == false) return;
  136.  
  137.         try {
  138.             degree = (Float.valueOf(edit1.getText())).floatValue();
  139.         } catch (NumberFormatException e) {FSelected = false; return; }
  140.  
  141.         if( degree <= 32 )
  142.             edit1.setBackground(Color.cyan);
  143.         else if( degree >= 212 )
  144.                 edit1.setBackground(Color.red);
  145.              else edit1.setBackground(Color.white);
  146.         edit1.setText(String.valueOf((degree-32.0)/1.8));
  147.  
  148.         FSelected = false;
  149.     }
  150.  
  151. }
  152.