//QuadraticApplet.java import java.text.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class LissajousApplet extends JApplet implements ActionListener { JLabel aLabel, bLabel, xLowLabel, xHighLabel, yLowLabel, yHighLabel, tickIntervalLabel; JTextField aField, bField, xLowField, xHighField, yLowField, yHighField, tickIntervalField; JButton aUpButton, aDownButton, bUpButton, bDownButton, zoomInButton, zoomOutButton; JSlider horizSlider, vertSlider; LissajousPlotPanel lissajousPlotPanel; double xLo=-1.1, xHi=1.1, yLo=-1.1, yHi=1.1, tickInt=1; double a, b; public void init() { //int westpanelwidth=20; Container container = getContentPane(); container.setLayout(null); //** no Layout Manager lissajousPlotPanel = new LissajousPlotPanel(); if (getHeight() < getWidth()) lissajousPlotPanel.setBounds(60,50,getHeight()-60,getHeight()-50); else lissajousPlotPanel.setBounds(60,50,getWidth()-60,getWidth()-50); container.add(lissajousPlotPanel); /* JPanel westPanel = new JPanel(); westPanel.setLayout( new GridLayout(4,1) ); westPanel.setBackground(Color.LIGHT_GRAY); westPanel.setBounds(0,0, westpanelwidth,200); //westPanel.setLayout(null); container.add(westPanel); */ /* JPanel coeffsPanel = new JPanel(); coeffsPanel.setLayout(new FlowLayout()); westPanel.add(coeffsPanel); */ /* JPanel plotXPanel = new JPanel(); plotXPanel.setLayout(new FlowLayout()); westPanel.add(plotXPanel); JPanel plotYPanel = new JPanel(); plotYPanel.setLayout(new FlowLayout()); westPanel.add(plotYPanel); JPanel plotTickPanel = new JPanel(); plotTickPanel.setLayout(new FlowLayout()); westPanel.add(plotTickPanel); */ JPanel zoomPanel = new JPanel(); zoomPanel.setBackground(Color.BLACK); //zoomPanel.setBounds(0,getHeight()-200, westpanelwidth,40); zoomPanel.setBounds(getWidth()-200,0, 200,40); container.add(zoomPanel); JLabel formulaLabel = new JLabel( "x=cos(at) y=sin(bt)" ); formulaLabel.setBounds(1,5,120,20); container.add( formulaLabel ); aLabel = new JLabel( "a" ); //coeffsPanel.add( aLabel ); aLabel.setBounds(20,140,20,20); container.add( aLabel ); aField = new JTextField("3",3 ); aField.setBounds(5,160,40,20); aField.setHorizontalAlignment(JTextField.RIGHT); aField.addActionListener( this ); container.add( aField ); //coeffsPanel.add( aField ); aUpButton = new JButton( "\u2191" ); aUpButton.setBounds(0,180,50,20); container.add( aUpButton ); aUpButton.addActionListener( this ); aDownButton = new JButton( "\u2193" ); aDownButton.setBounds(0,200,50,20); container.add( aDownButton ); aDownButton.addActionListener( this ); bLabel = new JLabel( "b" ); //coeffsPanel.add( bLabel ); bLabel.setBounds(200,10,20,20); container.add( bLabel ); bField = new JTextField("5",3 ); bField.setBounds(220,10,40,20); bField.setHorizontalAlignment(JTextField.RIGHT); bField.addActionListener( this ); container.add( bField ); //coeffsPanel.add( bField ); bUpButton = new JButton( "\u2191" ); bUpButton.setBounds(260,5,50,20); container.add( bUpButton ); bUpButton.addActionListener( this ); bDownButton = new JButton( "\u2193" ); bDownButton.setBounds(260,25,50,20); container.add( bDownButton ); bDownButton.addActionListener( this ); /* xLowLabel = new JLabel( "X low" ); plotXPanel.add( xLowLabel ); xLowField = new JTextField( "-1",3 ); xLowField.setHorizontalAlignment(JTextField.RIGHT); xLowField.addActionListener( this ); plotXPanel.add( xLowField ); xHighLabel = new JLabel( "X high" ); plotXPanel.add( xHighLabel ); xHighField = new JTextField( "1",3 ); xHighField.setHorizontalAlignment(JTextField.RIGHT); xHighField.addActionListener( this ); plotXPanel.add( xHighField ); yLowLabel = new JLabel( "Y low" ); plotYPanel.add( yLowLabel ); yLowField = new JTextField( "-1",3 ); yLowField.setHorizontalAlignment(JTextField.RIGHT); yLowField.addActionListener( this ); plotYPanel.add( yLowField ); yHighLabel = new JLabel( "Y high" ); plotYPanel.add( yHighLabel ); yHighField = new JTextField( "1",3 ); yHighField.setHorizontalAlignment(JTextField.RIGHT); yHighField.addActionListener( this ); plotYPanel.add( yHighField ); tickIntervalLabel = new JLabel( "Tick interval" ); plotTickPanel.add( tickIntervalLabel ); tickIntervalField = new JTextField( "1",2 ); tickIntervalField.setHorizontalAlignment(JTextField.RIGHT); tickIntervalField.addActionListener( this ); plotTickPanel.add( tickIntervalField ); */ horizSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 100, 30 ); horizSlider.setMajorTickSpacing( 10 ); //horizSlider.setMinorTickSpacing( 1 ); horizSlider.setPaintTicks( true ); //horizSlider.setPaintLabels( true ); horizSlider.setBounds(310,0,210,40); container.add(horizSlider); horizSlider.addChangeListener( new ChangeListener() { public void stateChanged( ChangeEvent e ) { bField.setText(""+(horizSlider.getValue()/10.0)); bField.postActionEvent(); } } ); vertSlider = new JSlider( SwingConstants.VERTICAL, 0, 100, 50); vertSlider.setMajorTickSpacing( 10 ); //vertSlider.setMinorTickSpacing( 1 ); vertSlider.setPaintTicks( true ); //vertSlider.setPaintLabels( true ); vertSlider.setBounds(10,220,40,200); container.add(vertSlider); vertSlider.addChangeListener( new ChangeListener() { public void stateChanged( ChangeEvent e ) { aField.setText(""+(vertSlider.getValue()/10.0)); aField.postActionEvent(); } } ); zoomInButton = new JButton( "Zoom IN" ); zoomPanel.add( zoomInButton ); zoomInButton.addActionListener( this ); zoomOutButton = new JButton( "Zoom OUT" ); zoomPanel.add( zoomOutButton ); zoomOutButton.addActionListener( this ); aField.postActionEvent(); } public void actionPerformed( ActionEvent event ) { double aTry, bTry; DecimalFormat d2, d1; d2 = new DecimalFormat ("0.00"); d1 = new DecimalFormat ("0.0"); if ( event.getSource() == aField || event.getSource() == bField ) { try { aTry = Double.parseDouble(aField.getText()); bTry = Double.parseDouble(bField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "a, and b must be numbers", "Quadratic: Invalid input", JOptionPane.ERROR_MESSAGE); return; } a = aTry; b = bTry; horizSlider.setValue((int)(b*10)); //doesn't invoke handler? vertSlider.setValue ((int)(a*10)); } // double xLo, xHi, yLo, yHi, tickInt; /* try { xLo = Double.parseDouble(xLowField.getText()); xHi = Double.parseDouble(xHighField.getText()); yLo = Double.parseDouble(yLowField.getText()); yHi = Double.parseDouble(yHighField.getText()); tickInt = Double.parseDouble(tickIntervalField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Plotting coordinates must be numbers", "Quadratic plotting: Invalid input", JOptionPane.ERROR_MESSAGE); return; } if (xLo>xHi || yLo>yHi || tickInt<=0) { JOptionPane.showMessageDialog(null, "Bad plotting coordinates", "Quadratic plotting: Invalid input", JOptionPane.ERROR_MESSAGE); return; } */ else if ( event.getSource() == aUpButton ) { a += .1; aField.setText(""+d1.format(a)); vertSlider.setValue ((int)(a*10)); } else if ( event.getSource() == aDownButton ) { if (a >= .1) a -= .1; else a = 0; aField.setText(""+d1.format(a)); vertSlider.setValue ((int)(a*10)); } else if ( event.getSource() == bUpButton ) { b += .1; bField.setText(""+d1.format(b)); horizSlider.setValue ((int)(b*10)); } else if ( event.getSource() == bDownButton ) { if (b >= .1) b -= .1; else b = 0; bField.setText(""+d1.format(b)); horizSlider.setValue ((int)(b*10)); } if ( event.getSource() == zoomInButton ) { double scale = .9; //shrink the grid by 10% xLo *= scale; xHi *= scale; yLo *= scale; yHi *= scale; /*xLowField.setText(""+d2.format(xLo)); xHighField.setText(""+d2.format(xHi)); yLowField.setText(""+d2.format(yLo)); yHighField.setText(""+d2.format(yHi)); *///repaint(); } else if ( event.getSource() == zoomOutButton ) { double scale = 1.1; //grow the grid by 10% xLo *= scale; xHi *= scale; yLo *= scale; yHi *= scale; /* xLowField.setText(""+d2.format(xLo)); xHighField.setText(""+d2.format(xHi)); yLowField.setText(""+d2.format(yLo)); yHighField.setText(""+d2.format(yHi)); */ //repaint(); } int numPoints=5000; double [] X = new double[numPoints]; double [] Y = new double[numPoints]; double dt = 2*Math.PI/numPoints; double t=0; for (int i=0; i