/*
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, textB1, textB2, textB3, textB4, textC1, textC2, textC3, textC4, textD1, textD2, textD3, textD4;
TextField  textTimeScale;
TextField  textChaseTime;
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("90", 6);
Label labelA2 = new Label("A2:");
textA2 = new TextField("1", 6);
Label labelB1 = new Label("B1:");
textB1 = new TextField("0", 6);
Label labelB2 = new Label("B2:");
textB2 = new TextField("600", 6);
Label labelB3 = new Label("B3:");
textB3 = new TextField("100", 6);
Label labelB4 = new Label("B4:");
textB4 = new TextField("100", 6);
Label labelC1 = new Label("C1:");
textC1 = new TextField("0", 6);
Label labelC2 = new Label("C2:");
textC2 = new TextField("500", 6);
Label labelC3 = new Label("C3:");
textC3 = new TextField("60", 6);
Label labelC4 = new Label("C4:");
textC4 = new TextField("60", 6);
Label labelD1 = new Label("D1:");
textD1 = new TextField("0", 6);
Label labelD2 = new Label("D2:");
textD2 = new TextField("400", 6);
Label labelD3 = new Label("D3:");
textD3 = new TextField("40", 6);
Label labelD4 = new Label("D4:");
textD4 = new TextField("40", 6);
Label labelTime = new Label("TS:");
textTimeScale = new TextField("120.0", 6);
Label labelChase = new Label("TC:");
textChaseTime = new TextField("60", 6);
Label labelMA = new Label("MA:");
textMy1 = new TextField("100.0", 6);
Label labelMP = new Label("MP:");
textMy2 = new TextField("1000.0", 6);
Label gLabel1 = new Label("Isotope Abundance");
Label xLabel1 = new Label("Time (min)");
Label yLabel1 = new Label("Atom %");
Label startXAxis1 = new Label ("0");
Label startYAxis1 = new Label ("0");
endXAxis1 = new Label (textTimeScale.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 (textTimeScale.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 = 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 = 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(labelB4,  gbc);
add(labelB4);
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(textB4,  gbc);
add(textB4);

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(labelC4,  gbc);
add(labelC4);

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(textC4,  gbc);
add(textC4);
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(labelD4,  gbc);
add(labelD4);

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(textD4,  gbc);
add(textD4);

gbc = new GridBagConstraints();
gbc.gridx = 8;
gbc.gridy = 0;
gb.setConstraints(labelTime,  gbc);
add(labelTime);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelChase,  gbc);
add(labelChase);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(labelMA,  gbc);
add(labelMA);
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(textTimeScale,  gbc);
add(textTimeScale);
gbc.gridy = GridBagConstraints.RELATIVE;
gb.setConstraints(textChaseTime,  gbc);
add(textChaseTime);
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 B1 = floatFromTextField(textB1);
float B2 = floatFromTextField(textB2);
float B3 = floatFromTextField(textB3);
float B4 = floatFromTextField(textB4);
float C1 = floatFromTextField(textC1);
float C2 = floatFromTextField(textC2);
float C3 = floatFromTextField(textC3);
float C4 = floatFromTextField(textC4);
float D1 = floatFromTextField(textD1);
float D2 = floatFromTextField(textD2);
float D3 = floatFromTextField(textD3);
float D4 = floatFromTextField(textD4);
float timeScale = floatFromTextField(textTimeScale);
float chaseTime = floatFromTextField(textChaseTime);
float Y1 = floatFromTextField(textMy1);
float Y2 = floatFromTextField(textMy2);

endXAxis1.setText(textTimeScale.getText());
endXAxis2.setText(textTimeScale.getText());
endYAxis1.setText(textMy1.getText());
endYAxis2.setText(textMy2.getText());

isotopeGraph.graphImage = blankGraph(GraphSize, GraphSize);
poolGraph.graphImage = blankGraph(GraphSize, GraphSize);

float t;
float Z = timeScale / 2000;
for (t=0; t <= timeScale; t = t + Z) {
if (t > chaseTime) {
A1= A2;
}
if (B3>0) { B1 = ((B1*B2)+(A1*Z*B3))/(B2+(Z*B3)); }
if (B3>0) { B2 = B2+(Z*B3); }
if (B4>0) { B1 = ((B1*B2)-(B1*Z*B4))/(B2-(Z*B4)); }
if (B4>0) { B2 = B2-(Z*B4); }
if (C3>0) { C1 = ((C1*C2)+(B1*Z*C3))/(C2+(Z*C3)); }
if (C3>0) { C2 = C2+(Z*C3); }
if (C4>0) { C1 = ((C1*C2)-(C1*Z*C4))/(C2-(Z*C4)); }
if (C4>0) { C2 = C2-(Z*C4); }
if (D3>0) { D1 = ((D1*D2)+(C1*Z*D3))/(D2+(Z*D3)); }
if (D3>0) { D2 = D2+(Z*D3); }
if (D4>0) { D1 = ((D1*D2)-(D1*Z*D4))/(D2-(Z*D4)); }
if (D4>0) { D2 = D2-(Z*D4); }
if (B2<0) { break; }
if (C2<0) { break; }
if (D2<0) { break; }

isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / timeScale) * GraphSize), GraphSize - ((int) ((A1 / Y1) * GraphSize)), Color.black);
isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / timeScale) * GraphSize), GraphSize - ((int) ((B1 / Y1) * GraphSize)), Color.green);
isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / timeScale) * GraphSize), GraphSize - ((int) ((C1 / Y1) * GraphSize)), Color.blue);
isotopeGraph.graphImage = addPoint(isotopeGraph.graphImage, (int) ((t / timeScale) * GraphSize), GraphSize - ((int) ((D1 / Y1) * GraphSize)), Color.red);
poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / timeScale) * GraphSize), GraphSize - ((int) ((B2 / Y2) * GraphSize)), Color.green);
poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / timeScale) * GraphSize), GraphSize - ((int) ((C2 / Y2) * GraphSize)), Color.blue);
poolGraph.graphImage = addPoint(poolGraph.graphImage, (int) ((t / timeScale) * GraphSize), GraphSize - ((int) ((D2 / Y2) * GraphSize)), Color.red);
//add error checking re: 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);
}
}	