gpt4 book ai didi

java - 为什么 JFreeCharts 无法正确连接 xy 线图中的点?

转载 作者:行者123 更新时间:2023-12-01 05:56:24 25 4
gpt4 key购买 nike

每个字母 A、T、G、C 代表绘图的方向。具体来说,“A”表示向右移动,“T”表示向下移动,“C”表示向上移动,“G”表示向左移动。当小程序读取 A、T、C 时,它会正确绘制图表。然而,当我绘制 G 时,图表困惑了。当我输入“ACACACA”时,图表就像一个上升的楼梯。当我输入“gtgtgt”时,图形应该看起来像一个楼梯,但它看起来像一个闪电。

import java.applet.Applet;
import java.awt.Button;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class If_Graph extends Applet implements ActionListener {

Panel panel;
TextArea textarea, outputArea;
Button move;
String thetext;
Scanner reader = new Scanner(System.in);
String thetext2;
int size, p, q;
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);
System.out.println("G is: " + x + "," + y);
break;
case 'g':
x -= 1;
y += 0;
System.out.println("g is: " + x + "," + y);
series.add(x, y);
break;
case 'T': //move down
x += 0;
y -= 1;
series.add(x, y);
System.out.println("T is: " + x + "," + y);
break;
case 't':
x += 0;
y -= 1;
series.add(x, y);
System.out.println("t is: " + 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);
outputArea.setText(thetext2);
}
}
}

最佳答案

请注意XYSeries允许“排序和重复默认值都可以在构造函数中更改。”所以试试这个:

XYSeries series = new XYSeries("DNA Walk", false, true);

alt text

此外,请考虑 switch 的这种变体形式声明:

switch (a) {
case 'A': case 'a': //moves right
x += 1;
y += 0;
series.add(x, y);
System.out.println("A is: " + x + "," + y);
break;
case 'C': case 'c': //moves up
...

关于java - 为什么 JFreeCharts 无法正确连接 xy 线图中的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2822595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com