
/*
    Calculate estimate of weight of a fish, based on the fish weight estimation algorithm
    posted to the Flyfish listserver in July of 1994

    Is this accurate? To paraphrase a Star Trek episode of many years ago, "Dammit Jim,
    I'm a computer scientist, not a biologist". The algorithm used is W=(L * (G^2))/R, where:

        W = Weight in decimal pounds
        L = Length in inches
        G = Girth in inches (default is .58 * L)
        R = 800 for trout/bass, 900 for pike/musky

    Joel Dunn, UNC-Chapel Hill
    joel_dunn@unc.edu
    September 1996

    Tweaked with Visual Cafe 1.x in January 1997
    Converted to JDK 1.1 with Visual Cafe 2.0 in January 1998
    Spotlet version w/JBuilder in September 2000
 */
package fishpalm;

import com.sun.kjava.*;

public class fishPalm extends Spotlet {

  ValueSelector fishLength = new ValueSelector("Fish Length", 1, 100, 12, 5, 35);
  ValueSelector fishGirth = new ValueSelector("Fish Girth", 0, 100, 0, 5, 55);
  RadioGroup fishGroup = new RadioGroup(2);
  RadioButton bassTrout = new RadioButton(10, 89, "Bass/Trout");
  RadioButton pikeMusky = new RadioButton(10, 104, "Pike/Musky");
  Button doIt = new Button("Compute Weight", 5, 145);
  Button quit = new Button("Quit", 90, 145);
  Graphics g = Graphics.getGraphics();

  public fishPalm() {
    initialize();
  }

  public static void main(String[] args) {
    new fishPalm().register(Spotlet.NO_EVENT_OPTIONS);
  }

  public void initialize() {
    g.clearScreen();
    g.drawString("fishPalm v1.0", 5, 5);
    g.drawString("Joel Dunn, rjd@unc.edu", 5, 17);
    g.drawString("Select Fish Type",5,72);
    fishLength.paint();
    fishGirth.paint();
    bassTrout.paint();
    pikeMusky.paint();
    doIt.paint();
    quit.paint();
    showWeight(0, 0);

    //set up buttons
    bassTrout.setState(true);
    fishGroup.add(bassTrout);
    fishGroup.add(pikeMusky);
  }

  /**
   * Handles a pen down event at the given coordinates.</p>
   * x coordinate of pen position
   * y coordinate of pen position
   */
  public void penDown(int x, int y) {

    // pass event to the proper target widget
    if (quit.pressed(x, y)) {
      System.exit(0);
    }
    else if (doIt.pressed(x, y)) {
      calculateWeight();
    }
    else if (bassTrout.pressed(x, y)) {
      bassTrout.handlePenDown(x, y);
      calculateWeight();
    }
    else if (pikeMusky.pressed(x, y)) {
      pikeMusky.handlePenDown(x, y);
      calculateWeight();
    }
    else if (fishLength.pressed(x, y)) {
      super.penDown(x, y);
    }
    else if (fishGirth.pressed(x, y)) {
      super.penDown(x, y);
    }
    else {
      super.penDown(x, y);
    }
  }

    // main algorithm for calculating weight...the rest of this stuff is fluff...
    public void calculateWeight() {

        int length;
        int shapefactor=800;
        int weightW, weightF, weightTemp, girth;

        // get the values of length and girth
        length = fishLength.getValue();
        girth = fishGirth.getValue();

        // must enter a length, will calculate a default girth
        if (girth == 0) {
            girth = length * 58;
            girth /= 100;
            fishGirth.setValue(girth);
            fishGirth.paint();
          }

        // set value for shape factor based on radio button state
        if (bassTrout.isSelected()) shapefactor = 800;
        if (pikeMusky.isSelected()) shapefactor = 900;

        // make calculation
        weightTemp = (length * girth * girth);
        weightW = weightTemp / shapefactor;
        weightF = weightTemp % shapefactor;

        // adjust fraction
        weightF *= 100;
        weightF = weightF / shapefactor;

        // put results on screen for you to see
        showWeight(weightW, weightF);
    }

  // handle dialog dismissed
  public void dialogDismissed(java.lang.String title) {
    register(Spotlet.NO_EVENT_OPTIONS);
    initialize();
  }

  /**
   * Displays fish weight
   * weight fish weight*100, whole number amount (convert to display format)
   */
  public void showWeight(int weightW, int weightF) {

    StringBuffer resultMsg = new StringBuffer("Fish Weight is: ");

    // whole amount
    resultMsg.append(weightW).append('.');

    // fraction amount.  Add a leading zero if less 2 digits.
    if (weightF < 10)
      resultMsg.append('0');
    resultMsg.append(weightF);

    // display
    String resultMsgString = resultMsg.toString();
    g.drawString(resultMsgString,10,119);
  }

}
