package svdtmp; /** *
Title:
* *Description:
* *Copyright: Copyright (c) 2009
* *Company:
* * @author not attributable * @version 1.0 */ import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; import java.util.Observable; import java.util.Observer; /***************************************************************************** * * * view SVDMVC * * * *****************************************************************************/ public class SVDMVC extends JApplet { private SVDController controller = new SVDController(); private int labelHeight = 20, // label height buttonHeight = 30, // button height panelHeight = 400, // panel height panelWidth = 402; // panel width //Create the menu bar. JMenuBar menuBar = new JMenuBar(); JMenu menu1 = new JMenu("Matrix"); JMenu menu2 = new JMenu("S V D"); JMenuItem menuItem1 = new JMenuItem("Show matrix"); JMenuItem menuItem2 = new JMenuItem("Show S V D"); /*************************************** * Panel Matrix * ***************************************/ JPanel panelInputData = new JPanel(); JPanel panelMatrixDim = new JPanel(); JPanel panelData = new JPanel(); JPanel panelHeader = new JPanel(); JPanel panelFid = new JPanel(); JLabel labelMatrixDim = new JLabel("Complex matrix dimension:"); JLabel labelMatrixDimRow = new JLabel("rows"); JLabel labelMatrixDimColumn = new JLabel("columns"); JLabel labelMatrixDimCondition = new JLabel("with columns <= rows"); JLabel labelFidRe = new JLabel("Real"); JLabel labelFidIm = new JLabel("Imaginary"); JLabel labelFid1 = new JLabel("part"); JLabel labelFid2 = new JLabel("part"); JTextField textFieldMatrixRow = new JTextField("3"); JTextField textFieldMatrixColumn = new JTextField("3"); JTextArea textAreaReal = new JTextArea("58\n68\n77\n27\n31\n47\n6\n19\n26\n"); JTextArea textAreaImag = new JTextArea("33\n52\n41\n8\n21\n21\n66\n49\n25\n"); JScrollPane scrollPaneFid = new JScrollPane(); JButton buttonClearMatrix = new JButton("Clear matrix"); JButton buttonRunSVD = new JButton("Run SVD"); /*************************************** * Panel S V D * ***************************************/ JPanel panelOutputData = new JPanel(); JPanel panelEigenValueData = new JPanel(); JPanel panelEigenValueHeader = new JPanel(); JPanel panelEigenValue = new JPanel(); JLabel labelEigen = new JLabel("Singular"); JLabel labelValue = new JLabel("Values"); JTextArea textAreaEigenValue = new JTextArea(); JScrollPane scrollPaneEigenValue = new JScrollPane(); JButton buttonClearSVD = new JButton("Clear SVD"); //Construct the applet public SVDMVC() { } //Initialize the applet public void init() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //très important : une seule fois dans un programme avant l'utilisation //de Jampack SVDMVCModel modela = new SVDMVCModel(); try { modela.essaisetBaseIndex(); } catch (Exception e) { textAreaEigenValue.setText("attention base"); } setJMenuBar(menuBar); menuBar.add(menu1); menuBar.add(menu2); menu1.add(menuItem1); menu2.add(menuItem2); //applet area this.setBackground(Color.white); this.getContentPane().setBackground(Color.white); this.getContentPane().setLayout(null); /*************************************** * Panel matrix * ***************************************/ panelInputData.setLayout(null); panelInputData.setBackground(Color.yellow); panelInputData.setBounds(2, 2, panelWidth, panelHeight); panelInputData.setFont(new Font("TimesRoman", Font.PLAIN, 12)); this.getContentPane().add(panelInputData); //Panel MatrixDim panelMatrixDim.setBounds(2, 2, 194, panelHeight); panelInputData.add(panelMatrixDim); panelMatrixDim.setBackground(Color.pink); panelMatrixDim.setLayout(null); panelMatrixDim.setFont(new Font("TimesRoman", Font.PLAIN, 14)); labelMatrixDim.setBounds(10, 20, 190, labelHeight); panelMatrixDim.add(labelMatrixDim); textFieldMatrixRow.setBounds(10, 50, 30, labelHeight); panelMatrixDim.add(textFieldMatrixRow); labelMatrixDimRow.setBounds(50, 50, 100, labelHeight); panelMatrixDim.add(labelMatrixDimRow); textFieldMatrixColumn.setBounds(10, 80, 30, labelHeight); panelMatrixDim.add(textFieldMatrixColumn); labelMatrixDimColumn.setBounds(50, 80, 100, labelHeight); panelMatrixDim.add(labelMatrixDimColumn); labelMatrixDimCondition.setBounds(10, 110, 160, labelHeight); panelMatrixDim.add(labelMatrixDimCondition); //Panel Data panelData.setBounds(200, 2, 200, panelHeight); panelInputData.add(panelData); panelData.setBackground(Color.pink); panelData.setLayout(null); panelData.setFont(new Font("TimesRoman", Font.PLAIN, 12)); panelHeader.setBounds(10, 7, 180, 2*labelHeight); panelHeader.setLayout(null); panelHeader.setBackground(Color.pink); labelFidRe.setBounds(0, 0, 90, labelHeight); panelHeader.add(labelFidRe); labelFidIm.setBounds(90, 0, 90, labelHeight); panelHeader.add(labelFidIm); labelFid1.setBounds(0, labelHeight, 90, labelHeight); panelHeader.add(labelFid1); labelFid2.setBounds(90, labelHeight, 90, labelHeight); panelHeader.add(labelFid2); panelData.add(panelHeader); scrollPaneFid.setBounds(10, 50, 180, 240); scrollPaneFid.getViewport().add(panelFid); panelData.add(scrollPaneFid); panelFid.setLayout(new GridLayout(1,2)); panelFid.add(textAreaReal); panelFid.add(textAreaImag); textAreaReal.setBackground(Color.orange); textAreaImag.setBackground(Color.yellow); textAreaReal.setMargin(new Insets(5, 5, 5, 5)); textAreaImag.setMargin(new Insets(5, 5, 5, 5)); textAreaReal.setEditable(true); textAreaImag.setEditable(true); buttonRunSVD.setBounds(50, 300, 95, buttonHeight); buttonRunSVD.setBackground(Color.red); panelData.add(buttonRunSVD); buttonClearMatrix.setBounds(40, 350, 115, buttonHeight); buttonClearMatrix.setBackground(Color.red); panelData.add(buttonClearMatrix); /*************************************** * Panel S V D * ***************************************/ panelOutputData.setLayout(null); panelOutputData.setBackground(Color.yellow); panelOutputData.setBounds(2, 2, panelWidth, panelHeight); panelOutputData.setFont(new Font("TimesRoman", Font.PLAIN, 12)); this.getContentPane().add(panelOutputData); panelEigenValueData.setBounds(2, 2, 140, panelHeight); panelOutputData.add(panelEigenValueData); panelEigenValueData.setBackground(Color.pink); panelEigenValueData.setLayout(null); panelEigenValueData.setFont(new Font("TimesRoman", Font.PLAIN, 12)); panelEigenValueHeader.setBounds(10, 7, 90, 2*labelHeight); panelEigenValueHeader.setLayout(null); panelEigenValueHeader.setBackground(Color.pink); labelEigen.setBounds(0, 0, 90, labelHeight); panelEigenValueHeader.add(labelEigen); labelValue.setBounds(0, labelHeight, 90, labelHeight); panelEigenValueHeader.add(labelValue); panelEigenValueData.add(panelEigenValueHeader); scrollPaneEigenValue.setBounds(10, 50, 120, 240); scrollPaneEigenValue.getViewport().add(panelEigenValue); panelEigenValueData.add(scrollPaneEigenValue); panelEigenValue.setLayout(new GridLayout(1,1)); panelEigenValue.add(textAreaEigenValue); textAreaEigenValue.setBackground(Color.orange); textAreaEigenValue.setMargin(new Insets(5, 5, 5, 5)); textAreaEigenValue.setEditable(true); buttonClearSVD.setBounds(22, 300, 95, buttonHeight); buttonClearSVD.setBackground(Color.red); panelEigenValueData.add(buttonClearSVD); } //end of jbinit /**Start the applet*/ public void start() { panelInputData.setVisible(true); panelOutputData.setVisible(false); buttonRunSVD.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { if (buttonRunSVD.getText() == "Run SVD") { controller.buttonRunSVD() ; }; } }); buttonClearMatrix.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { if (buttonClearMatrix.getText() == "Clear matrix") { controller.buttonClearMatrix() ; }; } }); buttonClearSVD.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { if (buttonClearSVD.getText() == "Clear SVD") { controller.buttonClearSVD(); }; } }); menuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { if (menuItem1.getText() == "Show matrix") { controller.menuItem1Run(); }; } }); menuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { if (menuItem2.getText() == "Show S V D") { controller.menuItem2Run(); }; } }); }//end of start() /**Stop the applet*/ public void stop() { } /**Destroy the applet*/ public void destroy() { } /**Get Applet information*/ public String getAppletInfo() { return "Applet Information"; } /**Get parameter info*/ public String[][] getParameterInfo() { return null; } /************************************************************************ * * * controller SVDController * * * ************************************************************************/ private class SVDController implements Runnable, Observer { public SVDMVCModel model = new SVDMVCModel(); public SVDController() { model.addObserver(this); } public void update(Observable o, Object arg){ } public void run() { } /*************************************** * Matrix * ***************************************/ private void menuItem1Run() { panelInputData.setVisible(true); panelOutputData.setVisible(false); }; private void buttonClearMatrix() { textAreaReal.setText(""); textAreaImag.setText(""); }; //--------------// // ButtonRunSVD // //--------------// private void buttonRunSVD() { int a, b; //convert matrix dimension into numbers a = model.setnvalue(Integer.parseInt(textFieldMatrixRow.getText())); b = model.setmvalue(Integer.parseInt(textFieldMatrixColumn.getText())); model.setmaxNbComplex(a*b); //convert experimental real part of FID intensities into numbers String sr = textAreaReal.getText(), s1r = ""; int spaceAtr = 0, startingFormr = 0, ipr = 0; double[] tmpr = new double[model.getmaxNbComplex()]; while (true) { spaceAtr = sr.indexOf("\n", startingFormr); if (spaceAtr == -1) break; s1r = sr.substring(startingFormr, spaceAtr); tmpr[ipr] = Double.valueOf(s1r).doubleValue(); startingFormr = spaceAtr + 1; ipr++; } model.setfidRe(tmpr); //convert experimental imaginary part of FID intensities into numbers String si = textAreaImag.getText(), s1i = ""; int spaceAti = 0, startingFormi = 0, ipi = 0; double[] tmpi = new double[model.getmaxNbComplex()]; while (true) { spaceAti = si.indexOf("\n", startingFormi); if (spaceAti == -1) break; s1i = si.substring(startingFormi, spaceAti); tmpi[ipi] = Double.valueOf(s1i).doubleValue(); startingFormi = spaceAti + 1; ipi++; } model.setfidIm(tmpi); //SVD call double[] titi = new double[model.getmvalue()+1]; try { titi = model.essaiZsvd(); //Show eigenvalues String s2 = ""; for (int j = 0; j < model.getmvalue(); j++) { s2 += titi[j] + "\n"; } textAreaEigenValue.setText(s2); } catch (Exception e) { buttonClearSVD.setBackground(Color.white); textAreaEigenValue.setText("Wrong\ndata\ninput"); } }; /*************************************** * S V D * ***************************************/ private void menuItem2Run() { panelInputData.setVisible(false); panelOutputData.setVisible(true); }; private void buttonClearSVD() { textAreaEigenValue.setText(""); }; }//end of SVDController() /************************************************************************ * * * Method main() * * * ************************************************************************/ public static void main(String[] args) { AppletApplicationFrame f = new AppletApplicationFrame("SVD applet window"); }//end of main() //Initialiseur statique pour le paramètre LookAndFeel static { try { //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch(Exception e) { } } }//end of class SVDMVC /***************************************************************************** * * * class AppletApplicationFrame * * * *****************************************************************************/ class AppletApplicationFrame extends JFrame { private SVDMVC myApplet; public AppletApplicationFrame(String title) { super(title); myApplet = new SVDMVC(); this.getContentPane().add(myApplet, BorderLayout.CENTER); this.setSize(737, 421); myApplet.init(); myApplet.start(); this.setVisible(true); this.setDefaultCloseOperation(3); } }//end of class AppletApplicationFrame