/*
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  textA1, textA2, textA3, textF1, textB1, textB2, textB3, textE1, textC1, textC2, textC3, textE2, textD1, textD2, textD3, textk;
TextField  textTS;
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 labelA1 = new Label("A1:");
textA1 = new TextField("0", 6);
Label labelA2 = new Label("A2:");
textA2 = new TextField("6", 6);
Label labelA3 = new Label("A3:");
textA3 = new TextField("0.05", 6);
Label labelF1 = new Label("F1:");
textF1 = new TextField("0", 6);
Label labelB1 = new Label("B1:");
textB1 = new TextField("0", 6);
Label labelB2 = new Label("B2:");
textB2 = new TextField("9", 6);
Label labelB3 = new Label("B3:");
textB3 = new TextField("0.05", 6);
Label labelE1 = new Label("E1:");
textE1 = new TextField("1000", 6);
Label labelC1 = new Label("C1:");
textC1 = new TextField("0", 6);
Label labelC2 = new Label("C2:");
textC2 = new TextField("12", 6);
Label labelC3 = new Label("C3:");
textC3 = new TextField("0.05", 6);
Label labelE2 = new Label("E2:");
textE2 = new TextField("4", 6);
Label labelD1 = new Label("D1:");
textD1 = new TextField("0", 6);
Label labelD2 = new Label("D2:");
textD2 = new TextField("20", 6);
Label labelD3 = new Label("D3:");
textD3 = new TextField("0.05", 6);
Label labelk = new Label("k:");
textk = new TextField("0.0125", 6);
Label labelTS = new Label("TS:");
textTS = new TextField("500.0", 6);
Label labelMR = new Label("MR:");
textMy1 = new TextField("4000.0", 6);
Label labelMP = new Label("MP:");
textMy2 = new TextField("50.00", 6);
Label gLabel1 = new Label("Radioactivity");
Label xLabel1 = new Label("Time (min)");
Label yLabel1 = new Label("nCi.gfw(-1)");
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 (min)");
Label yLabel2 = new Label("nmol.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(labelA1,  gbc);
add(labelA1);
gbc.gridy = 1;
gb.setConstraints(labelA2,  gbc);
add(labelA2);
gbc.gridy = 2;
gb.setConstraints(labelA3,  gbc);
add(labelA3);
gbc.gridy = 3;
gb.setConstraints(labelF1,  gbc);
add(labelF1);

gbc.gridy = 6;
gbc.anchor = GridBagConstraints.NORTHEAST;
gb.setConstraints(endYAxis1,  gbc);
add(endYAxis1);
gbc.gridy = 8;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridheight = 3;
gb.setConstraints(yLabel1,  gbc);
add(yLabel1);
gbc.gridy = 10;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gb.setConstraints(startYAxis1,  gbc);
add(startYAxis1);

gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gb.setConstraints(textA1,  gbc);
add(textA1);
gbc.gridy = 1;
gb.setConstraints(textA2,  gbc);
add(textA2);
gbc.gridy = 2;
gb.setConstraints(textA3,  gbc);
add(textA3);
gbc.gridy = 3;
gb.setConstraints(textF1,  gbc);
add(textF1);
gbc.gridy = 5;
gbc.gridwidth = 5;
gb.setConstraints(gLabel1,  gbc);
add(gLabel1);
gbc.gridy = 6;
gbc.gridheight = 5;
gb.setConstraints(isotopeGraph,  gbc);
add(isotopeGraph);
gbc.gridy = 11;
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(labelB1,  gbc);
add(labelB1);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelB2,  gbc);
add(labelB2);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelB3,  gbc);
add(labelB3);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelk,  gbc);
add(labelk);
gbc.gridy = 11;
gbc.gridwidth = 3;
gb.setConstraints(xLabel1,  gbc);
add(xLabel1);

gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 0;
gb.setConstraints(textB1,  gbc);
add(textB1);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textB2,  gbc);
add(textB2);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textB3,  gbc);
add(textB3);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textk,  gbc);
add(textk);

gbc = new GridBagConstraints();
gbc.gridx = 4;
gbc.gridy = 0;
gb.setConstraints(labelC1,  gbc);
add(labelC1);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelC2,  gbc);
add(labelC2);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelC3,  gbc);
add(labelC3);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelTS,  gbc);
add(labelTS);

gbc = new GridBagConstraints();
gbc.gridx = 5;
gbc.gridy = 0;
gb.setConstraints(textC1,  gbc);
add(textC1);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textC2,  gbc);
add(textC2);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textC3,  gbc);
add(textC3);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textTS,  gbc);
add(textTS);
gbc.gridy = 11;
gbc.anchor = GridBagConstraints.NORTHEAST;
gb.setConstraints(endXAxis1,  gbc);
add(endXAxis1);

gbc = new GridBagConstraints();
gbc.gridx = 6;
gbc.gridy = 0;
gb.setConstraints(labelD1,  gbc);
add(labelD1);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelD2,  gbc);
add(labelD2);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelD3,  gbc);
add(labelD3);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelE1,  gbc);
add(labelE1);

gbc = new GridBagConstraints();
gbc.gridx = 7;
gbc.gridy = 0;
gb.setConstraints(textD1,  gbc);
add(textD1);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textD2,  gbc);
add(textD2);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textD3,  gbc);
add(textD3);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textE1,  gbc);
add(textE1);

gbc = new GridBagConstraints();
gbc.gridx = 8;
gbc.gridy = 0;
gb.setConstraints(labelE2,  gbc);
add(labelE2);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelMR,  gbc);
add(labelMR);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelMP,  gbc);
add(labelMP);

gbc.gridy = 6;
gbc.anchor = GridBagConstraints.NORTHEAST;
gb.setConstraints(endYAxis2,  gbc);
add(endYAxis2);
gbc.gridy = 8;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridheight = 3;
gb.setConstraints(yLabel2,  gbc);
add(yLabel2);
gbc.gridheight = 1;
gbc.gridy = 10;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gb.setConstraints(startYAxis2,  gbc);
add(startYAxis2);

gbc = new GridBagConstraints();
gbc.gridx = 9;
gbc.gridy = 0;
gb.setConstraints(textE2,  gbc);
add(textE2);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textMy1,  gbc);
add(textMy1);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textMy2,  gbc);
add(textMy2);

gbc.gridy = 5;
gbc.gridwidth = 5;
gb.setConstraints(gLabel2,  gbc);
add(gLabel2);
gbc.gridy = 6;
gbc.gridheight = 5;
gb.setConstraints(poolGraph,  gbc);
add(poolGraph);
gbc.gridy = 11;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.NORTHWEST;
gb.setConstraints(startXAxis2,  gbc);
add(startXAxis2);

gbc = new GridBagConstraints();
gbc.gridx = 10;
gbc.gridy = 11;
gbc.gridwidth = 3;
gb.setConstraints(xLabel2,  gbc);
add(xLabel2);

gbc.gridx = 6;
gbc.gridy = 9;
gbc.gridwidth = 1;
gb.setConstraints(bDoGraph,  gbc);
add(bDoGraph);

gbc.gridx = 13;
gbc.gridy = 11;
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 A1 = floatFromTextField(textA1);
float A2 = floatFromTextField(textA2);
float A3 = floatFromTextField(textA3);
float B1 = floatFromTextField(textB1);
float B2 = floatFromTextField(textB2);
float B3 = floatFromTextField(textB3);
float E1 = floatFromTextField(textE1);
float C1 = floatFromTextField(textC1);
float C2 = floatFromTextField(textC2);
float C3 = floatFromTextField(textC3);
float E2 = floatFromTextField(textE2);
float D1 = floatFromTextField(textD1);
float D2 = floatFromTextField(textD2);
float D3 = floatFromTextField(textD3);
float F1 = floatFromTextField(textF1);
float k = floatFromTextField(textk);
float TS = floatFromTextField(textTS);
float Y1 = floatFromTextField(textMy1);
float Y2 = floatFromTextField(textMy2);

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 A4;
float t;
float Z = TS / 2000;
for (t=0; t <= TS; t = t + Z) {
A4 = E2*k;
if (A4>0) { E2 = E2-(Z*A4); }
if (A4>0) { A1 = ((A1*A2)+(E1*Z*A4))/(A2+(Z*A4)); }
if (A4>0) { A2 = A2+(Z*A4); }
if (A3>0) { A1 = ((A1*A2)+(F1*Z*A3))/(A2+(Z*A3)); }
if (A3>0) { A2 = A2+(Z*A3); }
if (B3>0) { A1 = ((A1*A2)-(A1*Z*B3))/(A2-(Z*B3)); }
if (B3>0) { A2 = A2-(Z*B3); }
if (B3>0) { B1 = ((B1*B2)+(A1*Z*B3))/(B2+(Z*B3)); }
if (B3>0) { B2 = B2+(Z*B3); }
if (C3>0) { B1 = ((B1*B2)-(B1*Z*C3))/(B2-(Z*C3)); }
if (C3>0) { B2 = B2-(Z*C3); }
if (C3>0) { C1 = ((C1*C2)+(B1*Z*C3))/(C2+(Z*C3)); }
if (C3>0) { C2 = C2+(Z*C3); }
if (D3>0) { C1 = ((C1*C2)-(C1*Z*D3))/(C2-(Z*D3)); }
if (D3>0) { C2 = C2-(Z*D3); }
if (D3>0) { D1 = ((D1*D2)+(C1*Z*D3))/(D2+(Z*D3)); }
if (D3>0) { D2 = D2+(Z*D3); }
if (E2<0) { break; }
if (A2<0) { break; }
if (B2<0) { break; }
if (C2<0) { break; }
if (D2<0) { break; }

isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((E1*E2) / Y1) * GraphSize)), Color.black);
isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((A1*A2) / Y1) * GraphSize)), Color.blue);
isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((B1*B2) / Y1) * GraphSize)), Color.green);
isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((C1*C2) / Y1) * GraphSize)), Color.red);
isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) (((D1*D2) / Y1) * GraphSize)), Color.orange);
poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((E2 / Y2) * GraphSize)), Color.black);
poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((A2 / Y2) * GraphSize)), Color.blue);
poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((B2 / Y2) * GraphSize)), Color.green);
poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((C2 / Y2) * GraphSize)), Color.red);
poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / TS) * GraphSize), GraphSize - ((int) ((D2 / Y2) * GraphSize)), Color.orange);
//add error checking re: A2, B2, C2, D2 < 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) {
Graphics tempGr;

tempGr = tempImage.getGraphics();
tempGr.setColor (pointColor);
tempGr.fillRect (x, y, 1, 1);

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);
}
}	