2010-05-27 84 views
0

有没有办法使用JFreeCharts为xy线图的绘图过程设置动画? 我希望能够观看程序绘制每一条线段并连接它们。如何编写JFreeCharts中绘制点之间的时间延迟?

例如,如果我将它粘贴到TextArea中,“gtgtaaacaatatatggcg”,我想要看它每个线段的图形。

在此先感谢! :)

我的代码如下:

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 RandomWalkComplete extends Applet implements ActionListener 
{ 
    Panel panel; 
    TextArea textarea, outputArea; 
    Button move,exit; 
    String thetext; 
    Scanner reader = new Scanner(System.in); 
    String thetext2; 
    Label instructions; 

    int x,y; 

    public void init() 
    { 
     setSize(500,250); //set size of applet 

     instructions=new Label("Paste the gene sequences in the " + 
       "text field and press the graph button."); 
     add(instructions); 

     panel = new Panel(); 
     add(panel); 
     setVisible(true); 
     textarea= new TextArea(10,20); 
     add(textarea); 

     move=new Button("Graph"); 
     move.addActionListener(this); 
     add(move); 

     exit=new Button("Exit"); 
     exit.addActionListener(this); 
     add(exit); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     XYSeries series = new XYSeries("DNA Walk",false,true); 

     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': case 'a'://moves right 
        x+=1; y+=0; 
        series.add(x,y); 
        break; 

       case 'C': case 'c': //moves up 
        x+=0; y+=1; 
        series.add(x,y); 
        break; 

       case 'G': case 'g': //move left 
        x-=1; y+=0; 
        series.add(x,y); 
        break; 

       case 'T': case 't'://move down 
        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); 
     }  
     if(e.getSource()==exit) 
     {System.exit(0);}  
    } 
    public void stop(){} 
} 
+0

重新格式化的代码;如果不正确请回复。 – trashgod 2010-05-27 21:24:54

回答

1

javax.swing.Timer一个实例很适合这一点,因为在How to Use Swing Timers描述。

附录:正如@David Sauter所观察到的,scheduleAtFixedRate()方法java.util.Timer将是一个合适的选择。文章Using Timers in Swing Applications比较了这两种方法。

+1

+1:调用定时器上的'scheduleAxFixedRate(...)'来实现定期更新。可能实现一个观众模式,听众可以在计时器中注册并在每个“脉冲”上得到通知以更新情节。 – 2010-05-28 09:02:55

+0

@David Sauter:关于'java.util.Timer'的好处。 – trashgod 2010-05-28 12:24:27