Java 打印更新错误-JMathPlot
我正在使用JMathPlot库生成一个简单的图形,在本例中,它是一个3D图形,在for循环中每次迭代都会更新它。但是,我得到的循环速度:Java 打印更新错误-JMathPlot,java,graphics,repaint,jmathplot,Java,Graphics,Repaint,Jmathplot,我正在使用JMathPlot库生成一个简单的图形,在本例中,它是一个3D图形,在for循环中每次迭代都会更新它。但是,我得到的循环速度: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 及 据我所知,这与AWT线程和主线程不协调有关。我知道我需要以一种特殊的方式更新图形线程,只是不确定如何更新。这是我的代码,如果有人能建议我如何在没有错误的情况下更新绘图(我想是重新绘制),那就太好了 import ja
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
及
据我所知,这与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;
}
}
import javax.swing.*;
导入org.math.plot.*;
导入静态java.lang.Math.*;
导入静态org.math.array.DoubleArray.*;
公共类GridPlotsExample{
公共静态void main(字符串[]args)引发InterruptedException{
JFrame=新的JFrame(“绘图面板”);
框架。设置尺寸(600600);
frame.setVisible(true);
//使用图例创建PlotPanel(可以将其用作JPanel)
//在南方
Plot3DPanel plot=新Plot3DPanel(“南”);
frame.setContentPane(绘图);
对于(int i=1;i<10;i++){
//定义您的数据
double[]x=增量(0.0,0.1,i);//x=0.0:0.1:1.0
double[]y=增量(0.0,0.05,i);//y=0.0:0.05:1.0
双[]z1=f1(x,y);
双[][]z2=f2(x,y);
//将栅格打印添加到“绘图”面板
plot.addGridPlot(“z=cos(PI*x)*sin(PI*y)”,x,y,z1);
plot.addGridPlot(“z=sin(PI*x)*cos(PI*y)”,x,y,z2);
}
}
//函数定义:z=cos(PI*x)*sin(PI*y)
公共静态双f1(双x,双y){
双z=cos(x*PI)*sin(y*PI);
返回z;
}
//函数的网格版本
公共静态双[]f1(双[]x,双[]y){
双精度[][]z=新双精度[y.length][x.length];
对于(int i=0;i
您可以将这两条代码语句移动到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);
}
for(int i=1;i<10;i++){
Plot3DPanel plot=新Plot3DPanel(“南”);
frame.setContentPane(绘图);
//定义您的数据
double[]x=增量(0.0,0.1,i);//x=0.0:0.1:1.0
double[]y=增量(0.0,0.05,i);//y=0.0:0.05:1.0
双[]z1=f1(x,y);
双[][]z2=f2(x,y);
//将栅格打印添加到“绘图”面板
plot.addGridPlot(“z=cos(PI*x)*sin(PI*y)”,x,y,z1);
plot.addGridPlot(“z=sin(PI*x)*cos(PI*y)”,x,y,z2);
}
编辑:使用同一帧时,必须使用刷新其内容的标准方式(作废和验证)。您可以将两条代码语句移动到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);
}
for(int i=1;i<10;i++){
Plot3DPanel plot=新Plot3DPanel(“南”);
frame.setContentPane(绘图);
//定义您的数据
double[]x=增量(0.0,0.1,i);//x=0.0:0.1:1.0
double[]y=增量(0.0,0.05,i);//y=0.0:0.05:1.0
双[]z1=f1(x,y);
双[][]z2=f2(x,y);
//将栅格打印添加到“绘图”面板
plot.addGridPlot(“z=cos(PI*x)*sin(PI*y)”,x,y,z1);
plot.addGridPlot(“z=sin(PI*x)*cos(PI*y)”,x,y,z2);
}
编辑:使用同一帧时,必须使用刷新其内容的标准方式(无效和验证)。如果需要在每次交互后查看结果,请暂停线程,然后绘制其他结果:
for(int i=1;i<10;i++){
//定义您的数据
double[]x=增量(0.0,0.1,i);//x=0.0:0.1:1.0
double[]y=增量(0.0,0.05,i);//y=0.0:0.05:1.0
双[]z1=f1(x,y);
双[][]z2=f2(x,y);
//将栅格打印添加到“绘图”面板
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毫秒
睡眠(1000);
}捕捉(中断异常e){
//TODO:处理异常
}
}
如果您需要在每次交互后查看结果,请暂停线程一段时间以绘制其他结果:
for(int i=1;i<10;i++){
//定义您的数据
double[]x=增量(0.0,0.1,i);//x=0.0:0.1:1.0
double[]y=增量(0.0,0.05,i);//y=0.0:0.05:1.0
双[]z1=f1(x,y);
双[][]z2=f2(x,y);
//将栅格打印添加到“绘图”面板
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毫秒
睡眠(1000);
}捕捉(中断异常e){
//TODO:处理异常
}
}
谢谢你的回复。在以我发布的方式尝试之前,我已经尝试过了,但是,我想避免在每次迭代中创建一个新的Plot3D面板,只是想“刷新”相同的绘图。这似乎不可行吗?干杯,谢谢你的回复。在以我发布的方式尝试之前,我已经尝试过了,但是,我想避免在每次迭代中创建一个新的Plot3D面板,只是想“刷新”相同的绘图。这似乎不可行吗?干杯
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
}
}