我学数学专业,做毕业论文使用java语言,从数值计算结果和图形两个方面验证多项式插值无限增加插值结点不具有逐点收敛性.使用的特例是龙格给的一个例子.
但是在绘图后,如何在clear按扭的响应代码区域把画的图形清除或者遮盖,而不影响下次做图.
没学过java,本来打算用matlab做的.后来觉得时间宽余,就用java做了,没系统学过java,原来看了点thinking in java.我贴出来代码,大家看看,设计的不是很好.有兴趣的朋友可以编译一下看看效果,然后帮我改改.主要是在clearButtonHandler里面该如何清除.
我自己感觉应该使用update函数,但不清楚如何做/*主窗口*/
package numbercrunch;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;class MainFrame extends JFrame{
private JFrame mainFrame;
private JPanel panel_south,panel_east;
private JTextArea outArea;
private JTextField tField1,tField2;
private JButton runButton,clearButton;
private JScrollPane scrollPane;
public MainFrame(){
mainFrame = new JFrame("Interpolation Graphics v1.0");
runButton = new JButton("Run");
clearButton = new JButton("Clear");
tField1 = new JTextField("10");
tField2 = new JTextField("100");
outArea = new JTextArea(30,15);
scrollPane = new JScrollPane(outArea);
panel_south = new JPanel();
panel_east = new JPanel();
panel_south.setLayout(new GridLayout(1,4));
panel_east.setLayout(new GridLayout(1,1));
panel_south.add(tField1);
panel_south.add(tField2);
panel_south.add(runButton);
panel_south.add(clearButton);
panel_east.add(scrollPane);
Container c = mainFrame.getContentPane();
c.add(panel_south,BorderLayout.SOUTH);
c.add(panel_east,BorderLayout.EAST);
mainFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});
RunButtonHandler eHandler = new RunButtonHandler();
runButton.addActionListener(eHandler);
ClearButtonHandler cHandler=new ClearButtonHandler();
clearButton.addActionListener(cHandler);
mainFrame.show();
}
class RunButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
Runge r=new Runge();
int n=Integer.parseInt(tField1.getText());
int m=Integer.parseInt(tField2.getText());
NewtonMethod p=new NewtonMethod(n);
for(int i=0;i<n;++i){
double temp=(double)i/(double)(n-1);
p.addDataPoint(new DataPoint(temp,r.valueAt(temp)));
}
/*result output*/
outArea.append("横坐标x\t\t"+"插值结果\t\t"+"龙格函数值\t\t"+"绝对误差\t\t"+"相对误差\n");
double y1,y2;
double temp_x;
String x_string,interpolation,runge_y,abError,reError;
for(int i=0;i<m;i++){
temp_x=(double)i/(double)m;
y1=p.valueAt(temp_x);
y2=r.valueAt(temp_x);
x_string = Double.toString(temp_x);
interpolation=Double.toString(y1);
runge_y=Double.toString(y2);
abError=Double.toString(Math.abs(y1-y2));
reError=Double.toString(100*Math.abs(y1/y2-1));
outArea.append(x_string+"\t\t"+interpolation+"\t"+
runge_y+"\t"+abError+"\t"+
reError+'\n');
} /*draw Graphics*/
Line plot=new Line(r,p,m);
Container c=mainFrame.getContentPane();
c.add(plot);
mainFrame.show();
}
}
class ClearButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
/* output result clear*/
outArea.setText("");
/* Graphics clear*/
//我不知道这里该如何处理,清除类Line的对象plot在屏幕画的图形.
}
}
public static void main(String arg[]){
new MainFrame();
}}牛顿方法类
package numbercrunch;class DataPoint{/*类DataPoint表示插值节点*/
public double x;
public double y;
public DataPoint(double x,double y){
this.x=x;
this.y=y;
}
}class NewtonMethod implements Function{ /* Function是一个仅有valueAt()方法的接口,此方法用来求函数值*/
private int n; //插值节点个数
private DataPoint data[];//插值节点数组
private double ddt[][];//差商表
public NewtonMethod(int maxPoints){
this.data=new DataPoint[maxPoints];
this.ddt=new double[data.length][data.length];
}
/* 加入新的插值节点,并修改差商表*/
public void addDataPoint(DataPoint dataPoint){
if(n>=data.length) return;
data[n]=dataPoint;
ddt[n][0]=dataPoint.y;
++n;
for(int order=1;order<n;++order){/* 计算差商*/
int bottom=n-order-1;
double numberator=ddt[bottom+1][order-1]-ddt[bottom][order-1];
double denominator=data[bottom+order].x-data[bottom].x;
ddt[bottom][order]=numberator/denominator;
}
}
/*重载valueAt()函数返回函数值*/
public double valueAt(double x){
double y=ddt[0][0];
double xFactor=1;
for(int order=1;order<n;order++){
xFactor=xFactor*(x-data[order-1].x);
y=y+xFactor*ddt[0][order];
}
return y;
}
public void reset(){ n=0;}//重置
}//画图的类
package numbercrunch;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class Line extends JComponent{ int x[];//横坐标数组
int runge_y[];//龙格函数对应x[]的纵坐标数组
int polation_y[];//牛顿插值多项式的纵坐标数组
final static int ENLARGMENT_X=600;
final static int ENLARGMENT_Y=600;//x坐标和y坐标扩大的倍数
public Line(Function r,Function p,int m){//Function r,p引用分别是龙格类的对象和牛顿类的对象
x=new int[m+1];
polation_y=new int[m+1];
runge_y=new int[m+1];
double temp_x;
for(int i=0;i<m;i++){//计算纵坐标数组
temp_x=(double)i/(double)m;
x[i]=(int)(temp_x*Line.ENLARGMENT_X);
/*java坐标系右上角为(0,0),所以需要做相应的减法*/
runge_y[i]=Line.ENLARGMENT_Y-(int)( r.valueAt(temp_x)*Line.ENLARGMENT_Y );
polation_y[i]=Line.ENLARGMENT_Y-(int)( p.valueAt(temp_x)*Line.ENLARGMENT_Y );
}
x[m]=Line.ENLARGMENT_X;
runge_y[m]=Line.ENLARGMENT_Y-(int)(r.valueAt(1)*Line.ENLARGMENT_Y);
polation_y[m]=runge_y[m]; } public void paint(Graphics g){//绘图龙格函数和插值函数
g.drawPolyline(x,runge_y,x.length);
g.drawPolyline(x,polation_y,x.length);
}
}
//function接口
package numbercrunch;
interface Function{
public double valueAt(double x);
}
但是在绘图后,如何在clear按扭的响应代码区域把画的图形清除或者遮盖,而不影响下次做图.
没学过java,本来打算用matlab做的.后来觉得时间宽余,就用java做了,没系统学过java,原来看了点thinking in java.我贴出来代码,大家看看,设计的不是很好.有兴趣的朋友可以编译一下看看效果,然后帮我改改.主要是在clearButtonHandler里面该如何清除.
我自己感觉应该使用update函数,但不清楚如何做/*主窗口*/
package numbercrunch;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;class MainFrame extends JFrame{
private JFrame mainFrame;
private JPanel panel_south,panel_east;
private JTextArea outArea;
private JTextField tField1,tField2;
private JButton runButton,clearButton;
private JScrollPane scrollPane;
public MainFrame(){
mainFrame = new JFrame("Interpolation Graphics v1.0");
runButton = new JButton("Run");
clearButton = new JButton("Clear");
tField1 = new JTextField("10");
tField2 = new JTextField("100");
outArea = new JTextArea(30,15);
scrollPane = new JScrollPane(outArea);
panel_south = new JPanel();
panel_east = new JPanel();
panel_south.setLayout(new GridLayout(1,4));
panel_east.setLayout(new GridLayout(1,1));
panel_south.add(tField1);
panel_south.add(tField2);
panel_south.add(runButton);
panel_south.add(clearButton);
panel_east.add(scrollPane);
Container c = mainFrame.getContentPane();
c.add(panel_south,BorderLayout.SOUTH);
c.add(panel_east,BorderLayout.EAST);
mainFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});
RunButtonHandler eHandler = new RunButtonHandler();
runButton.addActionListener(eHandler);
ClearButtonHandler cHandler=new ClearButtonHandler();
clearButton.addActionListener(cHandler);
mainFrame.show();
}
class RunButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
Runge r=new Runge();
int n=Integer.parseInt(tField1.getText());
int m=Integer.parseInt(tField2.getText());
NewtonMethod p=new NewtonMethod(n);
for(int i=0;i<n;++i){
double temp=(double)i/(double)(n-1);
p.addDataPoint(new DataPoint(temp,r.valueAt(temp)));
}
/*result output*/
outArea.append("横坐标x\t\t"+"插值结果\t\t"+"龙格函数值\t\t"+"绝对误差\t\t"+"相对误差\n");
double y1,y2;
double temp_x;
String x_string,interpolation,runge_y,abError,reError;
for(int i=0;i<m;i++){
temp_x=(double)i/(double)m;
y1=p.valueAt(temp_x);
y2=r.valueAt(temp_x);
x_string = Double.toString(temp_x);
interpolation=Double.toString(y1);
runge_y=Double.toString(y2);
abError=Double.toString(Math.abs(y1-y2));
reError=Double.toString(100*Math.abs(y1/y2-1));
outArea.append(x_string+"\t\t"+interpolation+"\t"+
runge_y+"\t"+abError+"\t"+
reError+'\n');
} /*draw Graphics*/
Line plot=new Line(r,p,m);
Container c=mainFrame.getContentPane();
c.add(plot);
mainFrame.show();
}
}
class ClearButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
/* output result clear*/
outArea.setText("");
/* Graphics clear*/
//我不知道这里该如何处理,清除类Line的对象plot在屏幕画的图形.
}
}
public static void main(String arg[]){
new MainFrame();
}}牛顿方法类
package numbercrunch;class DataPoint{/*类DataPoint表示插值节点*/
public double x;
public double y;
public DataPoint(double x,double y){
this.x=x;
this.y=y;
}
}class NewtonMethod implements Function{ /* Function是一个仅有valueAt()方法的接口,此方法用来求函数值*/
private int n; //插值节点个数
private DataPoint data[];//插值节点数组
private double ddt[][];//差商表
public NewtonMethod(int maxPoints){
this.data=new DataPoint[maxPoints];
this.ddt=new double[data.length][data.length];
}
/* 加入新的插值节点,并修改差商表*/
public void addDataPoint(DataPoint dataPoint){
if(n>=data.length) return;
data[n]=dataPoint;
ddt[n][0]=dataPoint.y;
++n;
for(int order=1;order<n;++order){/* 计算差商*/
int bottom=n-order-1;
double numberator=ddt[bottom+1][order-1]-ddt[bottom][order-1];
double denominator=data[bottom+order].x-data[bottom].x;
ddt[bottom][order]=numberator/denominator;
}
}
/*重载valueAt()函数返回函数值*/
public double valueAt(double x){
double y=ddt[0][0];
double xFactor=1;
for(int order=1;order<n;order++){
xFactor=xFactor*(x-data[order-1].x);
y=y+xFactor*ddt[0][order];
}
return y;
}
public void reset(){ n=0;}//重置
}//画图的类
package numbercrunch;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class Line extends JComponent{ int x[];//横坐标数组
int runge_y[];//龙格函数对应x[]的纵坐标数组
int polation_y[];//牛顿插值多项式的纵坐标数组
final static int ENLARGMENT_X=600;
final static int ENLARGMENT_Y=600;//x坐标和y坐标扩大的倍数
public Line(Function r,Function p,int m){//Function r,p引用分别是龙格类的对象和牛顿类的对象
x=new int[m+1];
polation_y=new int[m+1];
runge_y=new int[m+1];
double temp_x;
for(int i=0;i<m;i++){//计算纵坐标数组
temp_x=(double)i/(double)m;
x[i]=(int)(temp_x*Line.ENLARGMENT_X);
/*java坐标系右上角为(0,0),所以需要做相应的减法*/
runge_y[i]=Line.ENLARGMENT_Y-(int)( r.valueAt(temp_x)*Line.ENLARGMENT_Y );
polation_y[i]=Line.ENLARGMENT_Y-(int)( p.valueAt(temp_x)*Line.ENLARGMENT_Y );
}
x[m]=Line.ENLARGMENT_X;
runge_y[m]=Line.ENLARGMENT_Y-(int)(r.valueAt(1)*Line.ENLARGMENT_Y);
polation_y[m]=runge_y[m]; } public void paint(Graphics g){//绘图龙格函数和插值函数
g.drawPolyline(x,runge_y,x.length);
g.drawPolyline(x,polation_y,x.length);
}
}
//function接口
package numbercrunch;
interface Function{
public double valueAt(double x);
}
解决方案 »
- hibernate 根据占位符设值
- JAVA中学习GUI重要吗?
- java中的throw和throws的区别
- 各位高手看看我从书上抄的一个程序.编译过了.运行不过.
- 关于jProgressBar的一个奇怪问题
- java开发从何开始?
- 怎么产生随机数?
- 求助~AIX-UNIX下,java程序中如何调用一个shell命令?比如whoamI,并返回值~ 谢谢高手帮忙啊
- 用jsp输出文本文件,文件中文本的换行问题?
- 学完一部分李刚的疯狂JAVA,写代码总是那么低端,愁啊
- 如何设转置TextField默认焦点?如设置转移焦点的键啊,比如默认是TAB,我把它换成回车键改变焦点?
- 如何实现敲回车切换到下一个JTextField
[email protected]
outArea.setText("");
mainFrame.getContentPane().remove(2);
mainFrame.show();
mainFrame.repaint();