// LeastSquareFitFrame.java // demonstrate equations to compute and draw a least square fit curve // through a set of points and compute the integral import java.awt.*; import java.awt.event.*; import java.text.*; import java.io.*; import javax.swing.*; public class LeastSquareFitFrame extends JFrame { double x[] = new double[100]; double y[] = new double[100]; int n; // number of points so far boolean plot; int height=450; int width=450; int hw=400; // base height and width of point area int hwTemp=hw; final double xymax=100.0; // maximum value of x or y point double sca=(double)hw/xymax; double scaTemp=sca; int f1xOff=50; int f1yOff=50; int orderValues[] = {3, 4, 5, 6, -1, -2}; // -1 is code for N/2 -2 is code for N int orderItemValue; // set to order code int optionItemValues[]; JRadioButtonMenuItem orderItems[], options[]; ButtonGroup optionGroup, orderGroup; MyJPanel panel; private JDesktopPane theDesktop; public LeastSquareFitFrame() { n=0; plot=false; setTitle("LeastSquareFitFrame demonstration"); setSize(520,600); setBackground(Color.white); setForeground(Color.black); // create menu bar and attach it to AwtMenuTest window JMenuBar bar = new JMenuBar(); setJMenuBar(bar); // set up Algorithm menu JMenu algorithmMenu = new JMenu("Algorithm"); bar.add(algorithmMenu); // add algorithm menu to menu bar // set up Algorithm items JMenuItem leastItem = new JMenuItem("Least Square fit"); algorithmMenu.add(leastItem); JMenuItem evaluateItem = new JMenuItem("Evaluate"); algorithmMenu.add(evaluateItem); JMenuItem integrateItem = new JMenuItem("Integrate"); algorithmMenu.add(integrateItem); // set up Code menu JMenu codeMenu = new JMenu("Code"); bar.add(codeMenu); // add code menu to menu bar // set up Code items JMenuItem leastClassItem = new JMenuItem("Least Square Fit class"); codeMenu.add(leastClassItem); JMenuItem demonstrationItem = new JMenuItem("Demonstration"); codeMenu.add(demonstrationItem); // set up Parameter menu JMenu parameterMenu = new JMenu("Parameters"); bar.add( parameterMenu ); // add parameter menu to menu bar // create Order submenu String orders[] = {"3", "4", "5", "6", "N/2", "N" }; JMenu orderMenu = new JMenu( "Order" ); orderItems = new JRadioButtonMenuItem[ orders.length ]; orderGroup = new ButtonGroup(); ItemHandler itemHandler = new ItemHandler(); // create Order radio button menu items for(int i=0; ixymax) x[n]=xymax; y[n]=(double)(-yp+f1yOff+hw)/sca; if(y[n]<0.0) y[n]=0.0; if(y[n]>xymax) y[n]=xymax; for(int i=0; ixmax) xmax=x[i]; } for(int i=0; in) orderItemValue=n; break; } } System.out.println("orderItemValue="+orderItemValue); LeastSquareFit s1 = new LeastSquareFit(xx, ff, orderItemValue); // plot curve double dx = (xmax-xmin)/100.009; double xp, yp, xt, yt; g.setColor(Color.black); xp=x[ixmin]; yp=s1.evaluate(xp); for(int i=1; i<=100; i++) { xt = xmin+(double)i * dx; yt = s1.evaluate(xt); // evaluate based on coefficients if(xt>xymax)xt=xymax; // crop the plot if(xt<0.0)xt=0.0; if(yt>xymax)yt=xymax; if(yt<0.0)yt=0.0; // plot segment g.drawLine((int)(f1xOff+xp*sca), (int)(f1yOff-yp*sca+hw), (int)(f1xOff+xt*sca), (int)(f1yOff-yt*sca+hw)); xp=xt; yp=yt; } xt = s1.integrate(xmin, xmax); g.setColor(Color.blue); g.drawString("integral="+xt, 50, 30); } // end paint // return dimensions for sizing public Dimension getPreferredSize() { return new Dimension(500, 500); } } // end MyJPanel void displayHelp(String file_name) { Frame f = new Frame(); f.setTitle("Menu Information"); f.setSize(600, 400); f.setLocation(400, 100); TextArea t = new TextArea("", 24, 80, TextArea.SCROLLBARS_BOTH); Font cur12 = new Font("courier", 0, 12); t.setFont(cur12); try { BufferedReader in = new BufferedReader(new FileReader(file_name)); String input_line; input_line = in.readLine(); while(input_line != null) { t.append(input_line+"\n"); input_line = in.readLine(); } } catch(IOException exception) { System.out.println(exception); } f.add(t); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { e.getWindow().dispose(); }}); f.setVisible(true); } // end displayHelp // class to handle action events from order and option items private class ItemHandler implements ActionListener { // process order and option selections public void actionPerformed( ActionEvent event ) { // process order selection for(int i=0; i