package poisson; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; import edu.csusb.danby.applet.*; import javax.swing.*; import edu.csusb.danby.stat.*; import java.util.*; /** * Main SharkApplet class to demonstrate Poisson Random variables * @author Charles S. Stanton * @version Sun Jul 28 09:19:33 PDT 2002 */ public class SharkApplet extends DApplet implements DControlPanelAction { private static ResourceBundle resources; static { try { resources = ResourceBundle.getBundle("poisson.resources.Shark"); } catch (MissingResourceException mre) { System.err.println("Shark.properties not found"); System.exit(0); } } OceanPanel ocean; HistogramPanel hp; double lambda = 3.0; //Poisson distribution parameter //SharkApplet applet=this; SharkModel model; JLabel xc= new JLabel("X=0.0 ");// displays X String[] buttonLabels={"play", "reset"}; String[] fileMenuItem; String[] n_repsMenuItem={"1","5","20","100"}; String[] lambdaMenuItem={"0.3", "3", "5", "15", "30"}; String title, fileMenuName, lambdaMenuName, n_repsMenuName; private int reps =1; Image sharkgif, sharkgif1; MediaTracker tracker; // Tracker Class to wait for images int imageWidth , imageHeight; private int SMALL=0; private int LARGE=1; public void init() { JMenuBar menubar; DControlPanel cp; DMenu file, n_reps, lambdaMenu; model = new SharkModel(3.0); ocean = new OceanPanel(this, model); hp = new HistogramPanel(model); Color darkblue = new Color(0,0,127); hp.setBackground(darkblue); title = getResourceString("Title","Poisson Distribution"); dFrame = new DFrame(title, ocean,hp); buttonLabels = getResourceStringArray("controlPanelButtonNames",buttonLabels); cp = new DControlPanel( dFrame, this, buttonLabels); cp.setBackground(Color.white); cp.add(xc); // construct the menubar and add to the frame menubar = new JMenuBar(); dFrame.setJMenuBar(menubar); // construct "file" menu with "exit" fileMenuName = getResourceString("fileMenuName","file"); fileMenuItem=getResourceStringArray("fileMenuItems", fileMenuItem); file = new DMenu(this, fileMenuName, fileMenuItem); menubar.add(file); // rolls is a radio button menu n_repsMenuName = getResourceString("n_repsMenuName", "n_reps"); n_repsMenuItem = getResourceStringArray("n_repsMenuItems", n_repsMenuItem); n_reps = new DMenu(this, n_repsMenuName, n_repsMenuItem, true); n_reps.setSelectedItem(0); //n_reps=1 is default menubar.add(n_reps); // lambda sets the parameter of the poisson distribution lambdaMenuName = getResourceString("lambdaMenuName", lambdaMenuName); lambdaMenu = new DMenu(this,lambdaMenuName, lambdaMenuItem, true); lambdaMenu.setSelectedItem(1);// lambda=3 default menubar.add(lambdaMenu); dFrame.pack(); dFrame.validate(); setVisible(true); } /** * override DApplet.controlPanelButtonAction() to provide * correct behavior for buttons */ public void doControlPanelButtonAction(ActionEvent e){ String label = e.getActionCommand(); if (label.equals(buttonLabels[0])){//"play" model.fish(reps); ocean.updatePanel(model); xc.setText("X="+model.getNSharks()); xc.repaint(); hp.update(model); } else if (label.equals(buttonLabels[1])){//"reset" model.reset(); ocean.updatePanel(model); hp.update(model); } } /** * override DApplet.menuAction() to provide menu actions */ public void menuAction( String menuLabel, String itemLabel){ if (menuLabel.equals(fileMenuName)){//"file" if (itemLabel.equals(fileMenuItem[0])){//"exit" dFrame.dispose(); } } else if (menuLabel.equals(n_repsMenuName)){ reps = Integer.parseInt(itemLabel); } else if (menuLabel.equals(lambdaMenuName)){ lambda = Float.parseFloat(itemLabel); model.setLambda(lambda); model.reset(); } } public String getResourceString(String nm, String dfstr) { String str; try { str = this.resources.getString(nm); } catch (MissingResourceException mre) { str = dfstr; } return str; } public String[] getResourceStringArray(String nm, String[] dfstra) { String str; String[] stra; try { str = this.resources.getString(nm); stra = tokenize(str); } catch (MissingResourceException mre) { stra = dfstra; } //catch (String exception) return stra; } /** * Take the given string and chop it up into a series * of strings on whitespace boundries. This is useful * for trying to get an array of strings out of the * resource file. */ protected String[] tokenize(String input) { Vector v = new Vector(); StringTokenizer t = new StringTokenizer(input); String cmd[]; while (t.hasMoreTokens()) v.addElement(t.nextToken()); cmd = new String[v.size()]; for (int i = 0; i < cmd.length; i++){ cmd[i] = (String) v.elementAt(i); } return cmd; } /** * provides applet info */ public String getAppletInfo() { return "An demonstration of the Poisson distribution.\nAuthor: Charles S. Stanton"; } }