package applet; import java.awt.*; import java.awt.event.*; public class PolyApplet extends java.applet.Applet implements ActionListener, ItemListener, MouseListener//, Runnable { //COLORS Color colGlob = new Color(200,170,150); Color colGloblight = new Color(240,220,200); Color colButton = new Color(176,176,176); Color colMaroon = new Color(128,0,0); Color colNavy = new Color(0,0,128); Color colPale = new Color(255,255,220); Color colPanel = new Color(230,195,172); //FONTS Font fontS11 = new Font("System", Font.PLAIN, 11); Font fontSB11 = new Font("System", Font.BOLD, 11); Font fontS12 = new Font("System", Font.PLAIN, 12); Font fontSB12 = new Font("System", Font.BOLD, 12); Font fontA11 = new Font("Arial", Font.PLAIN, 11); Font fontA10 = new Font("Arial", Font.PLAIN, 10); List list1 = new List(11, false); TextField edit1 = new TextField("1", 2); TextField edit2 = new TextField("", 4); TextField edit3 = new TextField("", 4); Button butDec = new Button("Decr"); Button butInc = new Button("Incr"); Button butClear = new Button("CLEAR"); //VARIABLES final int X0=23, Y0=38, DX0=290, DY0=290, Nmax=200, Dmax=12; int Np=0, Dm=1; int Fun=1; double Ccoef, SumSq; double [] XVa = new double[Nmax+1]; double [] YVa = new double[Nmax+1]; double [] A = new double[Nmax+1]; boolean Frun=true, Fplot; //=================================================================== public void paint(Graphics g) { PseudoPanels(); ClearPlot(); DrawGrid(); } //=================================================================== public void init() { addMouseListener(this); setLayout(null); setBackground(colGlob); // ....... title+copyright ........ Label LabTitle = new Label("Least-Squares Polynomial Appoximation"); Label LabCopyR = new Label(" Designed by C. E. Efstathiou, Department of Chemistry, University of Athens, GREECE"); add(LabTitle,0,0,480,20,fontSB12,colNavy); LabTitle.setForeground(Color.white); add(LabCopyR,0,350,480,20,fontS11,colGlob); LabCopyR.setForeground(colGloblight); int Y=26; Label LabelSel = new Label("Polynomial degree",Label.CENTER); add(LabelSel,330,Y,143,16,fontSB11,colPanel); LabelSel.setForeground(colMaroon); add(butDec,338,Y+17,34,21,fontS12,colButton); butDec.addActionListener(this); add(butInc,431,Y+17,34,21,fontS12,colButton); butInc.addActionListener(this); add(edit1,382,Y+17,39,21,fontS11,colGloblight); edit1.setEditable(false); Y=70; Label Lab2a = new Label("Coefficient of",Label.CENTER); Label Lab2b = new Label("determination",Label.CENTER); add(Lab2a,333,Y-1,78,12,fontS11,colPanel); add(Lab2b,333,Y+10,78,12,fontS11,colPanel); Lab2a.setForeground(colMaroon); Lab2b.setForeground(colMaroon); add(edit2,412,Y,53,21,fontS11,colGloblight); edit2.setEditable(false); Y=95; Label Lab3a = new Label("Sum of squared",Label.CENTER); Label Lab3b = new Label("residuals",Label.CENTER); add(Lab3a,333,Y-1,78,12,fontS11,colPanel); add(Lab3b,333,Y+10,78,12,fontS11,colPanel); Lab3a.setForeground(colMaroon); Lab3b.setForeground(colMaroon); add(edit3,412,Y,53,21,fontS11,colGloblight); edit3.setEditable(false); add(butClear,366,330,72,19,fontS12,colButton); butClear.addActionListener(this); add(list1,337,131,128,190,new Font("Courier",Font.PLAIN,11),colPanel); list1.setBackground(colPale); } //=================================================================== void add(Component c, int x, int y, int w, int h, Font f, Color b) { // helper method to add components c.setBounds(x, y, w, h); c.setFont(f); c.setBackground(b); c.setForeground(Color.black); add(c); } //=================================================================== void PseudoPanels() { Graphics g = getGraphics(); g.setColor(colPanel); g.fillRect(6,26,DX0+30,DY0+31); g.fillRect(330,26,143,96); g.fillRect(330,126,143,200); } //=================================================================== void ClearPlot() { Graphics g = getGraphics(); g.setColor(new Color(225,255,255)); g.fillRect(X0,Y0+1,DX0+1,DY0-1); } //=================================================================== void DrawGrid() { Graphics g = getGraphics(); g.setFont(fontA11); FontMetrics FM0 = g.getFontMetrics(); int I,X,Y; g.drawRect(X0-1,Y0,DX0+2,DY0); int ofsX,ofsY,e; String stX="", stY=""; for (I=0; I<=10; I++) { stX=""+I; stY=""+(10-I); ofsX=FM0.stringWidth(stX); ofsY=FM0.stringWidth(stY); X=X0+I*29; Y=Y0+I*29; g.setColor(Color.black); if ((I>0)&&(I<10))g.drawLine(X,Y0+DY0,X,Y0+DY0-5); if ((I>0)&&(I<10)) g.drawLine(X0,Y,X0+4,Y); if (I==10) e=6; else e=0; g.setColor(colNavy); if (I>0) g.drawString(stX,X-ofsX/2+1,Y0+DY0+12); g.drawString(stY,X0-3-ofsY,Y+4+e); } } //=================================================================== void DrawPoint(int x, int y, Color col) { Graphics g = getGraphics(); g.setColor(col); g.fillRect(x-2,y-2,4,4); } //=================================================================== double GetValueX(int Xp) { return (Xp-X0)*10.0/(double)DX0 + 0.10*Math.random()- 0.05; } //=================================================================== double GetValueY(int Yp) { return (Y0+DY0-Yp)*10.0 /(double)DY0 + 0.10*Math.random()- 0.05; } //=================================================================== Point GetPhysPos(double Xv, double Yv) { return new Point(X0+(int)Math.round(Xv*DX0/10), Y0+DY0-(int)Math.round(Yv*DY0/10)); } //=================================================================== double CalcY(double X) { double Pr,Sum; int I,J; Sum=A[0]; for (I=1; I<=Dm; I++) { Pr=1; for (J=1; J<=I; J++) Pr*=X; Sum+=A[I]*Pr; } return Sum; } //=================================================================== void DrawCurve() { double Xr,Yr; int I,Xp,Yp, oldX=0, oldY=0; Graphics g = getGraphics(); g.setColor(Color.black); for (I=0; I<=DX0; I++) { Xr=10.0*I/(double)DX0; Yr=CalcY(Xr); Point p=GetPhysPos(Xr,Yr); Xp=p.x; Yp=p.y; if (YpY0+DY0) Yp=Y0+DY0; if (I!=0) g.drawLine(oldX,oldY,Xp,Yp); oldX=Xp; oldY = Yp; } for (I=1; I<=Np; I++) { Point p=GetPhysPos(XVa[I],YVa[I]); Xp=p.x; Yp=p.y; DrawPoint(Xp,Yp,Color.red); } Fplot = true; } //=================================================================== void CorCoefficient() { double Ymean=0, SUMn, SUMd, Yth; int I; for (I=1; I<=Np; I++) Ymean+=YVa[I]; Ymean/=Np; SUMn=0; SUMd=0; for (I=1; I<=Np; I++) { Yth=CalcY(XVa[I]); SUMn += (YVa[I]-Yth)*(YVa[I]-Yth); SUMd += (YVa[I]-Ymean)*(YVa[I]-Ymean); } if (Math.abs(SUMd)<1e-7) return; Ccoef= 1-SUMn/SUMd; SumSq=SUMn; } //=================================================================== void CalculateCoef() { boolean jump = false; int I,J,K=0; double LOC,S,Z; double [] AA = new double[2*Dmax+2]; double [][] R = new double[Dmax+2][Dmax+3]; double [] T = new double[Dmax+3]; for (I=1; I<=2*Dmax+1; I++) AA[I]=0; for (I=1; I<=Dmax+1; I++) for (J=1; J<=Dmax+2; J++) R[I][J]=0; for (I=1; I<=Dmax+2; I++) T[I]=0; AA[1]=Np; for (I=1; I<=Np; I++) { for (J=2; J<=2*Dm+1; J++) AA[J]+=Math.pow(XVa[I],J-1); for (K=1; K<=Dm+1; K++) { LOC=Math.pow(XVa[I],K-1); R[K][Dm+2]=T[K]+YVa[I]*LOC; T[K]+=YVa[I]*LOC; } T[Dm+2] += YVa[I]*YVa[I]; } for (J=1; J<=Dm+1; J++) for (K=1; K<=Dm+1; K++) R[J][K]=AA[J+K-1]; for (J = 1; J <= Dm + 1; J++) { for (K=J; K<=Dm+1; K++) if (Math.abs(R[K][J])>1e-30) { jump=true; break; } if (!jump) return; jump = false; for (I=1; I<=Dm+2; I++) { S=R[J][I]; R[J][I] = R[K][I]; R[K][I]=S; } Z=1/R[J][J]; for (I=1; I<=Dm+2; I++) R[J][I]*=Z; for (K=1; K<=Dm+1; K++) { if (K==J) jump = true; if (!jump) { Z=-R[K][J]; for (I=1; I<=Dm+2; I++) R[K][I]+=Z*R[J][I]; } jump=false; } } for (J=0; J<=Dm; J++) A[J]=R[J+1][Dm+2]; } //=================================================================== void DoAll() { int I; if (Np>Dm) { CalculateCoef(); CorCoefficient(); ClearPlot(); DrawGrid(); DrawCurve(); edit2.setText(""+(Math.round(10000*Ccoef)/10000.0)); edit3.setText(""+(Math.round(100*SumSq)/100.0)); list1.removeAll(); String st; for (I=0; I<=Dm; I++) { int m=5 - (int)(Math.log(A[I])/2.302585093); if (m<0) m=0; st=" = "; if (I>9) st="= "; if (A[I]>=0) st=st+"+"; list1.add("a"+I+st+((double)Math.round(Math.pow(10,m)*A[I])/Math.pow(10,m))); } } } //=================================================================== public void actionPerformed(ActionEvent e) { if (e.getSource()==butClear) { Np=0; Dm=1; edit1.setText("1"); edit2.setText(""); edit3.setText(""); list1.removeAll(); ClearPlot(); DrawGrid(); } if (e.getSource()==butDec) { if (Dm>1) Dm--; else return; edit1.setText(""+Dm); DoAll(); } if (e.getSource()==butInc) { if (DmX0)&&(xY0)&&(y