/*
    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 in January 1997
 */

import java.awt.*;
import java.applet.*;
import java.awt.image.*;

public class fishcalc extends Applet {

    protected Image fish;   //picture of trout

    public void init() {

        super.init();

        //{{INIT_CONTROLS
		setLayout(null);
		addNotify();
		resize(432,238);
		setForeground(new Color(0));
		label2 = new java.awt.Label("Length of Fish");
		label2.reshape(24,12,114,20);
		add(label2);
		label1 = new java.awt.Label("Girth of Fish");
		label1.reshape(24,60,114,20);
		add(label1);
		label3 = new java.awt.Label("Fish Type");
		label3.reshape(24,96,114,20);
		add(label3);
		group1 = new CheckboxGroup();
		basstrout = new java.awt.Checkbox("Bass/Trout", group1, true);
		basstrout.reshape(144,96,120,24);
		add(basstrout);
		pikemusky = new java.awt.Checkbox("Pike/Musky", group1, false);
		pikemusky.reshape(144,120,126,18);
		add(pikemusky);
		fishgirth = new java.awt.TextField(9);
		fishgirth.setText("0");
		fishgirth.reshape(144,60,82,25);
		add(fishgirth);
		label5 = new java.awt.Label("(in decimal pounds)");
		label5.reshape(240,156,182,23);
		add(label5);
		doit = new java.awt.Button("Calculate");
		doit.reshape(144,192,70,19);
		doit.setFont(new Font("Dialog", Font.BOLD|Font.ITALIC, 10));
		add(doit);
		label4 = new java.awt.Label("Weight of Fish");
		label4.reshape(24,156,114,20);
		add(label4);
		fishweight = new java.awt.Label("n/a");
		fishweight.reshape(144,156,100,24);
		fishweight.setFont(new Font("Helvetica", Font.BOLD, 12));
		fishweight.setForeground(new Color(16711680));
		add(fishweight);
		fishlength = new symantec.itools.awt.NumericSpinner();
		fishlength.reshape(144,12,84,36);
		add(fishlength);
		fishlength.setMin(1);
		fishlength.setMax(99);
		fishlength.setCurrent(12);
		whopper = new symantec.itools.multimedia.NervousText();
		whopper.hide();
		whopper.reshape(264,180,144,48);
		whopper.setFont(new Font("Dialog", Font.BOLD, 8));
		add(whopper);
		whopper.setText("Whopper!");
		//}}

        // Load fish image from server
        FishImage();

    }

    // Drive event handler for radio buttons and "calculate" box
    public boolean handleEvent(Event event) {
        if (event.id == Event.ACTION_EVENT && event.target == pikemusky) {
                clickedPikemusky();
                return true;
        }
        else
        if (event.id == Event.ACTION_EVENT && event.target == basstrout) {
                clickedBasstrout();
                return true;
        }
        else
        if (event.id == Event.ACTION_EVENT && event.target == doit) {
                clickedDoit();
                return true;
        }

        return super.handleEvent(event);
    }

    //{{DECLARE_CONTROLS
	java.awt.Label label2;
	java.awt.Label label1;
	java.awt.Label label3;
	java.awt.Checkbox basstrout;
	CheckboxGroup group1;
	java.awt.Checkbox pikemusky;
	java.awt.TextField fishgirth;
	java.awt.Label label5;
	java.awt.Button doit;
	java.awt.Label label4;
	java.awt.Label fishweight;
	symantec.itools.awt.NumericSpinner fishlength;
	symantec.itools.multimedia.NervousText whopper;
	//}}

    // Drive weight calculation if you click the "calculate" box or a radio button
    public void clickedDoit() {
        CalculateWeight();
    }
    public void clickedBasstrout() {
        CalculateWeight();
    }
    public void clickedPikemusky() {
        CalculateWeight();
    }

    // main algorithm for calculating weight...the rest of this stuff is fluff...
    public void CalculateWeight() {

        int length;
        int shapefactor=800;
        float weight, girth;
        
        //initialize variables
        whopper.hide();
        
        // get the values of length and girth
        length = fishlength.getCurrent();
        try {
        girth = (Float.valueOf(fishgirth.getText()).floatValue());
        }
        catch (NumberFormatException e) {
            girth = 0;
        }

        // must enter a length, will calculate a default girth
        if (girth == 0) {
            girth = length * (float).58;
            fishgirth.setText(String.valueOf(girth));
        }

        // set value for shape factor based on radio button state
        if (basstrout.getState()) shapefactor = 800;
        if (pikemusky.getState()) shapefactor = 900;

        // make calculation
        weight = (length * (girth * girth)) / shapefactor;
        if (weight >= 4) {
            whopper.show();
        }

        // put results on screen for you to see
        fishweight.setText(String.valueOf(weight));

    }

    // load fish image from server
    public void FishImage(){
       fish = this.getImage(this.getDocumentBase(),"rb100.gif");
       }

    // paint the image on the screen @ x=250, y=25
    // set background color
    // the background color stuff doesn't seem to work too well...
    public void paint(Graphics g){
        g.drawImage(fish,250,25,this);
        Color background = getColorParameter("background");
        if (background != null) this.setBackground(background);
    }

    // get color parameter; code lifted from
    // "Java in a Nutshell" page 93
    // Read the parameter and interpret as a
    // hex number of form RRGGBB and convert to color
    protected Color getColorParameter(String name){
        String value = this.getParameter(name);
        int intvalue;
        try {intvalue = Integer.parseInt(value, 16);}
        catch (NumberFormatException e) { return null; }
        return new Color(intvalue);
    }

    // applet info
    public String getAppletInfo(){
        return "Fishcalc V 1.1, Joel Dunn, joel_dunn@unc.edu";
    }
}
