home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / degrees.java < prev    next >
Text File  |  1998-02-26  |  4KB  |  147 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(265,61);
  12.         edit1 = new java.awt.TextField(15);
  13.         edit1.setBounds(0,0,265,30);
  14.         add(edit1);
  15.         group1 = new CheckboxGroup();
  16.         Fahrenheit = new java.awt.Checkbox("Fahrenheit", group1, true);
  17.         Fahrenheit.setBounds(132,0,265,30);
  18.         add(Fahrenheit);
  19.         clearbutton = new java.awt.Button();
  20.         clearbutton.setActionCommand("button");
  21.         clearbutton.setLabel("Clear");
  22.         clearbutton.setBounds(0,30,132,30);
  23.         clearbutton.setBackground(new Color(8421504));
  24.         add(clearbutton);
  25.         Celcius = new java.awt.Checkbox("Celcius", group1, false);
  26.         Celcius.setBounds(132,30,132,30);
  27.         add(Celcius);
  28.         //}}
  29.  
  30.         FSelected = true;
  31.         edit1.requestFocus();
  32.  
  33.     
  34.         //{{REGISTER_LISTENERS
  35.         SymItem lSymItem = new SymItem();
  36.         Celcius.addItemListener(lSymItem);
  37.         Fahrenheit.addItemListener(lSymItem);
  38.         SymAction lSymAction = new SymAction();
  39.         clearbutton.addActionListener(lSymAction);
  40.         //}}
  41.         validate();
  42.     }
  43.  
  44.     //{{DECLARE_CONTROLS
  45.     java.awt.TextField edit1;
  46.     java.awt.Checkbox Fahrenheit;
  47.     CheckboxGroup group1;
  48.     java.awt.Button clearbutton;
  49.     java.awt.Checkbox Celcius;
  50.     //}}
  51.  //-------------------------------------------------------------
  52.  // This file has been migrated from the 1.0 to 1.1 event model.
  53.  // This method is not used with the new 1.1 event model. You can 
  54.  // move any code you need to keep, then remove this method.
  55.  //-------------------------------------------------------------
  56.  // 
  57.  // 
  58.  //     public boolean handleEvent(Event event) {
  59.  //         if (event.id == Event.ACTION_EVENT && event.target == Celcius) {
  60.  //                 clickedCelcius();
  61.  //                 return true;
  62.  //         }
  63.  //         else
  64.  //         if (event.id == Event.ACTION_EVENT && event.target == Fahrenheit) {
  65.  //                 clickedFahrenheit();
  66.  //                 return true;
  67.  //         }
  68.  // 
  69.  //         return super.handleEvent(event);
  70.  //     }
  71.  //-------------------------------------------------------------
  72.  
  73.     void clickedFahrenheit(java.awt.event.ItemEvent event) {
  74.  
  75.         float degree;
  76.  
  77.         if(FSelected == true) return;
  78.  
  79.         try {
  80.             degree = (Float.valueOf(edit1.getText())).floatValue();
  81.         } catch(NumberFormatException e) {FSelected = true; return; }
  82.  
  83.         if( degree <= 0 )
  84.             edit1.setBackground(Color.cyan);
  85.         else if( degree >= 100 )
  86.                 edit1.setBackground(Color.red);
  87.              else edit1.setBackground(Color.white);
  88.  
  89.         edit1.setText(String.valueOf(1.8*degree+32));
  90.  
  91.         FSelected = true;
  92.     }
  93.  
  94.     void clickedCelcius(java.awt.event.ItemEvent event) {
  95.  
  96.         float degree;
  97.  
  98.         if(FSelected == false) return;
  99.  
  100.         try {
  101.             degree = (Float.valueOf(edit1.getText())).floatValue();
  102.         } catch (NumberFormatException e) {FSelected = false; return; }
  103.  
  104.         if( degree <= 32 )
  105.             edit1.setBackground(Color.cyan);
  106.         else if( degree >= 212 )
  107.                 edit1.setBackground(Color.red);
  108.              else edit1.setBackground(Color.white);
  109.         edit1.setText(String.valueOf((degree-32.0)/1.8));
  110.  
  111.         FSelected = false;
  112.     }
  113.  
  114.     class SymItem implements java.awt.event.ItemListener
  115.     {
  116.         public void itemStateChanged(java.awt.event.ItemEvent event)
  117.         {
  118.             Object object = event.getSource();
  119.             if (object == Celcius)
  120.                 clickedCelcius(event);
  121.             else if (object == Fahrenheit)
  122.                 clickedFahrenheit(event);
  123.         }
  124.     }
  125.  
  126.     class SymAction implements java.awt.event.ActionListener
  127.     {
  128.         public void actionPerformed(java.awt.event.ActionEvent event)
  129.         {
  130.             Object object = event.getSource();
  131.             if (object == clearbutton)
  132.                 clearbutton_ActionPerformed(event);
  133.         }
  134.     }
  135.  
  136.     void clearbutton_ActionPerformed(java.awt.event.ActionEvent event)
  137.     {
  138.         // to do: code goes here.
  139.              
  140.         //{{CONNECTION
  141.         // Set the text for TextField...
  142.         edit1.setText("");
  143.         edit1.setBackground(Color.white);
  144.         //}}
  145.     }
  146. }
  147.