//PoissonCurve.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.util.*; import java.text.*; public class PoissonCurve extends JApplet implements ActionListener { double yScale=1; int lambda, numPoissons; int pNums[], others; JPanel controlPanel; JLabel lambdaLabel, yScaleLabel, numPoissonsLabel; JTextField lambdaTextField, yScaleTextField, numPoissonsTextField; JButton yScaleUpButton, yScaleDownButton, showPoissonsButton; JTextArea poissontextarea; JScrollPane poissonscrollpane; // JSlider zSlider; DecimalFormat d4, d3; public void init() { String input; Container container = getContentPane(); container.setLayout( new BorderLayout() ); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); controlPanel.setBackground(Color.MAGENTA); controlPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED)); container.add(controlPanel,BorderLayout.NORTH); lambdaLabel = new JLabel( "\u03BB" ); controlPanel.add( lambdaLabel ); lambdaTextField = new JTextField("4",3); lambda = Integer.parseInt(lambdaTextField.getText()); lambdaTextField.setHorizontalAlignment(JTextField.RIGHT); lambdaTextField.addActionListener( this ); controlPanel.add( lambdaTextField ); yScaleLabel = new JLabel( "y scale" ); controlPanel.add( yScaleLabel ); yScaleTextField = new JTextField("1",4); yScale = Double.parseDouble(yScaleTextField.getText()); yScaleTextField.setHorizontalAlignment(JTextField.RIGHT); yScaleTextField.addActionListener( this ); controlPanel.add( yScaleTextField ); yScaleUpButton = new JButton( "+" ); controlPanel.add( yScaleUpButton ); yScaleUpButton.addActionListener( this ); yScaleDownButton = new JButton( "-" ); controlPanel.add( yScaleDownButton ); yScaleDownButton.addActionListener( this ); numPoissonsLabel = new JLabel( "Sample size" ); controlPanel.add( numPoissonsLabel ); numPoissonsTextField = new JTextField("100",4); numPoissons = Integer.parseInt(numPoissonsTextField.getText()); numPoissonsTextField.setBackground(Color.BLUE); numPoissonsTextField.setForeground(Color.WHITE); numPoissonsTextField.setHorizontalAlignment(JTextField.RIGHT); numPoissonsTextField.addActionListener( this ); controlPanel.add( numPoissonsTextField ); showPoissonsButton = new JButton( "Show sample" ); controlPanel.add( showPoissonsButton ); showPoissonsButton.addActionListener( this ); d4 = new DecimalFormat (".0000"); d3 = new DecimalFormat (".000"); poissontextarea = new JTextArea(30,100); poissontextarea.setLineWrap( true ); poissontextarea.setFont( new Font("Courier",Font.BOLD,14) ); poissonscrollpane = new JScrollPane(poissontextarea); // zTextField.setText("1.0"); //fires listener? no // zTextField.postActionEvent(); } public void actionPerformed( ActionEvent event ) { try { int lambdaTry = Integer.parseInt(lambdaTextField.getText()); numPoissons = Integer.parseInt(numPoissonsTextField.getText()); double yScaleTry = Double.parseDouble(yScaleTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "All inputs must be numeric", "PoissonCurve: Invalid input", JOptionPane.ERROR_MESSAGE); return; } if ( event.getSource() == lambdaTextField ) { int oldLambda = lambda; lambda = Integer.parseInt(lambdaTextField.getText()); if (lambda < 1) { JOptionPane.showMessageDialog(null, "\u03BB must be > 0", "NormalCurve: Invalid input", JOptionPane.ERROR_MESSAGE); lambda = oldLambda; lambdaTextField.setText(""+lambda); } repaint(); } else if ( event.getSource() == numPoissonsTextField ) { numPoissons = Integer.parseInt(numPoissonsTextField.getText()); repaint(); } /* else if ( event.getSource() == areaTextField ) { double oldarea = area; area = Double.parseDouble(areaTextField.getText()); if (area<0 || area>0.5) { JOptionPane.showMessageDialog(null, "0 <= area <= 0.5", "NormalCurve: Invalid input", JOptionPane.ERROR_MESSAGE); area = oldarea; areaTextField.setText(""+d4.format(area)); } else { z = calcZ(area); zTextField.setText(""+z); zSlider.setValue((int)(z/4*getWidth()/2)); x = u + z*s; xTextField.setText(""+d4.format(x)); repaint(); } } */ else if ( event.getSource() == yScaleTextField ) { double oldYscale = yScale; yScale = Double.parseDouble(yScaleTextField.getText()); if (yScale < 0) { JOptionPane.showMessageDialog(null, "y scale can not be negative", "NormalCurve: Invalid input", JOptionPane.ERROR_MESSAGE); yScale = oldYscale; yScaleTextField.setText(""+yScale); } else repaint(); } else if ( event.getSource() == yScaleUpButton ) { yScale++; yScaleTextField.setText(""+yScale); repaint(); } else if ( event.getSource() == yScaleDownButton ) { if (yScale > 1) { yScale--; yScaleTextField.setText(""+yScale); repaint(); } } else if ( event.getSource() == showPoissonsButton ) { int sum=0, sumSqrd=0; double mean, sd; for (int i=0; i