2012-02-09 61 views
2

我正在使用JMathPlot庫來生成一個簡單的圖形,在這種情況下,它是一個爲for循環中的每個迭代更新的3D。然而,速度在其中循環我越來越:繪圖更新錯誤 - JMathPlot

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

回答

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

} 

編輯:當使用相同的框架,你必須使用刷新它的內容的標準方式(無效並驗證)。

+0

感謝您的回覆。我嘗試過以我發佈的方式嘗試它,但是,我想避免在每次迭代時創建一個新的Plot3D面板,只是想'刷新'相同的情節。這似乎不可行嗎?乾杯。 – ritchie888 2012-02-14 14:48:38