//
import java.awt.*;
import java.applet.*;
public class LeastSquares extends Applet {
Choice c;
GraphPanel gp;
Checkbox cb1,cb2;
public void init(){
setBackground(Color.white);
c = new Choice();
CheckboxGroup cbg = new CheckboxGroup();
cb1 = new Checkbox("Fit Y to X",cbg,true);
cb2 = new Checkbox("Fit X to Y",cbg,false);
gp = new GraphPanel(c,cb1);
add(gp);
add(step1());
add(step234(step2n3()));
//add(step2(c));
//add(step3());
//add(step4());
}
public Panel step2n3(){
Panel p = new Panel();
p.setLayout(new GridLayout(1,2,2,2));
p.setBackground(Color.white);
p.add(step2(c));
p.add(step3(cb1,cb2));
return p;
}
public Panel step234(Panel p1){
Panel p = new Panel();
p.setLayout(new GridLayout(2,1,2,2));
p.setBackground(Color.white);
p.add(p1);
p.add(step4());
return p;
}
public Panel step1(){
Panel p = new Panel();
p.setBackground(Color.white);
p.add(new Label("Select points by clicking on the graph or press "));
p.add(new Button("Example"));
return p;
}
public Panel step2(Choice c){
Panel p = new Panel();
p.setBackground(Color.white);
p.add(new Label("Degree of polynomial:"));
p.add(c);
return p;
}
public Panel step3(Checkbox cb1, Checkbox cb2){
Panel p = new Panel();
p.setBackground(Color.white);
p.setLayout(new GridLayout(2,1));
p.add(cb1);
p.add(cb2);
return p;
}
public Panel step4(){
Panel p = new Panel();
p.setBackground(Color.white);
p.add(new Button("Calculate"));
p.add(new Button("View Polynomial"));
p.add(new Button("Reset"));
return p;
}
public boolean action(Event evt, Object arg){
if (arg.equals("View Polynomial"))
gp.showFrame();
if (arg.equals("Reset"))
gp.reset();
if (arg.equals("Example"))
gp.addExample();
if (arg.equals("Calculate"))
gp.fitDraw = true;
//if (evt.target instanceof Choice){
//if (evt.target == cb1){
gp.repaint();
return true;
}
} // _LeastSquares()
//
//----------------------------------------------------------
//
class GraphPanel extends Panel {
Dimension size = new Dimension(300,300);
final Color myColor = new Color(255,255,200);
int current, max;
Point point[];
Choice c;
Checkbox cb;
boolean fitDraw;
GraphFrame gf;
public Dimension getMinimumSize() {return size;}
public Dimension getPreferredSize(){return size;}
public GraphPanel(Choice c, Checkbox cb){
setBackground(myColor);
max = 75;
this.c = c;
this.cb = cb;
gf = new GraphFrame();
reset();
}
public void showFrame(){
gf.show();
}
public void paint(Graphics g){
drawTicks(g);
drawPoints(g);
if (fitDraw)
drawFit(g);
// Draw Panel border
g.setColor(Color.black);
g.drawRect(0,0,size.width-1,size.height-1);
}
public void drawFit(Graphics g){
int numPoints = current;
int degreePoly = 0;
try {
degreePoly = 1+(int)Integer.parseInt(c.getSelectedItem());
} catch(NumberFormatException e){
c.select(1);
degreePoly = 2;
}
Jama.Matrix mat = new Jama.Matrix(numPoints,degreePoly);
double ab[];
Jama.Matrix bmat;
if (cb.getState()){
for (int j=0;j 0)
c.addItem(""+(current));
current++;
}
}
public void reset(){
point = new Point[max];
for (int i=0;i Math.abs(t[i]))
smallest = Math.abs(t[i]);
}
double range = 200/(largest - smallest);
for (int i=0;i