绘图更新错误 - JMathPlot

时间:2012-02-09 15:38:56

标签: java graphics repaint jmathplot

我使用JMathPlot库生成一个简单的图形,在这种情况下,它是一个3D,它为for循环中的每次迭代更新。然而,它的循环速度我得到了:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException

我的理解是与AWT线程和主线程无关。我知道我需要以特殊的方式更新图形线程,只是不确定如何。这是我的代码,如果有人可以建议我如何更新情节(重绘,我猜)没有错误,这将是伟大的。

import javax.swing.*;
import org.math.plot.*;
import static java.lang.Math.*;
import static org.math.array.DoubleArray.*;

public class GridPlotsExample {

public static void main(String[] args) throws InterruptedException {

    JFrame frame = new JFrame("a plot panel");
    frame.setSize(600, 600);
    frame.setVisible(true);

    // create your PlotPanel (you can use it as a JPanel) with a legend
    // at SOUTH
    Plot3DPanel plot = new Plot3DPanel("SOUTH");

    frame.setContentPane(plot);

    for (int i = 1; i < 10; i++) {

        // define your data
        double[] x = increment(0.0, 0.1, i); // x = 0.0:0.1:1.0
        double[] y = increment(0.0, 0.05, i);// y = 0.0:0.05:1.0
        double[][] z1 = f1(x, y);
        double[][] z2 = f2(x, y);

        // add grid plot to the PlotPanel
        plot.addGridPlot("z=cos(PI*x)*sin(PI*y)", x, y, z1);
        plot.addGridPlot("z=sin(PI*x)*cos(PI*y)", x, y, z2);

    }

}

// function definition: z=cos(PI*x)*sin(PI*y)
public static double f1(double x, double y) {
    double z = cos(x * PI) * sin(y * PI);
    return z;
}

// grid version of the function
public static double[][] f1(double[] x, double[] y) {
    double[][] z = new double[y.length][x.length];
    for (int i = 0; i < x.length; i++)
        for (int j = 0; j < y.length; j++)
            z[j][i] = f1(x[i], y[j]);
    return z;
}

// another function definition: z=sin(PI*x)*cos(PI*y)
public static double f2(double x, double y) {
    double z = sin(x * PI) * cos(y * PI);
    return z;
}

// grid version of the function
public static double[][] f2(double[] x, double[] y) {
    double[][] z = new double[y.length][x.length];
    for (int i = 0; i < x.length; i++)
        for (int j = 0; j < y.length; j++)
            z[j][i] = f2(x[i], y[j]);
    return z;
}
}

2 个答案:

答案 0 :(得分:0)

您可以将2个代码语句移动到for循环中并尝试:

for (int i = 1; i < 10; i++) {
    Plot3DPanel plot = new Plot3DPanel("SOUTH");
    frame.setContentPane(plot);

    // define your data
    double[] x = increment(0.0, 0.1, i); // x = 0.0:0.1:1.0
    double[] y = increment(0.0, 0.05, i);// y = 0.0:0.05:1.0
    double[][] z1 = f1(x, y);
    double[][] z2 = f2(x, y);

    // add grid plot to the PlotPanel
    plot.addGridPlot("z=cos(PI*x)*sin(PI*y)", x, y, z1);
    plot.addGridPlot("z=sin(PI*x)*cos(PI*y)", x, y, z2);

}

编辑:使用相同的框架时,您必须使用标准方式刷新其内容(无效并验证)。

答案 1 :(得分:0)

如果您需要在每次交互后查看结果,请暂停Thread一段时间,以绘制其他结果:

for (int i = 1; i < 10; i++) {

    // define your data
    double[] x = increment(0.0, 0.1, i); // x = 0.0:0.1:1.0
    double[] y = increment(0.0, 0.05, i);// y = 0.0:0.05:1.0
    double[][] z1 = f1(x, y);
    double[][] z2 = f2(x, y);

    // add grid plot to the PlotPanel
    plot.addGridPlot("z=cos(PI*x)*sin(PI*y)", x, y, z1);
    plot.addGridPlot("z=sin(PI*x)*cos(PI*y)", x, y, z2);

    try {
      // Pause 1 ms
       Thread.sleep(1000);        
    } catch (InterruptedException e) {
      // TODO: handle exception
    }

}