ritchie888
ritchie888

Reputation: 583

Plot update error - JMathPlot

I'm using the JMathPlot library to produce a simple graph, in this case it's a 3D which is updated for every iteration in the for loop. However, the speed in which it loops I'm getting:

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

and

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

Which to my understanding is something to do with the AWT thread and the main thread not getting along. I know that I need to update the graphic thread in a special way, just not sure how. Here is my code, if anyone could suggest how I update the plot (repaint, I guess) without the error, that would be great.

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;
}
}

Upvotes: 2

Views: 1429

Answers (2)

dikey
dikey

Reputation: 21

If you need view the result after each interation, pause the Thread for while in end for plot other result:

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
    }

}

Upvotes: 0

bchetty
bchetty

Reputation: 2241

You can move the 2 code statements into the for loop and try it:

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);

}

EDIT: When using the same frame, you have to use the standard way of refreshing it's content (invalidate and validate).

Upvotes: 0

Related Questions