//QuadraticApplet.java import java.text.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class QuadraticApplet extends JApplet implements ActionListener { JLabel formulaLabel, aLabel, bLabel, cLabel, equationLabel, x1Label, x2Label, numSolutionsLabel, vertexHLabel, discriminantLabel, focusLabel, directrixLabel, distanceLabel, latusRectumLabel, xLowLabel, xHighLabel, yLowLabel, yHighLabel, tickIntervalLabel; JTextField aField, bField, cField, x1Field, x2Field, numSolutionsField, vertexHField, vertexKField, discriminantField, focusXField, focusYField, directrixField, distanceField, latusRectumField, xLowField, xHighField, yLowField, yHighField, tickIntervalField; JButton zoomInButton, zoomOutButton; PlotPanel quadPlotPanel; public void init() { int westpanelwidth=200; Container container = getContentPane(); container.setLayout(null); //** no Layout Manager quadPlotPanel = new PlotPanel(); quadPlotPanel.setBounds(westpanelwidth,0,getWidth()-westpanelwidth,getHeight()); container.add(quadPlotPanel); //dummy blank plot... NOT needed anymore //quadPlotPanel.newPlot(null,null,0,0,0,0,0,0,0,1,2,1,2,10); //??have it paint, else nothing displays?!?!? JPanel westPanel = new JPanel(); westPanel.setLayout( new GridLayout(14,1) ); westPanel.setBackground(Color.LIGHT_GRAY); westPanel.setBounds(0,0, westpanelwidth,450); //westPanel.setLayout(null); container.add(westPanel); JPanel formulaPanel = new JPanel(); formulaPanel.setLayout(new FlowLayout()); westPanel.add(formulaPanel); JPanel coeffsPanel = new JPanel(); coeffsPanel.setLayout(new FlowLayout()); //topPanel.setBounds(0,0, 200,50); westPanel.add(coeffsPanel); JPanel equationPanel = new JPanel(); equationPanel.setLayout(new FlowLayout()); westPanel.add(equationPanel); JPanel discriminantPanel = new JPanel(); discriminantPanel.setLayout(new FlowLayout()); westPanel.add(discriminantPanel); JPanel numSolutionsPanel = new JPanel(); numSolutionsPanel.setLayout(new FlowLayout()); westPanel.add(numSolutionsPanel); JPanel midPanel = new JPanel(); midPanel.setLayout(new FlowLayout()); westPanel.add(midPanel); JPanel vertexPanel = new JPanel(); vertexPanel.setLayout(new FlowLayout()); westPanel.add(vertexPanel); JPanel focusPanel = new JPanel(); focusPanel.setLayout(new FlowLayout()); westPanel.add(focusPanel); JPanel directrixPanel = new JPanel(); directrixPanel.setLayout(new FlowLayout()); westPanel.add(directrixPanel); JPanel distancePanel = new JPanel(); distancePanel.setLayout(new FlowLayout()); westPanel.add(distancePanel); JPanel latusRectumPanel = new JPanel(); latusRectumPanel.setLayout(new FlowLayout()); westPanel.add(latusRectumPanel); 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); container.add(zoomPanel); formulaLabel = new JLabel( "y=ax\u00B2+bx+c" ); formulaPanel.add(formulaLabel); aLabel = new JLabel( "a" ); coeffsPanel.add( aLabel ); aField = new JTextField("1",3 ); aField.setHorizontalAlignment(JTextField.RIGHT); aField.addActionListener( this ); coeffsPanel.add( aField ); bLabel = new JLabel( "b" ); coeffsPanel.add( bLabel ); bField = new JTextField("0",3 ); bField.setHorizontalAlignment(JTextField.RIGHT); bField.addActionListener( this ); coeffsPanel.add( bField ); cLabel = new JLabel( "c" ); coeffsPanel.add( cLabel ); cField = new JTextField("0",3 ); cField.setHorizontalAlignment(JTextField.RIGHT); cField.addActionListener( this ); coeffsPanel.add( cField ); equationLabel = new JLabel( quadraticEquation(Double.parseDouble(aField.getText()), Double.parseDouble(bField.getText()), Double.parseDouble(cField.getText()))); equationPanel.add(equationLabel); numSolutionsLabel = new JLabel( "# real solutions" ); numSolutionsPanel.add( numSolutionsLabel ); numSolutionsField = new JTextField( 1 ); numSolutionsField.setEditable(false); numSolutionsPanel.add( numSolutionsField ); x1Label = new JLabel( "x=" ); midPanel.add( x1Label ); x1Field = new JTextField( 4 ); x1Field.setBackground(Color.CYAN); x1Field.setHorizontalAlignment(JTextField.RIGHT); x1Field.setEditable(false); midPanel.add( x1Field ); x2Label = new JLabel( "x=" ); midPanel.add( x2Label ); x2Field = new JTextField( 4 ); x2Field.setBackground(Color.CYAN); x2Field.setHorizontalAlignment(JTextField.RIGHT); x2Field.setEditable(false); midPanel.add( x2Field ); discriminantLabel = new JLabel( "Discriminant b\u00B2-4ac" ); discriminantPanel.add( discriminantLabel ); discriminantField = new JTextField( 4 ); discriminantField.setHorizontalAlignment(JTextField.RIGHT); discriminantField.setEditable(false); discriminantPanel.add( discriminantField ); vertexHLabel = new JLabel( "Vertex (h,k)" ); vertexPanel.add( vertexHLabel ); vertexHField = new JTextField( 4 ); vertexHField.setBackground(Color.MAGENTA); vertexHField.setHorizontalAlignment(JTextField.RIGHT); vertexHField.setEditable(false); vertexPanel.add( vertexHField ); vertexKField = new JTextField( 4 ); vertexKField.setBackground(Color.MAGENTA); vertexKField.setEditable(false); vertexKField.setHorizontalAlignment(JTextField.RIGHT); vertexPanel.add( vertexKField ); focusLabel = new JLabel( "Focus (x,y)" ); focusPanel.add( focusLabel ); focusXField = new JTextField( 4 ); focusXField.setBackground(Color.GREEN); focusXField.setHorizontalAlignment(JTextField.RIGHT); focusXField.setEditable(false); focusPanel.add( focusXField ); focusYField = new JTextField( 4 ); focusYField.setBackground(Color.GREEN); focusYField.setEditable(false); focusYField.setHorizontalAlignment(JTextField.RIGHT); focusPanel.add( focusYField ); directrixLabel = new JLabel( "Directrix y=" ); directrixPanel.add( directrixLabel ); directrixField = new JTextField( 4 ); directrixField.setBackground(Color.YELLOW); directrixField.setHorizontalAlignment(JTextField.RIGHT); directrixField.setEditable(false); directrixPanel.add( directrixField ); distanceLabel = new JLabel( "|F-V|=|V-D|=" ); distancePanel.add( distanceLabel ); distanceField = new JTextField( 4 ); //distanceField.setBackground(Color.YELLOW); distanceField.setHorizontalAlignment(JTextField.RIGHT); distanceField.setEditable(false); distancePanel.add( distanceField ); latusRectumLabel = new JLabel( "|latus rectum|" ); latusRectumPanel.add( latusRectumLabel ); latusRectumField = new JTextField( 4 ); latusRectumField.setBackground(Color.PINK); latusRectumField.setHorizontalAlignment(JTextField.RIGHT); latusRectumField.setEditable(false); latusRectumPanel.add( latusRectumField ); xLowLabel = new JLabel( "X low" ); plotXPanel.add( xLowLabel ); xLowField = new JTextField( "-5",3 ); xLowField.setHorizontalAlignment(JTextField.RIGHT); xLowField.addActionListener( this ); plotXPanel.add( xLowField ); xHighLabel = new JLabel( "X high" ); plotXPanel.add( xHighLabel ); xHighField = new JTextField( "5",3 ); xHighField.setHorizontalAlignment(JTextField.RIGHT); xHighField.addActionListener( this ); plotXPanel.add( xHighField ); yLowLabel = new JLabel( "Y low" ); plotYPanel.add( yLowLabel ); yLowField = new JTextField( "-3",3 ); yLowField.setHorizontalAlignment(JTextField.RIGHT); yLowField.addActionListener( this ); plotYPanel.add( yLowField ); yHighLabel = new JLabel( "Y high" ); plotYPanel.add( yHighLabel ); yHighField = new JTextField( "3",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 ); 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 a, b, c, discriminant, h, k, focusX, focusY, directrix; double x1=Double.POSITIVE_INFINITY, x2=Double.POSITIVE_INFINITY; DecimalFormat d2, d3; d2 = new DecimalFormat ("0.00"); d3 = new DecimalFormat ("0.000"); try { a = Double.parseDouble(aField.getText()); b = Double.parseDouble(bField.getText()); c = Double.parseDouble(cField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "a, b, and c must be numbers", "Quadratic: Invalid input", JOptionPane.ERROR_MESSAGE); return; } discriminant = b*b - 4*a*c; h = -b / (2 * a); k = -(b*b-4*a*c) / (4*a); focusX = h; //oh... focusY = k + 1/(4*a); directrix = k - 1/(4*a); if (a == 0) { /* x1Field.setText(""); x2Field.setText(""); numSolutionsField.setText(""); discriminantField.setText(""); vertexHField.setText(""); vertexKField.setText(""); focusXField.setText(""); focusYField.setText(""); directrixField.setText(""); */ JOptionPane.showMessageDialog(null, "a can not be 0", "Quadratic: Invalid input", JOptionPane.ERROR_MESSAGE); return; } else if (discriminant > 0) { // calculate the two possible real values for x. x1 = (-b + Math.sqrt(discriminant)) / (2 * a); x2 = (-b - Math.sqrt(discriminant)) / (2 * a); numSolutionsField.setText("2"); x1Field.setText(""+d2.format(x1)); x2Field.setText(""+d2.format(x2)); } else if (discriminant == 0) { //one real root x1 = -b / (2*a); numSolutionsField.setText("1"); x1Field.setText(""+d2.format(x1)); x2Field.setText(""); } else { //no real roots numSolutionsField.setText("0"); x1Field.setText(""); x2Field.setText(""); } discriminantField.setText(""+d2.format(discriminant)); vertexHField.setText(""+d2.format(h)); vertexKField.setText(""+d2.format(k)); focusXField.setText(""+d2.format(focusX)); focusYField.setText(""+d2.format(focusY)); directrixField.setText(""+d2.format(directrix)); equationLabel.setText( quadraticEquation(a,b,c)); distanceField.setText(""+d3.format(Math.abs(k-focusY))); latusRectumField.setText(""+d3.format(4*Math.abs(k-focusY))); 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; } 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(); } double [] X = new double[2000]; double [] Y = new double[2000]; double xDiff = (xHi - xLo) / X.length; X[0] = xLo; Y[0] = a*X[0]*X[0] + b*X[0] + c; for (int i=1; i