/*
To do Rhodes' modeling - 7/98 EFG
Interface tweaking - 10/98 EFG
Modified - 01/99 DR
*/

import java.awt.*;
import java.applet.Applet;

public class model extends Applet
{
TextField  textr1, textr2, textr3, textr4;
TextField  textr5, textr6, textr7, textr8, textr9;
TextField  textr10, textr11, textr12, textr13, textr14, textr15; 
TextField  textTS, textTC, textPI, textCI, textOpt;
TextField  textChlGln, textCytGln, textVacGln, textProtGln;
TextField  textChlGlu, textCytGlu, textVacGlu, textProtGlu;
TextField  textCytPro, textVacPro, textProtPro;
TextField  textCytGABA;
TextField  textMy1;
TextField  textMy2;
Label      endXAxis1, endXAxis2;
Label      endYAxis1, endYAxis2;
Button     bDoGraph;
Graph		isotopeGraph, poolGraph;
static final int GraphSize = 200;

public void init() {

// Arrange the user interface using a gridbag.
GridBagConstraints gbc;
GridBagLayout  gb;
gb = new GridBagLayout();
setLayout(gb); // set the layout for the whole applet

// add the data entry fields
Label labelr1 = new Label("r1:");
textr1 = new TextField("5.4", 6);
Label labelr2 = new Label("r2:");
textr2 = new TextField("0.6", 6);
Label labelr3 = new Label("r3:");
textr3 = new TextField("0.3", 6);
Label labelr4 = new Label("r4:");
textr4 = new TextField("0.2", 6);
Label labelr5 = new Label("r5:");
textr5 = new TextField("0.1", 6);
Label labelr6 = new Label("r6:");
textr6 = new TextField("0.1", 6);
Label labelr7 = new Label("r7:");
textr7 = new TextField("0.05", 6);
Label labelr8 = new Label("r8:");
textr8 = new TextField("4.15", 6);
Label labelr9 = new Label("r9:");
textr9 = new TextField("0.1", 6);
Label labelr10 = new Label("r10:");
textr10 = new TextField("0.3", 6);
Label labelr11 = new Label("r11:");
textr11 = new TextField("0.1", 6);
Label labelr12 = new Label("r12:");
textr12 = new TextField("0.1", 6);
Label labelr13 = new Label("r13:");
textr13 = new TextField("0.6", 6);
Label labelr14 = new Label("r14:");
textr14 = new TextField("0.3", 6);
Label labelr15 = new Label("r15:");
textr15 = new TextField("0.3", 6);
Label labelChlGln = new Label("ChlGln:");
textChlGln = new TextField("3.5", 6);
Label labelCytGln = new Label("CytGln:");
textCytGln = new TextField("2.5", 6);
Label labelVacGln = new Label("VacGln:");
textVacGln = new TextField("3.5", 6);
Label labelProtGln = new Label("ProtGln:");
textProtGln = new TextField("2.0", 6);
Label labelChlGlu = new Label("ChlGlu:");
textChlGlu = new TextField("0.8", 6);
Label labelCytGlu = new Label("CytGlu:");
textCytGlu = new TextField("1.0", 6);
Label labelVacGlu = new Label("VacGlu:");
textVacGlu = new TextField("1.0", 6);
Label labelProtGlu = new Label("ProtGlu:");
textProtGlu = new TextField("1.5", 6);
Label labelCytPro = new Label("CytPro:");
textCytPro = new TextField("3.0", 6);
Label labelVacPro = new Label("VacPro:");
textVacPro = new TextField("1.0", 6);
Label labelProtPro = new Label("ProtPro:");
textProtPro = new TextField("1.0", 6);
Label labelCytGABA = new Label("CytGABA:");
textCytGABA = new TextField("1.4", 6);
Label labelTS = new Label("TS:");
textTS = new TextField("10.0", 6);
Label labelTC = new Label("TC:");
textTC = new TextField("5.0", 6);
Label labelML = new Label("ML:");
textMy1 = new TextField("80.0", 6);
Label labelMR = new Label("MR:");
textMy2 = new TextField("12.0", 6);
Label labelPI = new Label("PI:");
textPI = new TextField("95", 6);
Label labelCI = new Label("CI:");
textCI = new TextField("0", 6);
Label labelOpt = new Label("Opt:");
textOpt = new TextField("1", 6);
Label gLabel1 = new Label("Isotope Abundance");
Label xLabel1 = new Label("Time (h)");
Label yLabel1 = new Label("%15N");
Label startXAxis1 = new Label ("0");
Label startYAxis1 = new Label ("0");
endXAxis1 = new Label (textTS.getText());
endYAxis1 = new Label (textMy1.getText());
isotopeGraph = new Graph(GraphSize, GraphSize);
isotopeGraph.graphImage = blankGraph(GraphSize, GraphSize);
bDoGraph = new Button ("Start");
Label gLabel2 = new Label("Pool Size");
Label xLabel2 = new Label("Time (h)");
Label yLabel2 = new Label("umol.gfw(-1)");
Label startXAxis2 = new Label ("0");
Label startYAxis2 = new Label ("0");
endXAxis2 = new Label (textTS.getText());
endYAxis2 = new Label (textMy2.getText());
poolGraph = new Graph(GraphSize, GraphSize);
poolGraph.graphImage = blankGraph(GraphSize, GraphSize);

gbc = new GridBagConstraints();
gbc.gridx = gbc.gridy = 0;
gb.setConstraints(labelr1,  gbc);
add(labelr1);
gbc.gridy = 1;
gb.setConstraints(labelr2,  gbc);
add(labelr2);
gbc.gridy = 2;
gb.setConstraints(labelr3,  gbc);
add(labelr3);
gbc.gridy = 3;
gb.setConstraints(labelr4,  gbc);
add(labelr4);
gbc.gridy = 4;
gb.setConstraints(labelr5,  gbc);
add(labelr5);
gbc.gridy = 5;
gb.setConstraints(labelr6,  gbc);
add(labelr6);
gbc.gridy = 6;
gb.setConstraints(labelr7,  gbc);
add(labelr7);

gbc.gridy = 8;
gbc.anchor = GridBagConstraints.NORTHEAST;
gb.setConstraints(endYAxis1,  gbc);
add(endYAxis1);
gbc.gridy = 10;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridheight = 3;
gb.setConstraints(yLabel1,  gbc);
add(yLabel1);
gbc.gridy = 12;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gb.setConstraints(startYAxis1,  gbc);
add(startYAxis1);

gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gb.setConstraints(textr1,  gbc);
add(textr1);
gbc.gridy = 1;
gb.setConstraints(textr2,  gbc);
add(textr2);
gbc.gridy = 2;
gb.setConstraints(textr3,  gbc);
add(textr3);
gbc.gridy = 3;
gb.setConstraints(textr4,  gbc);
add(textr4);
gbc.gridy = 4;
gb.setConstraints(textr5,  gbc);
add(textr5);
gbc.gridy = 5;
gb.setConstraints(textr6,  gbc);
add(textr6);
gbc.gridy = 6;
gb.setConstraints(textr7,  gbc);
add(textr7);

gbc.gridy = 7;
gbc.gridwidth = 5;
gb.setConstraints(gLabel1,  gbc);
add(gLabel1);
gbc.gridy = 8;
gbc.gridheight = 5;
gb.setConstraints(isotopeGraph,  gbc);
add(isotopeGraph);
gbc.gridy = 13;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.NORTHWEST;
gb.setConstraints(startXAxis1,  gbc);
add(startXAxis1);

gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 0;
gb.setConstraints(labelr8,  gbc);
add(labelr8);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelr9,  gbc);
add(labelr9);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelr10,  gbc);
add(labelr10);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelr11,  gbc);
add(labelr11);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelr12,  gbc);
add(labelr12);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelr13,  gbc);
add(labelr13);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelr14,  gbc);
add(labelr14);

gbc.gridy = 13;
gbc.gridwidth = 3;
gb.setConstraints(xLabel1,  gbc);
add(xLabel1);

gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 0;
gb.setConstraints(textr8,  gbc);
add(textr8);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textr9,  gbc);
add(textr9);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textr10,  gbc);
add(textr10);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textr11,  gbc);
add(textr11);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textr12,  gbc);
add(textr12);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textr13,  gbc);
add(textr13);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textr14,  gbc);
add(textr14);

gbc = new GridBagConstraints();
gbc.gridx = 4;
gbc.gridy = 0;
gb.setConstraints(labelr15,  gbc);
add(labelr15);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelTS,  gbc);
add(labelTS);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelTC,  gbc);
add(labelTC);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelML,  gbc);
add(labelML);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelMR,  gbc);
add(labelMR);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelPI,  gbc);
add(labelPI);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelCI,  gbc);
add(labelCI);

gbc = new GridBagConstraints();
gbc.gridx = 5;
gbc.gridy = 0;
gb.setConstraints(textr15,  gbc);
add(textr15);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textTS,  gbc);
add(textTS);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textTC,  gbc);
add(textTC);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textMy1,  gbc);
add(textMy1);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textMy2,  gbc);
add(textMy2);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textPI,  gbc);
add(textPI);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textCI,  gbc);
add(textCI);

gbc.gridy = 13;
gbc.anchor = GridBagConstraints.NORTHEAST;
gb.setConstraints(endXAxis1,  gbc);
add(endXAxis1);

gbc = new GridBagConstraints();
gbc.gridx = 6;
gbc.gridy = 0;
gb.setConstraints(labelChlGln,  gbc);
add(labelChlGln);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelCytGln,  gbc);
add(labelCytGln);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelVacGln,  gbc);
add(labelVacGln);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelProtGln,  gbc);
add(labelProtGln);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelChlGlu,  gbc);
add(labelChlGlu);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelCytGlu,  gbc);
add(labelCytGlu);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelOpt,  gbc);
add(labelOpt);

gbc = new GridBagConstraints();
gbc.gridx = 7;
gbc.gridy = 0;
gb.setConstraints(textChlGln,  gbc);
add(textChlGln);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textCytGln,  gbc);
add(textCytGln);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textVacGln,  gbc);
add(textVacGln);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textProtGln,  gbc);
add(textProtGln);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textChlGlu,  gbc);
add(textChlGlu);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textCytGlu,  gbc);
add(textCytGlu);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textOpt,  gbc);
add(textOpt);

gbc = new GridBagConstraints();
gbc.gridx = 8;
gbc.gridy = 0;
gb.setConstraints(labelVacGlu,  gbc);
add(labelVacGlu);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelProtGlu,  gbc);
add(labelProtGlu);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelCytPro,  gbc);
add(labelCytPro);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelVacPro,  gbc);
add(labelVacPro);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelProtPro,  gbc);
add(labelProtPro);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelCytGABA,  gbc);
add(labelCytGABA);

gbc.gridy = 8;
gbc.anchor = GridBagConstraints.NORTHEAST;
gb.setConstraints(endYAxis2,  gbc);
add(endYAxis2);
gbc.gridy = 10;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridheight = 3;
gb.setConstraints(yLabel2,  gbc);
add(yLabel2);
gbc.gridheight = 1;
gbc.gridy = 12;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gb.setConstraints(startYAxis2,  gbc);
add(startYAxis2);

gbc = new GridBagConstraints();
gbc.gridx = 9;
gbc.gridy = 0;
gb.setConstraints(textVacGlu,  gbc);
add(textVacGlu);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textProtGlu,  gbc);
add(textProtGlu);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textCytPro,  gbc);
add(textCytPro);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textVacPro,  gbc);
add(textVacPro);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textProtPro,  gbc);
add(textProtPro);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textCytGABA,  gbc);
add(textCytGABA);

gbc.gridy = 7;
gbc.gridwidth = 5;
gb.setConstraints(gLabel2,  gbc);
add(gLabel2);
gbc.gridy = 8;
gbc.gridheight = 5;
gb.setConstraints(poolGraph,  gbc);
add(poolGraph);
gbc.gridy = 13;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.NORTHWEST;
gb.setConstraints(startXAxis2,  gbc);
add(startXAxis2);

gbc = new GridBagConstraints();
gbc.gridx = 10;
gbc.gridy = 13;
gbc.gridwidth = 3;
gb.setConstraints(xLabel2,  gbc);
add(xLabel2);

gbc.gridx = 6;
gbc.gridy = 11;
gbc.gridwidth = 1;
gb.setConstraints(bDoGraph,  gbc);
add(bDoGraph);

gbc.gridx = 13;
gbc.gridy = 13;
gbc.anchor = GridBagConstraints.NORTHEAST;
gb.setConstraints(endXAxis2,  gbc);
add(endXAxis2);


}

/*
public void update (Graphics g) { paint(g); }

public void paint (Graphics g) {
g.drawImage (graphImage, 0, 0, this);
}

public void run () {
repaint();
}*/

public boolean action(Event e, Object arg) {
if (e.target == bDoGraph) {
doGraph();
}
else
System.out.println("Unrecognized event");

return super.action(e, arg);
}

public void doGraph () {
bDoGraph.disable();

float r1 = floatFromTextField(textr1);
float r2 = floatFromTextField(textr2);
float r3 = floatFromTextField(textr3);
float r4 = floatFromTextField(textr4);
float r5 = floatFromTextField(textr5);
float r6 = floatFromTextField(textr6);
float r7 = floatFromTextField(textr7);
float r8 = floatFromTextField(textr8);
float r9 = floatFromTextField(textr9);
float r10 = floatFromTextField(textr10);
float r11 = floatFromTextField(textr11);
float r12 = floatFromTextField(textr12);
float r13 = floatFromTextField(textr13);
float r14 = floatFromTextField(textr14);
float r15 = floatFromTextField(textr15);
float b2 = floatFromTextField(textChlGln);
float b4 = floatFromTextField(textCytGln);
float b6 = floatFromTextField(textVacGln);
float b8 = floatFromTextField(textProtGln);
float d2 = floatFromTextField(textChlGln);
float d4 = floatFromTextField(textCytGln);
float d6 = floatFromTextField(textVacGln);
float d8 = floatFromTextField(textProtGln);
float c2 = floatFromTextField(textChlGlu);
float c4 = floatFromTextField(textCytGlu);
float c6 = floatFromTextField(textVacGlu);
float c8 = floatFromTextField(textProtGlu);
float e2 = floatFromTextField(textCytPro);
float e4 = floatFromTextField(textVacPro);
float e6 = floatFromTextField(textProtPro);
float f2 = floatFromTextField(textCytGABA);
float TS = floatFromTextField(textTS);
float TC = floatFromTextField(textTC);
float a1 = floatFromTextField(textPI);
float a2 = floatFromTextField(textCI);
float Y1 = floatFromTextField(textMy1);
float Y2 = floatFromTextField(textMy2);
float Opt = floatFromTextField(textOpt);

endXAxis1.setText(textTS.getText());
endXAxis2.setText(textTS.getText());
endYAxis1.setText(textMy1.getText());
endYAxis2.setText(textMy2.getText());

isotopeGraph.graphImage = blankGraph(GraphSize, GraphSize);
poolGraph.graphImage = blankGraph(GraphSize, GraphSize);

float b1;
float b3;
float b5;
float b7;
float c1;
float c3;
float c5;
float c7;
float d1;
float d3;
float d5;
float d7;
float e1;
float e3;
float e5;
float f1;
float t;
float z = TS / 2000;
b1=0;
c1=0;
d1=0;
e1=0;
f1=0;
b3=0;
c3=0;
d3=0;
e3=0;
b5=0;
c5=0;
d5=0;
e5=0;
b7=0;
c7=0;
d7=0;
for (t=0; t <= TS; t = t + z) {
if (t>TC) { a1 = a2; }
if (r1>0) { b1 = ((b1*b2)+(a1*z*r1))/(b2+(z*r1)); }
if (r1>0) { b2 = b2+(z*r1); }
if (r1>0) { b1 = ((b1*b2)-(b1*z*r1))/(b2-(z*r1)); }
if (r1>0) { b2 = b2-(z*r1); }
if (r1>0) { c1 = ((c1*c2)+(b1*z*r1))/(c2+(z*r1)); }
if (r1>0) { c2 = c2+(z*r1); }
if (r1>0) { c1 = ((c1*c2)+(d1*z*r1))/(c2+(z*r1)); }
if (r1>0) { c2 = c2+(z*r1); }
if (r1>0) { c1 = ((c1*c2)-(c1*z*2*r1))/(c2-(z*2*r1)); }
if (r1>0) { c2 = c2-(z*2*r1); }
if (r1>0) { d1 = ((d1*d2)+(c1*z*r1))/(d2+(z*r1)); }
if (r1>0) { d2 = d2+(z*r1); }
if (r1>0) { d1 = ((d1*d2)-(d1*z*r1))/(d2-(z*r1)); }
if (r1>0) { d2 = d2-(z*r1); }
if (r1>0) { c3 = ((c3*c4)+(c1*z*r1))/(c4+(z*r1)); }
if (r1>0) { c4 = c4+(z*r1); }
if (r9>0) { c3 = ((c3*c4)+(e1*z*r9))/(c4+(z*r9)); }
if (r9>0) { c4 = c4+(z*r9); }
if (r14>0) { c3 = ((c3*c4)+(f1*z*r14))/(c4+(z*r14)); }
if (r14>0) { c4 = c4+(z*r14); }
if (r2>0) { c3 = ((c3*c4)-(c3*z*r2))/(c4-(z*r2)); }
if (r2>0) { c4 = c4-(z*r2); }
if (r6>0) { c3 = ((c3*c4)-(c3*z*r6))/(c4-(z*r6)); }
if (r6>0) { c4 = c4-(z*r6); }
if (r7>0) { c3 = ((c3*c4)-(c3*z*r7))/(c4-(z*r7)); }
if (r7>0) { c4 = c4-(z*r7); }
if (r8>0) { c3 = ((c3*c4)-(c3*z*r8))/(c4-(z*r8)); }
if (r8>0) { c4 = c4-(z*r8); }
if (r10>0) { c3 = ((c3*c4)-(c3*z*r10))/(c4-(z*r10)); }
if (r10>0) { c4 = c4-(z*r10); }
if (r13>0) { c3 = ((c3*c4)-(c3*z*r13))/(c4-(z*r13)); }
if (r13>0) { c4 = c4-(z*r13); }
if (r2>0) { b3 = ((b3*b4)+(a1*z*r2))/(b4+(z*r2)); }
if (r2>0) { b4 = b4+(z*r2); }
if (r3>0) { b3 = ((b3*b4)-(b3*z*r3))/(b4-(z*r3)); }
if (r3>0) { b4 = b4-(z*r3); }
if (r4>0) { b3 = ((b3*b4)-(b3*z*r4))/(b4-(z*r4)); }
if (r4>0) { b4 = b4-(z*r4); }
if (r5>0) { b3 = ((b3*b4)-(b3*z*r5))/(b4-(z*r5)); }
if (r5>0) { b4 = b4-(z*r5); }
if (r2>0) { d3 = ((d3*d4)+(c3*z*r2))/(d4+(z*r2)); }
if (r2>0) { d4 = d4+(z*r2); }
if (r3>0) { d3 = ((d3*d4)-(d3*z*r3))/(d4-(z*r3)); }
if (r3>0) { d4 = d4-(z*r3); }
if (r4>0) { d3 = ((d3*d4)-(d3*z*r4))/(d4-(z*r4)); }
if (r4>0) { d4 = d4-(z*r4); }
if (r5>0) { d3 = ((d3*d4)-(d3*z*r5))/(d4-(z*r5)); }
if (r5>0) { d4 = d4-(z*r5); }
if (r10>0) { e1 = ((e1*e2)+(c3*z*r10))/(e2+(z*r10)); }
if (r10>0) { e2 = e2+(z*r10); }
if (r9>0) { e1 = ((e1*e2)-(e1*z*r9))/(e2-(z*r9)); }
if (r9>0) { e2 = e2-(z*r9); }
if (r11>0) { e1 = ((e1*e2)-(e1*z*r11))/(e2-(z*r11)); }
if (r11>0) { e2 = e2-(z*r11); }
if (r12>0) { e1 = ((e1*e2)-(e1*z*r12))/(e2-(z*r12)); }
if (r12>0) { e2 = e2-(z*r12); }
if (r12>0) { e3 = ((e3*e4)+(e1*z*r12))/(e4+(z*r12)); }
if (r12>0) { e4 = e4+(z*r12); }
if (r11>0) { e5 = ((e5*e6)+(e1*z*r11))/(e6+(z*r11)); }
if (r11>0) { e6 = e6+(z*r11); }
if (r13>0) { f1 = ((f1*f2)+(c3*z*r13))/(f2+(z*r13)); }
if (r13>0) { f2 = f2+(z*r13); }
if (r14>0) { f1 = ((f1*f2)-(f1*z*r14))/(f2-(z*r14)); }
if (r14>0) { f2 = f2-(z*r14); }
if (r15>0) { f1 = ((f1*f2)-(f1*z*r15))/(f2-(z*r15)); }
if (r15>0) { f2 = f2-(z*r15); }
if (r7>0) { c5 = ((c5*c6)+(c3*z*r7))/(c6+(z*r7)); }
if (r7>0) { c6 = c6+(z*r7); }
if (r4>0) { b5 = ((b5*b6)+(b3*z*r4))/(b6+(z*r4)); }
if (r4>0) { b6 = b6+(z*r4); }
if (r4>0) { d5 = ((d5*d6)+(d3*z*r4))/(d6+(z*r4)); }
if (r4>0) { d6 = d6+(z*r4); }
if (r6>0) { c7 = ((c7*c8)+(c3*z*r6))/(c8+(z*r6)); }
if (r6>0) { c8 = c8+(z*r6); }
if (r5>0) { b7 = ((b7*b8)+(b3*z*r5))/(b8+(z*r5)); }
if (r5>0) { b8 = b8+(z*r5); }
if (r5>0) { d7 = ((d7*d8)+(d3*z*r5))/(d8+(z*r5)); }
if (r5>0) { d8 = d8+(z*r5); }

if (b2<0) { break; }
if (c2<0) { break; }
if (d2<0) { break; }
if (e2<0) { break; }
if (f2<0) { break; }
if (b4<0) { break; }
if (c4<0) { break; }
if (d4<0) { break; }
if (e4<0) { break; }
if (b6<0) { break; }
if (c6<0) { break; }
if (d6<0) { break; }
if (e6<0) { break; }
if (b8<0) { break; }
if (c8<0) { break; }
if (d8<0) { break; }

if (Opt==1) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((((b1*b2)+(b3*b4)+(b5*b6)) / (b2+b4+b6)) / Y1) * GraphSize)), Color.green, 1, 1); }
if (Opt==1) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((((c1*c2)+(c3*c4)+(c5*c6)) / (c2+c4+c6)) / Y1) * GraphSize)), Color.red, 1, 1); }
if (Opt==1) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((((d1*d2)+(d3*d4)+(d5*d6)) / (d2+d4+d6)) / Y1) * GraphSize)), Color.orange, 1, 1); }
if (Opt==1) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((((e1*e2)+(e3*e4)) / (e2+e4)) / Y1) * GraphSize)), Color.blue, 1, 1); }
if (Opt==1) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((f1 / Y1) * GraphSize)), Color.black, 1, 1); }

if (Opt==2) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((b1 / Y1) * GraphSize)), Color.green, 1, 1); }
if (Opt==2) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((c1 / Y1) * GraphSize)), Color.red, 1, 1); }
if (Opt==2) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((d1 / Y1) * GraphSize)), Color.orange, 1, 1); }

if (Opt==3) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((b3 / Y1) * GraphSize)), Color.green, 1, 1); }
if (Opt==3) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((c3 / Y1) * GraphSize)), Color.red, 1, 1); }
if (Opt==3) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((d3 / Y1) * GraphSize)), Color.orange, 1, 1); }
if (Opt==3) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((e1 / Y1) * GraphSize)), Color.blue, 1, 1); }
if (Opt==3) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((f1 / Y1) * GraphSize)), Color.black, 1, 1); }

if (Opt==4) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((b5 / Y1) * GraphSize)), Color.green, 1, 1); }
if (Opt==4) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((c5 / Y1) * GraphSize)), Color.red, 1, 1); }
if (Opt==4) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((d5 / Y1) * GraphSize)), Color.orange, 1, 1); }
if (Opt==4) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((e3 / Y1) * GraphSize)), Color.blue, 1, 1); }

if (Opt==5) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((b7 / Y1) * GraphSize)), Color.green, 1, 1); }
if (Opt==5) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((c7 / Y1) * GraphSize)), Color.red, 1, 1); }
if (Opt==5) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((d7 / Y1) * GraphSize)), Color.orange, 1, 1); }
if (Opt==5) { isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((e5 / Y1) * GraphSize)), Color.blue, 1, 1); }

if (Opt==1) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((b2+b4+b6) / Y2) * GraphSize)), Color.green, 1, 1); }
if (Opt==1) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((c2+c4+c6) / Y2) * GraphSize)), Color.red, 1, 1); }
if (Opt==1) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((d2+d4+d6) / Y2) * GraphSize)), Color.orange, 1, 1); }
if (Opt==1) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((e2+e4) / Y2) * GraphSize)), Color.blue, 1, 1); }
if (Opt==1) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((f2 / Y2) * GraphSize)), Color.black, 1, 1); }

if (Opt==2) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((b2 / Y2) * GraphSize)), Color.green, 1, 1); }
if (Opt==2) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((c2 / Y2) * GraphSize)), Color.red, 1, 1); }
if (Opt==2) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((d2 / Y2) * GraphSize)), Color.orange, 1, 1); }

if (Opt==3) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((b4 / Y2) * GraphSize)), Color.green, 1, 1); }
if (Opt==3) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((c4 / Y2) * GraphSize)), Color.red, 1, 1); }
if (Opt==3) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((d4 / Y2) * GraphSize)), Color.orange, 1, 1); }
if (Opt==3) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((e2 / Y2) * GraphSize)), Color.blue, 1, 1); }
if (Opt==3) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((f2 / Y2) * GraphSize)), Color.black, 1, 1); }

if (Opt==4) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((b6 / Y2) * GraphSize)), Color.green, 1, 1); }
if (Opt==4) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((c6 / Y2) * GraphSize)), Color.red, 1, 1); }
if (Opt==4) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((d6 / Y2) * GraphSize)), Color.orange, 1, 1); }
if (Opt==4) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((e4 / Y2) * GraphSize)), Color.blue, 1, 1); }

if (Opt==5) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((b8 / Y2) * GraphSize)), Color.green, 1, 1); }
if (Opt==5) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((c8 / Y2) * GraphSize)), Color.red, 1, 1); }
if (Opt==5) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((d8 / Y2) * GraphSize)), Color.orange, 1, 1); }
if (Opt==5) { poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((e6 / Y2) * GraphSize)), Color.blue, 1, 1); }

//add error checking re: b2, c2, d2, e2, f2, b4, c4, d4, e4, b6, c6, d6, e6, b8, c8, d8 < 0
}



isotopeGraph.repaint();
poolGraph.repaint();
bDoGraph.enable();
repaint();

}




float floatFromTextField (TextField tf) {
String s;
float value;

s = tf.getText();

try {
value = Float.valueOf(s).floatValue();
} catch (Exception e) {
value = (float) 0.0;
}
return value;
}

Image blankGraph (int width, int height) {
Image tempImage;
Graphics tempGr;

tempImage = createImage(width, height);
tempGr = tempImage.getGraphics();



// draw a box around the graph
tempGr.setColor (Color.white);
tempGr.fillRect (0, 0, width, height);
tempGr.setColor (Color.black);
tempGr.drawRect (0, 0, width - 1, height - 1);
// draw ticks marks around edges
int i;
int hinterval = width / 5;
int vinterval = height / 5;
int TickLength = 2;
for (i = 0; i < 4; i++) {
tempGr.drawLine (hinterval * (i + 1), 0, hinterval * (i + 1), TickLength);
tempGr.drawLine (0, vinterval * (i + 1), TickLength, vinterval * (i + 1));
tempGr.drawLine (width - TickLength - 1, hinterval * (i + 1), height - 1, vinterval * (i + 1));
tempGr.drawLine (hinterval * (i + 1), height - TickLength - 1, hinterval * (i + 1), height);
}

return (tempImage);
}


Image addPoint (Image tempImage, int x, int y, Color pointColor, int size1, int size2) {
Graphics tempGr;

tempGr = tempImage.getGraphics();
tempGr.setColor (pointColor);
tempGr.fillRect (x, y, size1, size2);

return (tempImage);
}

}
class Graph extends Canvas {
public Image graphImage;
public int width;
public int height;
public Dimension preferredSize() {
return new Dimension (width, height);
}

Graph (int width, int height) {
this.width = width;
this.height = height;
resize(preferredSize());
}

public void update (Graphics g) { paint(g); }

public void paint (Graphics g) {
g.drawImage (graphImage, 0, 0, this);
}
}	