嗨,我正在尝试实现一种算法并实现其功能:当其中一个分子数变为0时,整个while循环将结束,并将自动使用JFreeChart进行下一步图表绘制。现在,在使用"return“之后,eclipse不会显示任何错误,并完全处理main方法。但是,我看不到我的图表显示出来。想知道是否有任何步骤出错。我想展示不同物种在不同时间的分子数。每个物种将代表一条新的线。SoI创建新图表
static JPanel chartPanel;
在构造函数中,我设置了一些类似的东西(只需遵循在线教程)
super("Line Chart of molecule numbers at different times");
add(chartPanel, BorderLayout.CENTER);
setSize(640, 480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
在main方法中,我希望在折线图中显示输入和计算的数字(没有创建另一个类,因为害怕空指针异常)。
public static void main(String args[]) throws Exception {
//input the number of species
System.out.println("Enter the number of species:");
int n = sc.nextInt();
//input the number of reactions
System.out.println("Enter the number of reactions:");
int m = sc.nextInt();
//
int[][]matrixPre = new int[m][n];
enterMatrixDataPre(sc, matrixPre, m, n);
printMatrixPre(matrixPre, m, n);
//convert the 2d int to 2d double
double [][] matrixPre2 = new double[m][n];
for(int i = 0; i <m; i++)
{
for(int j = 0; j < n; j++)
matrixPre2[i][j] = (double) matrixPre[i][j];
}
RealMatrix PreMatrix = new Array2DRowRealMatrix(matrixPre2);
// remember to add space key when doing the typing
int[][]matrixPost = new int[m][n];
enterMatrixDataPost(sc, matrixPost, m, n);
printMatrixPost(matrixPost, m, n);
//convert the 2d int to 2d double
double [][] matrixPost2 = new double[m][n];
for(int i = 0; i <m; i++)
{
for(int j = 0; j < n; j++)
matrixPost2[i][j] = (double) matrixPost[i][j];
}
RealMatrix PostMatrix = new Array2DRowRealMatrix(matrixPost2);
//
RealMatrix matrixSubtract = PreMatrix.subtract(PostMatrix);
System.out.println("So the transpose matrix after subtraction is:\t"+matrixSubtract.transpose());
//input the default maxium time of the whole reaction
System.out.println("Enter the maxium time of the whole reaction:");
double Tmax =sc.nextDouble();
//input the name of all the species
System.out.println("Enter the name of all species");
String[] strs=new String[n];
for(int i = 0; i< n; i++) {
strs[i]=sc.nextLine();
}
//input the rate constant of all the reactions(the coefficient), must be a double
System.out.println("Enter the rate constant of each reaction:");
Double[] rate=new Double[m];
for(int r = 0; r< m; r++) {
rate[r]=sc.nextDouble();
}
//
Vector<Double> timeList = new Vector<Double>(0);
Vector<int[]> allStateList = new Vector<int[]>(0);
timeList.add(newTime);
//input the initial states of numbers of molecules
System.out.println("Enter the initial molecule numbers of all species:");
int[]state = new int[n];
for (int i = 0; i< n; i++)
{
state[i]=sc.nextInt();
}
allStateList.add(state);
while(newTime<Tmax) {
for(int loopIndex =0; loopIndex<allStateList.size(); loopIndex++) {
// calculate the hazard for each reaction and the general hazard
double[] h = new double[m];
H = 0;
try {
for(int i =0; i<m; i++) {
for(int j =0; j<n; j++) {
h[i]=rate[i]*CombinatoricsUtils.binomialCoefficientDouble(allStateList.get(loopIndex)[j],(int)(PreMatrix.getRowVector(i).toArray()[j]));
H +=h[i];
}
}
}
catch(NumberIsTooLargeException exceptionn) {
System.out.println("One of the species has been exhausted and there is no next firing");
return;
}
System.out.println("So the general hazard is:"+H);
// select a random reaction time
Random random = new Random();
double tau = (1*Math.log(1/random.nextDouble()))/H;
System.out.println("So the random reaction time is"+tau);
//put the newTime
newTime = timeList.get(loopIndex)+tau;
System.out.println("So the new reaction time is:" + newTime);
timeList.add(newTime);
//select a random reaction j
Random random2 = new Random();
int index =0;
for (int i=0; i<m; i++) {
if(h[i]>random2.nextDouble()*H)
index =i;
}
System.out.println("So the next simulated event is:"+index);
//Update the state
double[] vectorDoubleArray = matrixSubtract.transpose().getColumnVector(index).toArray();
int[] intArray = new int[n];
for (int i=0; i<n; i++) {
intArray[i] = (int) vectorDoubleArray[i];
}
int[] newState = new int[n];
int newS =0;
for (int p =0; p<n; p++){
newS= intArray[p]+allStateList.get(loopIndex)[p];
newState[p]=newS;
}
System.out.println("Right now the molecule number of all species are:"+Arrays.toString(newState));
allStateList.add(newState);
}
//close the scanner
sc.close();
}
}
所有准备工作完成后,我想使用jfreechart打印两个向量列表的数字timeList和allStateList
String chartTitle = "Line chart of molecule numbers";
String categoryAxisLabel = "time";
String valueAxisLabel = "molecule numbers";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int[] eachSpecieState = new int[allStateList.size()];
for (int i =0; i<n; i++) {
for (int j=0; j<allStateList.size(); j++) {
eachSpecieState[i]=allStateList.get(j)[i];
}
}
for (int i =0; i<m;i++) {
String series[]= new String[m];
series[i]=""+strs[i];
for(int k =0; k<n;k++) {
for (int j=0; j<allStateList.size(); j++) {
eachSpecieState[k]=allStateList.get(j)[k];
dataset.addValue(eachSpecieState[k],series[i+1],""+timeList.get(j));
}
}
}
JFreeChart chart = ChartFactory.createLineChart(chartTitle,
categoryAxisLabel, valueAxisLabel, dataset);
chartPanel = new ChartPanel(chart);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StochasticProcess().setVisible(true);
}
});
然而,没有图表显示。由于我的swing语言不是很好,有人能帮我指出有什么地方出错吗?(代码的最后一个图和图表部分)谢谢~
https://stackoverflow.com/questions/51694201
复制相似问题