如何在 JFreeCharts 中编写绘图点之间的时间延迟代码?

发布于 2024-09-03 09:30:00 字数 2945 浏览 2 评论 0原文

有没有办法使用 JFreeCharts 动画绘制 xy 线图表的过程? 我希望能够观看程序绘制每个线段并将它们连接起来。

例如,如果我将其粘贴到文本区域“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(){}
}

Is there a way to animate the plotting process of an xy-line chart using JFreeCharts?
I want to be able to watch the program draw each line segment and connect them.

For example, if I paste this into the TextArea, "gtgtaaacaatatatggcg," I want to watch it graph each line segment one by one.

Thanks in advance! :)

My code is below:

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(){}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生路 2024-09-10 09:30:00

javax.swing.Timer< 的实例/code>对此效果很好,如 如何使用 Swing 计时器

附录:正如 @David Sauter 所观察到的, java.util.Timer 将是一个合适的替代方案。文章在 Swing 应用程序中使用计时器 比较两种方法。

An instance of javax.swing.Timer works well for this, as described in How to Use Swing Timers.

Addendum: As @David Sauter observes, the scheduleAtFixedRate() method of java.util.Timer would be a suitable alternative. The article Using Timers in Swing Applications compares the two approaches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文