📄 drawuseleastsquare.java
字号:
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class drawUseLeastSquare extends JFrame implements ActionListener
{
static Scanner obj=new Scanner(System.in);
static int windowSizeX=1024, windowSizeY=768, x0=400, y0=500, m, i;
static double range1, range2, Headway, size=1;
static double[][] t, arrayA;
static double[] x, y, s, sy, a;
static drawUseLeastSquare g;
static Graphics2D g2;
static Container mainContainer;
static Color[] color=new Color[216+1];
static JScrollPane scrollPane;
String[] strLabel={"M=","X Range:","~","Headway:","ViewSize","%"};
JLabel[] label=new JLabel[strLabel.length];
String[] strItem={"Load","Exit"};
JMenuItem[] item=new JMenuItem[strItem.length];
String[] strButton={"Draw","Clear","Resize"};
JButton[] button=new JButton[strButton.length];
String[] strInit={"1","-10.0","10.0","0.01","100"};
JTextField[] text=new JTextField[5];
public mainPaintCanvas mpc;
public subPaintCanvas spc;
JPanel mainPanel;
Dimension mainSize=new Dimension(windowSizeX, windowSizeY);
Font font=new Font("",Font.PLAIN,14);
static Vector<Integer> tempM=new Vector<Integer>();
public drawUseLeastSquare()
{
mpc=new mainPaintCanvas();
spc=new subPaintCanvas();
spc.setFont(font);
setTitle("Java ø");
setSize(windowSizeX,windowSizeY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
mainContainer=getContentPane();
mainContainer.setLayout(new BorderLayout());
mainContainer.setBackground(Color.WHITE);
JMenuBar bar=new JMenuBar();
JMenu menu=new JMenu("File");
menu.getPopupMenu().setLightWeightPopupEnabled(false);
for(int i=0;i<strItem.length;i++)
{
item[i]=new JMenuItem(strItem[i]);
menu.add(item[i]);
item[i].addActionListener(this);
}
bar.add(menu);
for(int i=0;i<strLabel.length-2;i++)
{
label[i]=new JLabel(strLabel[i]);
bar.add(label[i]);
text[i]=new JTextField(strInit[i],3);
bar.add(text[i]);
}
for(int i=0;i<strButton.length-1;i++)
{
button[i]=new JButton(strButton[i]);
bar.add(button[i]);
button[i].addActionListener(this);
}
label[4]=new JLabel(strLabel[4]);
bar.add(label[4]);
text[4]=new JTextField(strInit[4],2);
bar.add(text[4]);
label[5]=new JLabel(strLabel[5]);
bar.add(label[5]);
button[2]=new JButton(strButton[2]);
bar.add(button[2]);
button[2].addActionListener(this);
mainContainer.add(bar, BorderLayout.NORTH);
mpc.setBackground(Color.WHITE);
mpc.setPreferredSize(mainSize);
mainPanel=new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(mpc, BorderLayout.CENTER);
spc.setPreferredSize(new Dimension(windowSizeX, 50));
mainPanel.add(spc, BorderLayout.SOUTH);
mainPanel.setBackground(Color.WHITE);
scrollPane=new JScrollPane(mainPanel);
scrollPane.setSize(windowSizeX,windowSizeY);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setViewportView(mainPanel);
mainContainer.add(scrollPane, BorderLayout.CENTER);
setVisible(true);
g2=(Graphics2D)getGraphics();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Load"))
{
tempM.removeAllElements();
mainPanel.repaint();
FileDialog flog=new FileDialog(g,"Load file",0);
flog.setVisible(true);
String name=flog.getDirectory()+flog.getFile();
File Obj=new File(name);
BufferedReader fileInput;
try
{
i=0;
fileInput=new BufferedReader(new FileReader(Obj));
String fl=fileInput.readLine();
while(true)
{
fl=fileInput.readLine();
if(fl!=null)
{
i++;
System.out.println("Line"+i+":"+fl);
}
else
break;
}
System.out.println("i:"+i);
fileInput.close();
x = new double [i];
y = new double [i];
fileInput=new BufferedReader(new FileReader(Obj));
fl=fileInput.readLine();
for(int j=0;j<i;j++)
{
fl=fileInput.readLine();
if(fl!=null)
{
StringTokenizer to=new StringTokenizer(fl);
String s=to.nextToken();
x[j] = Double.parseDouble(s);
s=to.nextToken();
y[j] = Double.parseDouble(s);
System.out.println("x"+j+"="+x[j]+" y"+j+"="+y[j]);
}
else
break;
}
fileInput.close();
mpc.repaint();
}
catch(IOException i)
{
}
}
if(e.getActionCommand().equals("Exit"))
{
dispose();
System.exit(0);
}
if(e.getActionCommand().equals("Draw"))
{
m=Integer.parseInt(text[0].getText());
range1=Double.parseDouble(text[1].getText());
range2=Double.parseDouble(text[2].getText());
Headway=Double.parseDouble(text[3].getText());
boolean exist=false;
for(int cm=0;cm<tempM.size();cm++)
{
if(m==tempM.elementAt(cm))
exist=true;
}
if(exist!=true)
tempM.addElement(m);
mainPanel.repaint();
}
if(e.getActionCommand().equals("Clear"))
{
tempM.removeAllElements();
mainPanel.repaint();
}
if(e.getActionCommand().equals("Resize"))
{
size=Double.parseDouble(text[4].getText())*0.01;
mainSize.setSize(windowSizeX*size, windowSizeY*size);
mainPanel.setPreferredSize(mainSize);
scrollPane.setViewportView(mainPanel);
mpc.repaint();
}
}
public static void main(String args[])
{
defineColor();
g = new drawUseLeastSquare();
}
public static void defineColor()
{
int c1=0,c2=1,c3=1,t=51;
for(int c=1;c<217;c++)
{
int r=255,g=255,b=255;
if(c>=Math.pow((255/t+1),2)*c3)
{
c2=1;
if(c!=216)
c3++;
g=255;
}
double sg=(255/t+1)*c2+Math.pow((255/t+1),2)*(c3-1);
if(c>=sg)
{
c2++;
if(c==216)
c2=6;
r=255;
}
color[c]=new Color(r-t*c1,g-t*(c2-1),b-t*(c3-1));
if(c1<5)
c1++;
else
c1=0;
}
}
public static double locateX(double x)
{
x=x*2+x0;
return x;
}
public static int locateX(int x)
{
x=x*2+x0;
return x;
}
public static double locateY(double y)
{
y=y*(-2)+y0;
return y;
}
public static int locateY(int y)
{
y=y*(-2)+y0;
return y;
}
public static double P(double x)
{
double p=a[m];
for(int k=m-1;k>=0;k--)
p*=x+a[k];
return p;
}
public static void inputLeastSquareData()
{
t = new double[m+1][m+1];
s = new double [2*m+1];
sy = new double [m+1];
a = new double [m+1];
arrayA = new double [m+1][m+2];
for(int k=1;k<=2*m;k++)
for(int j=0;j<i;j++)
{
s[k]+=Math.pow(x[j],k);
}
for(int k=0;k<=m;k++)
for(int j=0;j<i;j++)
{
sy[k]+=y[j]*Math.pow(x[j],k);
}
for(int a=0;a<m+1;a++)
for(int b=0;b<m+1;b++)
{
if(a==0 && b==0)
t[a][b]=i;
else
t[a][b]=s[a+b];
}
for(int a=0;a<m+1;a++)
{
for(int b=0;b<m+1;b++)
{
System.out.print(t[a][b]+" ");
}
System.out.println(" ");
}
}
public static void Gauss(double[][] t,double[] sy,int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<=n;j++)
{
if(j==m+1)
arrayA[i][j]=sy[i];
else
arrayA[i][j]=t[i][j];
}
for(int i=0;i<n;i++)
{
for(int j=0;j<=n;j++)
{
System.out.print("arrayA["+i+"]["+j+"]="+arrayA[i][j]+" ");
}
System.out.println("");
}
for(int k=0;k<n;k++)
{
partialPivoting(arrayA,k,k,n);
for(int i=k+1;i<n;i++)
{
double d=arrayA[i][k]/arrayA[k][k];
for(int j=k;j<=n;j++)
{
arrayA[i][j]-=arrayA[k][j]*d;
}
}
}
System.out.println("After");
for(int i=0;i<n;i++)
{
for(int j=0;j<=n;j++)
{
System.out.print("arrayA["+i+"]["+j+"]="+arrayA[i][j]+" ");
}
System.out.println("");
}
a=compute(arrayA,n);
for(int i=0;i<n;i++)
{
System.out.println("a["+i+"]="+a[i]+" ");
}
}
public static void partialPivoting(double[][] a,int current,int k, int n)
{
int max=current;
double temp=a[current][k];
for(int i=current;i<n;i++)
{
if(a[i][k]>temp)
max=i;
}
swap(a,current,max,n);
}
public static void swap(double[][] a,int current, int max, int n)
{
double[] temp=new double[n+1];
for(int i=0;i<=n;i++)
{
temp[i]=a[current][i];
a[current][i]=a[max][i];
a[max][i]=temp[i];
}
}
public static double[] compute(double[][] a,int n)
{
double[] x=new double[n];
double sum=0;
for(int i=n-1;i>=0;i--)
{
sum=0;
for(int j=i+1;j<n;j++)
sum+=a[i][j]*x[j];
x[i]=(a[i][n]-sum)/a[i][i];
}
return x;
}
class mainPaintCanvas extends JComponent
{
public void paint(Graphics g)
{
g2=(Graphics2D)g;
g2.setColor(color[0]);
g2.scale(size,size);
g2.draw(new Line2D.Double(locateX(-2000*size),locateY(0),locateX(2000*size),locateY(0)));
g2.draw(new Line2D.Double(locateX(0),locateY(-1500*size),locateX(0),locateY(1500*size)));
g2.drawString("(0,0)",locateX(0),locateY(0));
for(int cm=0;cm<i;cm++)
{
g2.fill(new Ellipse2D.Double(locateX(x[cm]),locateY(y[cm]),3,3));
}
for(int cm=0;cm<tempM.size();cm++)
{
m=tempM.elementAt(cm);
inputLeastSquareData();
Gauss(t,sy, m+1);
g2.setColor(color[m+4]);
for(double i=range1;i<=range2;i+=Headway)
{
g2.fill(new Ellipse2D.Double(locateX(i),locateY(P(i)),1.5,1.5));
}
}
}
}
class subPaintCanvas extends JLabel
{
public void paint(Graphics g)
{
g2 = (Graphics2D)g;
int lx=10,ly=10;
for(int cm=0;cm<tempM.size();cm++)
{
m=tempM.elementAt(cm);
g2.setColor(color[m+4]);
g2.drawString("M"+Integer.toString(m)+" : -----",lx,ly);
lx+=80;
if(lx>=spc.getWidth())
{
lx=10;
ly+=20;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -