//****************************************************************************
// Odo.java:	Applet
//
//****************************************************************************
import java.applet.*;
import java.awt.*;

//============================================================================
// Main Class for applet Odo
//
//============================================================================
public class Odo extends Applet
{
  final static int SIZE = 4;
  final static int MAX = 16;
  TextField odom[];
  int val;
  Button up, down;

  // Odo Class Constructor
  //--------------------------------------------------------------------------
  public Odo()
  {
    // TODO: Add constructor code here
  }

  // APPLET INFO SUPPORT:
  // The getAppletInfo() method returns a string describing the applet's
  // author, copyright date, or miscellaneous information.
  //--------------------------------------------------------------------------
  public String getAppletInfo()
  {
    return "Name: Odo\r\n" +
      "Author: Richard Salter\r\n" +
      "Created with Microsoft Visual J++ Version 1.0";
  }


  // The init() method is called by the AWT when an applet is first loaded or
  // reloaded.  Override this method to perform whatever initialization your
  // applet needs, such as initializing data structures, loading images or
  // fonts, creating frame windows, setting the layout manager, or adding UI
  // components.
  //--------------------------------------------------------------------------
  public void init()
  {
    // If you use a ResourceWizard-generated "control creator" class to
    // arrange controls in your applet, you may want to call its
    // CreateControls() method from within this method. Remove the following
    // call to resize() before adding the call to CreateControls();
    // CreateControls() does its own resizing.
    //----------------------------------------------------------------------
    resize(320, 65);
    String bgcolor = getParameter("bgcolor");
	Color bgcol;
    if (bgcolor == null) {bgcol = Color.white;}
    else {
      try {
	bgcol = new Color(Integer.parseInt(bgcolor.substring(0,2), 16),
			  Integer.parseInt(bgcolor.substring(2,4), 16),
			  Integer.parseInt(bgcolor.substring(4,6), 16));
      } catch (Exception e) {bgcol = Color.white;}
    } 
    setBackground(bgcol);
    odom = new TextField[SIZE];
    for (int i = 0; i < SIZE; i++) {
      odom[i] = new TextField("0");
      add(odom[i]);
    }
    Panel bpanel = new Panel();
    bpanel.setLayout(new GridLayout(2,1));
    up = new OButton("^", this);
    down = new OButton("v", this);
    bpanel.add(up);
    bpanel.add(down);
    add(bpanel);
    repaint();

    // TODO: Place additional initialization code here
  }

  // Place additional applet clean up code here.  destroy() is called when
  // when you applet is terminating and being unloaded.
  //-------------------------------------------------------------------------
  public void destroy()
  {
    // TODO: Place applet cleanup code here
  }

  // Odo Paint Handler
  //--------------------------------------------------------------------------
  public void paint(Graphics g)
  {

  }

  //	The start() method is called when the page containing the applet
  // first appears on the screen. The AppletWizard's initial implementation
  // of this method starts execution of the applet's thread.
  //--------------------------------------------------------------------------
  public void start()
  {
    // TODO: Place additional applet start code here
  }
	
  //	The stop() method is called when the page containing the applet is
  // no longer on the screen. The AppletWizard's initial implementation of
  // this method stops execution of the applet's thread.
  //--------------------------------------------------------------------------
  public void stop()
  {
  }

  void valtoodo()
  {
    int tmp = val;
    if (tmp < 0) tmp = MAX + tmp;
    for (int i = SIZE-1; i >= 0; i--) {
      odom[i].setText(Integer.toString(tmp % 2));
      tmp /= 2;
    }
  }

  void odotoval()
  {
    for (int i = 0; i < SIZE; i++) {
      val = val * 2 + Integer.parseInt(odom[i].getText());
    }
  }

  // TODO: Place additional applet code here

}

class OButton extends Button
{
  Odo theApplet;

  OButton(String label, Odo theApplet)
  {
    super(label);
    this.theApplet = theApplet;
  }

  public boolean action(Event evt, Object what)
  {
    theApplet.odotoval();
    switch (((String)what).charAt(0)) {
    case '^':
      theApplet.val++; 
      theApplet.val %= theApplet.MAX;
      theApplet.valtoodo();
      repaint();
      break;
    case 'v':
      theApplet.val--;
      if (theApplet.val < 0) theApplet.val = theApplet.MAX + theApplet.val;
      theApplet.val %= theApplet.MAX;
      theApplet.valtoodo();
      repaint();
      break;
    }
    return true;
  }
}
