2012-02-09 7 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 (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:同じフレームを使用している場合、あなたはさわやかの標準的な方法を使用する必要があり、それはコンテンツ(無効だが検証する)。

+0

ご回答いただきありがとうございます。しかし、私が投稿したやり方で試してみる前に、試してみましたが、各プロットで新しいPlot3Dパネルを作成しなくても、同じプロットを 'リフレッシュ'したいと思っていました。それは実現可能ではないようですか?乾杯。 – ritchie888

関連する問題