/*
     This file can be used for experimenting with Java's
     handling of different number types. The program reads
     in three numbers, then you can do any calculations you
     might want to with them. In particular, try out some of
     the operators given on pages 208-209 of the textbook and
     the casting described on pages 212-213.
*/
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Numbers2 extends Applet implements AdjustmentListener, ActionListener
{
  private TextField intField, double1Field, double2Field;
  private int i = 0;
  private double x = 0, y = 0;

  public void init ()
  {
     Label intLabel = new Label("An int, i:");
     add(intLabel);
     intField = new TextField(12);
     add(intField);
     intField.addActionListener(this);

     Label double1Label = new Label("A double, x:");
     add(double1Label);
     double1Field = new TextField(17);
     add(double1Field);
     double1Field.addActionListener(this);

     Label double2Label = new Label("A double, y:");
     add(double2Label);
     double2Field = new TextField(17);
     add(double2Field);
     double2Field.addActionListener(this);
  }

  public void paint (Graphics g)
  {
     // In this method we can do arbitrary calculations
     // with the numbers i, x, and y and then print out
     // the results.

     g.drawString("i = " + i, 20, 200);
     g.drawString("x = " + x, 20, 210);
     g.drawString("y = " + y, 20, 220);

     g.drawString("x cast to a short is: " + (short)x, 20, 240);

  }

  public void adjustmentValueChanged(AdjustmentEvent e)
  {
      //put code for scrollbars here
      repaint();
  }

  public void actionPerformed(ActionEvent event)
  {
      if (event.getSource() == intField)
          i = Integer.parseInt( intField.getText() );

      if (event.getSource() == double1Field)
          x = Double.valueOf( double1Field.getText() ).doubleValue();

     if (event.getSource() == double2Field)
          y = Double.valueOf( double2Field.getText() ).doubleValue();

      repaint();
  }
}

