/*
    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
 */	

import java.awt.*;
import java.applet.*;

import symantec.itools.awt.util.spinner.NumericSpinner;
import symantec.itools.awt.Label3D;

import symantec.itools.multimedia.NervousText;
public class fishcalc extends Applet
{
    
    protected Image fish;   //picture of trout
    
	public void init()
	{
		symantec.itools.lang.Context.setApplet(this);
	
		//{{INIT_CONTROLS
		setLayout(null);
		setSize(426,266);
		label1 = new java.awt.Label("Fish Length");
		label1.setBounds(12,24,110,29);
		add(label1);
		label2 = new java.awt.Label("Fish Girth");
		label2.setBounds(12,72,110,29);
		add(label2);
		label3 = new java.awt.Label("Fish Type");
		label3.setBounds(12,120,110,29);
		add(label3);
		label4 = new java.awt.Label("Fish Weight");
		label4.setBounds(12,180,120,29);
		add(label4);
		fishlength = new symantec.itools.awt.util.spinner.NumericSpinner();
		try {
			fishlength.setMin(1);
		}
		catch(java.beans.PropertyVetoException e) { }
		try {
			fishlength.setCurrent(12);
		}
		catch(java.beans.PropertyVetoException e) { }
		fishlength.setLayout(null);
		try {
			fishlength.setMax(99);
		}
		catch(java.beans.PropertyVetoException e) { }
		fishlength.setBounds(144,24,64,24);
		add(fishlength);
		fishgirth = new java.awt.TextField();
		fishgirth.setBounds(144,72,65,26);
		add(fishgirth);
		Group1 = new CheckboxGroup();
		basstrout = new java.awt.Checkbox("Bass/Trout", Group1, true);
		basstrout.setBounds(144,120,96,18);
		add(basstrout);
		pikemusky = new java.awt.Checkbox("Pike/Musky", Group1, false);
		pikemusky.setBounds(144,144,96,18);
		add(pikemusky);
		fishweight = new java.awt.Label("");
		fishweight.setBounds(144,180,102,30);
		add(fishweight);
		doit = new java.awt.Button();
		doit.setActionCommand("button");
		doit.setLabel("Calculate");
		doit.setBounds(144,228,92,28);
		doit.setFont(new Font("Dialog", Font.ITALIC, 12));
		doit.setBackground(new Color(12632256));
		add(doit);
		label5 = new java.awt.Label("(in decimal pounds)");
		label5.setBounds(264,180,118,34);
		add(label5);
		whopper = new symantec.itools.multimedia.NervousText();
		try {
			whopper.setText("Whopper!");
		}
		catch(java.beans.PropertyVetoException e) { }
		whopper.setBounds(252,216,168,46);
		whopper.setForeground(new Color(16711680));
		add(whopper);
		//}}
		
	// Load fish image from server
    FishImage();	
    
    //Turn off whopper
    whopper.hide();
		
	
	//{{REGISTER_LISTENERS
	SymAction lSymAction = new SymAction();
	doit.addActionListener(lSymAction);
		
	SymItem lSymItem = new SymItem();
	basstrout.addItemListener(lSymItem);
	pikemusky.addItemListener(lSymItem);
	//}}
	}
	
	//{{DECLARE_CONTROLS
	java.awt.Label label1;
	java.awt.Label label2;
	java.awt.Label label3;
	java.awt.Label label4;
	symantec.itools.awt.util.spinner.NumericSpinner fishlength;
	java.awt.TextField fishgirth;
	java.awt.Checkbox basstrout;
	CheckboxGroup Group1;
	java.awt.Checkbox pikemusky;
	java.awt.Label fishweight;
	java.awt.Button doit;
	java.awt.Label label5;
	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
    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 2.0, Joel Dunn, joel_dunn@unc.edu";
    }
	

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == doit)
				doit_Action(event);
		}
	}

	void doit_Action(java.awt.event.ActionEvent event)
	{
		// initiate calculation
		clickedDoit();	 
	}

	

	class SymItem implements java.awt.event.ItemListener
	{
		public void itemStateChanged(java.awt.event.ItemEvent event)
		{
			Object object = event.getSource();
			if (object == basstrout)
				basstrout_ItemStateChanged(event);
			else if (object == pikemusky)
				pikemusky_ItemStateChanged(event);
		}
	}

	void basstrout_ItemStateChanged(java.awt.event.ItemEvent event)
	{
		// initiate calculation
		clickedDoit();
	}

	void pikemusky_ItemStateChanged(java.awt.event.ItemEvent event)
	{
		// initiate calculation
		clickedDoit();
	}
}
