📄 threeinone.java
字号:
package mypack;
import java.awt.*;
import java.awt.event.*;
public class ThreeInOne
{
public static void main(String args[])
{
Three t=new Three();
t.display();
}
}
class _paint extends Panel implements ActionListener
{
public int p_count=0,flag=0;
int point[][]=new int[100][2];
double h[],ath[],c[],b[],d[],s2[],s1;
double a[];
int tempx,tempy;
public _paint()
{
p_count=0;
this.addMouseListener(new IsMouseClicked());
}
///////////////////////////
public void p_random()
{
Graphics g = getGraphics();
g.setColor(Color.black);
int i,set=0,j,flag=0;
//p_count=10;
for(i=0;i<6;i++)
{
set=0;
while(set==0)
{
System.out.println("1_");
tempx=(int)(Math.random()*1000);
if(tempx>=50&&tempx<=690)
{
point[p_count][0]=tempx-401;
set=1;
}
}
set=0;
while(set==0)
{
System.out.println("2_");
tempy=(int)(Math.random()*1000);
if(tempy>=200&&tempy<=300)
{
point[p_count][1]=-tempy+281;
set=1;
}
}
System.out.println("3_");
p_count++;
g.fillOval(tempx,tempy,5,5);
}
}
//////////////////////////////
public void paint(Graphics g)
{
g.setColor(Color.black);
Font fntName=new Font("Serif",0,40);
g.setFont(fntName);
g.drawString("y",378,50);
g.drawString("↑",381,50);
g.drawRect(400,50,1,450);
g.drawRect(40,280,700,1);
g.drawString("→",720,295);
g.drawString("x",730,305);
g.setFont(new Font("Serif",2,25));
g.drawString("0",400,300);
}
/////////////////////////////////////
public void drawLagrange()
{
Graphics g = getGraphics();
Color color=Color.red;
g.setColor(color);
if(p_count>1)
{
for(int x=point[0][0];x<=point[p_count-1][0];x++)
{
double t=1,s=0;
for(int k=0;k<p_count;k++)
{
t=1;
for(int i=0;i<p_count;i++)
{
if(i!=k&&point[k][0]!=point[i][0])
{
t*=(double)(x-point[i][0])/(point[k][0]-point[i][0]);
}
}
s+=t*point[k][1];
}
g.fillOval(x+401,281-(int)s,3,3);
}
}
}
///////////////////////////////////////
public void setAarray()
{
a=new double[p_count];
int n=p_count-1;
for(int ext=0;ext<=n;ext++)
{
double s=0;
double p;
for(int k=0;k<=ext;k++)
{
p=point[k][1];
for(int i=0;i<=ext;i++)
{
if(i!=k)
{
p/=(double)(point[k][0]-point[i][0]);
}
}
s+=p;
}
a[ext]=s;
}
}
public void drawNewton()
{
int m=0;
Graphics g = getGraphics();
Color color=Color.blue;
g.setColor(color);
if(p_count>=1)
{ m=0;
for(int x=point[0][0];x<=point[p_count-1][0];x++)
{
double p=a[p_count-1];
for(int i=p_count-2;i>=0;i--)
{
if(x!=point[i][0])
p=p*(x-point[i][0])+a[i];
}
if(x==point[m][0]&&m<p_count)
{
g.fillOval(x+401,281-point[m][1],3,3);
m++;
//System.out.println("oo");
}
else
g.fillOval(x+401,281-(int)p,3,3);
}
}
}
///////////////
public void setParameters() //prepare for the threeSpline
{
//distribute space for the arrays
h=new double[p_count];
ath=new double[p_count];
c=new double[p_count];
d=new double[p_count];
b=new double[p_count];
s2=new double[p_count];
int k,n=p_count-1;
for(k=0;k<=n-1;k++) //step 2
{
h[k]=point[k+1][0]-point[k][0];
}
ath[1]=2*(h[0]+h[1]); //step 3
for(k=2;k<=n-1;k++) //step 4
{
ath[k]=2*(h[k-1]+h[k])-h[k-1]*h[k-1]/ath[k-1];
}
for(k=1;k<=n;k++) //step 5
{
c[k]=(point[k][1]-point[k-1][1])/h[k-1];
}
for(k=1;k<=n-1;k++) //step 6
{
d[k]=6*(c[k+1]-c[k]);
}
b[1]=d[1]; //step 7
for(k=2;k<=n-1;k++) //step 8
{
b[k]=d[k]-b[k-1]*h[k-1]/ath[k-1];
}
s2[n-1]=b[n-1]/ath[n-1]; //step 9
for(k=n-2;k>0;k--) //step 10
{
s2[k]=(b[k]-h[k]*s2[k+1])/ath[k];
}
s2[0]=0;s2[n]=0; //step 11
}
public void drawThreeSpline()
{
int k,n=p_count-1; //loop veriable
double s; //final result
Graphics g = getGraphics();
Color color=Color.yellow;
g.setColor(color);
if(p_count>1)
{
for(int x=point[0][0];x<=point[p_count-1][0];x++)
{
for(k=0;k<n;k++) //step 12.2
{
if(x<=point[k+1][0])
break;
}
if(k>=p_count)
{
System.out.println("Error Exit!!!");
return ;
}
s1=c[k+1]-s2[k+1]*h[k]/6-s2[k]*h[k]/3; //step 12.4
s=point[k][1]+s1*(x-point[k][0])+s2[k]*(x-point[k][0])*(x-point[k][0])/2+(s2[k+1]-s2[k])*(x-point[k][0])*(x-point[k][0])*(x-point[k][0])/(6*h[k]); //step 12.5
g.fillOval(x+401,281-(int)s,3,3);
}//for every point x
}
}
////////////////////////////////
public void clear()
{
p_count=0;
Graphics g = getGraphics();
Color color=Color.white;
update(g);
}
/////////////////
public void sequence()
{
int j;
int tempX,tempY;
for(int i=0;i<p_count-1;i++)
{
for(j=0;j<(p_count-1)-i;j++)
if(point[j][0]>point[j+1][0])
{
tempX=point[j][0];tempY=point[j][1];point[j][0]=point[j+1][0];point[j][1]=point[j+1][1];
point[j+1][0]=tempX;point[j+1][1]=tempY;
}
}
}
//////////////////////////
public void actionPerformed(ActionEvent e)
{}
public class IsMouseClicked extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
Graphics g = getGraphics();
point[p_count][0] = e.getX()-401;
point[p_count][1] = -e.getY()+281;
g.fillOval((int)e.getX(),(int)e.getY(),5,5);
p_count++;
}
}
}
class Three extends WindowAdapter implements ActionListener
{
Frame f;
Panel p1,p2,p3;
Button b1,b2,b3,b4,b5;
_paint p;
public void display()
{
f=new Frame("数值算法");
f.setSize(800,600); //设置框架的大小
f.setResizable(false); //框架不能改变大小
f.setBackground(Color.gray);
f.setLayout(new BorderLayout());
p1=new Panel();
p=new _paint();
p.setBackground(Color.white);
b1=new Button("随机产生点");
b2=new Button("拉格朗日插值");
b2.setBackground(Color.red);
b3=new Button(" 牛顿插值 ");
b3.setBackground(Color.blue);
b4=new Button("三次样条插值");
b4.setBackground(Color.yellow);
b5=new Button(" 清空 ");
f.add(p1,BorderLayout.SOUTH);
f.add(p,BorderLayout.CENTER);
f.addWindowListener(this);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
p.p_random();
}
if(e.getSource()==b2)
{
if(p.p_count>1)
{
p.sequence();
p.drawLagrange();
}
}
if(e.getSource()==b3)
{
if(p.p_count>1)
{
p.sequence();
p.setAarray();
p.drawNewton();
}
}
if(e.getSource()==b4)
{
if(p.p_count>1)
{
p.sequence();
p.setParameters();
p.drawThreeSpline();
}
}
if(e.getSource()==b5)
{
p.clear();
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -