2010-05-27 67 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