import java.awt.*;
import java.applet.Applet;

public class Simp extends Applet {
	TextField textField1;
	TextField textField1a;
	TextField textField2;
	TextField textField2a;
	TextField textField3;
	TextArea textArea;
	Label lab1;
	Label lab1a;
	Label lab2;
	Label lab2a;
	Label lab3;
	Ndraw anp;

	public void init() {
		this.setBackground(new Color(200,200,255));
		textField1 = new TextField(25);
		textField1a = new TextField(25);
		textField2 = new TextField(25);
		textField2a = new TextField(25);
		textField3 = new TextField(25);
    textArea = new TextArea(4, 25);
    textArea.setEditable(false);
		textField3.setEditable(false);
		lab1 = new Label("sense DNA:");
		lab1a = new Label("antisense DNA:");
		lab2 = new Label("mRNA:");
		lab2a = new Label("tRNA list:");
		lab3 = new Label("polypeptide:");
		int[][] pos = {{0,0},{1,0},{0,1},{1,1},{2,1}};
		int[] lpos = {1,2,1,1,0};
		String[] d = {"","","","",""};
		Color[] dc = {Color.red,Color.red,Color.blue,new Color(0,100,0),new Color(0,70,70)};
		anp = new Ndraw(d,dc,pos,lpos,4);
		//for anp declare see below

		//Add Components to the Applet.
		GridBagLayout gridBag = new GridBagLayout();
		setLayout(gridBag);
		GridBagConstraints c = new GridBagConstraints();

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 1;
		c.weightx = 1;
		gridBag.setConstraints(lab1, c);
		add(lab1);

		c.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(lab1a, c);
		add(lab1a);


		c.gridwidth = 1;
		gridBag.setConstraints(textField1, c);
		add(textField1);

		c.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(textField1a, c);
		add(textField1a);

		c.gridwidth = 1;
		gridBag.setConstraints(lab2, c);
		add(lab2);

		c.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(lab2a, c);
		add(lab2a);

		c.gridwidth = 1;
		gridBag.setConstraints(textField2, c);
		add(textField2);

		c.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(textField2a, c);
		add(textField2a);

		c.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(lab3, c);
		add(lab3);

		c.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(textField3, c);
		add(textField3);

		c.fill = GridBagConstraints.BOTH;
		c.weighty = 1;
		c.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(textArea, c);
		add(textArea);
		c.weighty = .5;

		c.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(anp, c);
		add(anp);


		validate();
	}

	public boolean action(Event evt, Object arg) {
		DNA DP;
		DNA DC;
		mRNA MP;
		mRNA MC;
		PolyP PP;
		String text;
		text = arg.toString();
		if( text.equals("?") || text.equals("help")){
			giveHelp();
		}
		//USER ENTERS PRIMARY DNA
		if (evt.target == textField1){
			text = textField1.getText();
    	textField1.selectAll();
			try{
				DP = new DNA(text);
				DC = new DNA(DP.getCodonSet().ComplimentTo("DNA"));
				DP = new DNA(DC.getCodonSet().ComplimentTo("DNA"));
				MP = new mRNA(DP);
				MC = new mRNA(DC);
				PP = new PolyP(MP);
				setUp(DP, DC, MP, MC, PP);
			}catch (NotABaseException e){
				textArea.appendText("\""+new Character(e.base).toString()+"\""+" is not a valid base.");
				textArea.appendText("\n");
			}//close try-catch
		}//close if

		//USER ENTER COMPLIMENT DNA
		if (evt.target == textField1a){
			text = textField1a.getText();
    	textField1a.selectAll();
    	try{
				DC = new DNA(text);
				DP = new DNA(DC.getCodonSet().ComplimentTo("DNA"));
				DC = new DNA(DP.getCodonSet().ComplimentTo("DNA"));
				MP = new mRNA(DP);
				MC = new mRNA(DC);
				PP = new PolyP(MP);
				setUp(DP, DC, MP, MC, PP);
			}catch (NotABaseException e){
				textArea.appendText("\""+new Character(e.base).toString()+"\""+" is not a valid base.");
				textArea.appendText("\n");
			}//close try-catch
		}//close if

		//USER ENTERS PRIMARY RNA
		if (evt.target == textField2){
			text = textField2.getText();
    	textField2.selectAll();

			try{
				MP = new mRNA(text);
				DP = new DNA(MP);
				DC = new DNA(DP.getCodonSet().ComplimentTo("DNA"));
				MP = new mRNA(DP);
				MC = new mRNA(DC);
				PP = new PolyP(MP);
				setUp(DP, DC, MP, MC, PP);
			}catch (NotABaseException e){
				textArea.appendText("\""+new Character(e.base).toString()+"\""+" is not a valid base.");
				textArea.appendText("\n");
			}//close try-catch
		}//close if

		//USER ENTERS COMPLIMENT RNA
		if (evt.target == textField2a){
			text = textField2a.getText();
			textField2a.selectAll();
			StringBuffer pps = new StringBuffer();
			try{
				MC = new mRNA(text);
				DC = new DNA(MC);
				DP = new DNA(DC.getCodonSet().ComplimentTo("DNA"));
				MC = new mRNA(DC);
				MP = new mRNA(DP);
				PP = new PolyP(MP);
				setUp(DP, DC, MP, MC, PP);
			}catch (NotABaseException e){
				textArea.appendText("\""+new Character(e.base).toString()+"\""+" is not a valid base.");
				textArea.appendText("\n");
			}//close try-catch
		}//close if
		return true;
	}
	public void setUp(DNA DP, DNA DC, mRNA MP, mRNA MC, PolyP PP){
			int i;
			String[] data;
			data = new String[5];
			data[0] = DP.toString();
			data[1] = DC.toString();
			data[2] = MP.toString();
			data[3] = MC.toString();
			String[] ps;
			StringBuffer pps = new StringBuffer();
			textField1.setText(DP.toString());
			textField1a.setText(DC.toString());
			textField2.setText(MP.toString());
			textField2a.setText(MC.toString());
			ps = PP.Ribosome();
			for(i=0;i<ps.length;i++){
				pps.append(ps[i]+ " ");
			}//close for
			textField3.setText(pps.toString());
			textArea.appendText(pps.toString()+"\n");
			data[4] = pps.toString();
			anp.setString(data);
	}
	public void giveHelp(){
		textArea.appendText("VER: 5/16/98"+"\n");
		textArea.appendText("Applet and other classes by Robert Arnold Hultman Kramer. Just call me \"boy wonder\"."+"\n");
	}
	public void paint(Graphics g){
		anp.repaint();
	}
}
