I have to write an Applet that allows the user to paste groups of letter A,T,G,C into a text area, press a button, the the corresponding graph will pop up. Specifically, “A” means move right, “T” is move down, “C” is move up, and “G” is move left. For example if I paste a sequence like "aacacggtgcgggttt" the plot should look like
HTML Code:
http://img99.imageshack.us/img99/7607/screenshot20100511at359.png
<-this is by matlab. However, when I use my Applet, it looks like
HTML Code:
http://img440.imageshack.us/img440/2757/screenshot20100512at105.png
The "G"s mess up the graph, but I can't figure out why. I am using JFreeCharts to make the graph.
My code is below. Thank you in advance for your help!! =)
import java.util.Scanner;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.*;
public class TextArea2 extends Applet implements ActionListener
{
Panel panel;
TextArea textarea, outputArea;
Button move;
String thetext;
Scanner reader = new Scanner(System.in);
String thetext2;
int x,y;
public void init()
{
setSize(500,500); //set size of applet
panel = new Panel();
add(panel);
setVisible(true);
textarea= new TextArea(10,20);
add(textarea);
move=new Button("Graph");
move.addActionListener(this);
add(move);
}
public void actionPerformed(ActionEvent e)
{
XYSeries series = new XYSeries("DNA Walk");
x= 0; y = 0;
series.add(x,y);
if(e.getSource() == move)
{
thetext=textarea.getText(); //the text is the DNA bases pasted
thetext=thetext.replaceAll(" ",""); //removes spaces
thetext2 = "";
for(int i=0; i<thetext.length(); i++)
{
char a = thetext.charAt(i);
switch (a)
{
case 'A': //moves right
x+=1; y+=0;
series.add(x,y);
break;
case 'a':
x+=1;y+=0;
series.add(x,y);
break;
case 'C': //moves up
x+=0; y+=1;
series.add(x,y);
break;
case 'c':
x+=0; y+=1;
System.out.println(x + "," + y);
series.add(x,y);
break;
case 'G': //move left
x-=1; y+=0;
series.add(x,y);
break;
case 'g':
x-=1; y+=0;
System.out.println(x + "," + y);
series.add(x,y);
break;
case 'T': //move down
x+=0; y-=1;
series.add(x,y);
break;
case 't':
x+=0; y-=1;
series.add(x,y);
break;
default: // series.add(0,0);
break;
}
}
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart
("DNA Random Walk", "", "", xyDataset, PlotOrientation.VERTICAL, true, true, false);
ChartFrame frame1=new ChartFrame("DNA Random Walk",chart);
frame1.setVisible(true);
frame1.setSize(300,300);
}
}
}