//QuadraticDriver.java import java.awt.*; import java.util.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class QuadraticDriver extends JApplet implements ActionListener, ListSelectionListener { private JLabel aLabel, bLabel, cLabel, zoomLabel; private JTextField aField, bField, cField; private JButton anotherButton; private JButton sortButton; private JButton clearButton; private JList quadraticJList; private JSlider zoomSlider; private ArrayList quadraticArrayList; private GraphingPanel quadraticGraphingPanel; public void init() { Container container = getContentPane(); container.setLayout(new GridLayout(1,2)); JPanel leftPanel = new JPanel(); leftPanel.setLayout(new BorderLayout()); JPanel northPanel = new JPanel(); northPanel.setLayout(new GridLayout(2,1)); JPanel topNorthPanel = new JPanel(); topNorthPanel.setLayout(new FlowLayout()); aLabel = new JLabel( "a" ); topNorthPanel.add( aLabel ); aField = new JTextField( 3 ); topNorthPanel.add( aField ); bLabel = new JLabel( "b" ); topNorthPanel.add( bLabel ); bField = new JTextField( 3 ); topNorthPanel.add( bField ); cLabel = new JLabel( "c" ); topNorthPanel.add( cLabel ); cField = new JTextField( 3 ); topNorthPanel.add( cField ); anotherButton = new JButton( "New quadratic" ); topNorthPanel.add( anotherButton ); anotherButton.addActionListener( this ); northPanel.add(topNorthPanel); JPanel bottomNorthPanel = new JPanel(); bottomNorthPanel.setLayout(new FlowLayout()); sortButton = new JButton( "Sort" ); bottomNorthPanel.add( sortButton ); sortButton.addActionListener( this ); clearButton = new JButton( "Clear All" ); bottomNorthPanel.add( clearButton ); clearButton.addActionListener( this ); northPanel.add(bottomNorthPanel); leftPanel.add(northPanel,BorderLayout.NORTH); quadraticArrayList = new ArrayList(); quadraticJList = new JList(quadraticArrayList.toArray()); quadraticJList.setVisibleRowCount(30); quadraticJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); quadraticJList.addListSelectionListener( this ); leftPanel.add(new JScrollPane(quadraticJList),BorderLayout.CENTER); container.add(leftPanel); JPanel rightPanel = new JPanel(); rightPanel.setLayout(new BorderLayout()); //??why need explicitly JPanel rightTopPanel = new JPanel(); rightTopPanel.setLayout(new GridLayout(2,1)); zoomLabel = new JLabel( "Zoom" ); rightTopPanel.add(zoomLabel); zoomSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 100, 10 ); rightTopPanel.add(zoomSlider); rightPanel.add(rightTopPanel,BorderLayout.NORTH); quadraticGraphingPanel = new GraphingPanel(); rightPanel.add(quadraticGraphingPanel,BorderLayout.CENTER); container.add(rightPanel); zoomSlider.addChangeListener( new ChangeListener() { public void stateChanged( ChangeEvent e ) { // //....?? } } ); } public void actionPerformed( ActionEvent event ) { if (event.getSource() == anotherButton) { double a, b, c; try { a = Double.parseDouble(aField.getText() ); b = Double.parseDouble(bField.getText()); c = Double.parseDouble(cField.getText()); Quadratic quad; try { quad = new Quadratic(a,b,c); quadraticArrayList.add(quad); quadraticJList.setListData(quadraticArrayList.toArray()); quadraticGraphingPanel.drawQuadratic(quad); } catch (InvalidQuadraticException e) { JOptionPane.showMessageDialog(null,e.getMessage()); } } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null,"a, b, and c must be numbers"); } } else if (event.getSource() == sortButton) { Collections.sort(quadraticArrayList,new QuadraticComparator()); quadraticJList.setListData(quadraticArrayList.toArray()); } else if (event.getSource() == clearButton) { quadraticArrayList.clear(); aField.setText(""); bField.setText(""); cField.setText(""); quadraticJList.setListData(quadraticArrayList.toArray()); } } public void valueChanged( ListSelectionEvent event ) { if ( event.getSource() == quadraticJList ) { int i = quadraticJList.getSelectedIndex(); if (i>=0) { //kludge .... Quadratic quad = (Quadratic)(quadraticArrayList.get(i)); quadraticGraphingPanel.drawQuadratic(quad); aField.setText(""+quad.getA()); bField.setText(""+quad.getB()); cField.setText(""+quad.getC()); } } } } //??? should this be public? private? inner? class GraphingPanel extends JPanel { Quadratic refQuad; public void paintComponent(Graphics g) { super.paintComponent(g); int w=getWidth(); int h=getHeight(); g.drawLine(0,h/2,w,h/2); g.drawLine(w/2,0,w/2,h); double a, b, c, x1, x2; if (refQuad != null) { a = refQuad.getA(); b = refQuad.getB(); c = refQuad.getC(); switch (refQuad.getNumberSolutions()) { case 2: x2 = refQuad.getx2(); case 1: x1 = refQuad.getx1(); } //quick and dirty plotting g.setColor(Color.RED); int y; for (int x=-w/2; x