package jmslexamples; import java.awt.*; import com.softsynth.jmsl.JMSLRandom; import com.softsynth.jmsl.util.EventDistributions; import com.softsynth.jmsl.view.JMSLScrollbar; import com.softsynth.jmsl.view.JMSLScrollbarProcessor; /** * Nick Didkovsky Sept. 10, 1997 * * EventDistributionsDemo.java * * Use scrollbars to compare mean event distributions between Exponential and * Myhill distributions. * * Revised 2/17/99 to comply with jdk1.1 event model. * * @author Nick Didkovsky and Phil Burk */ /* * (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved JMSL is based upon * HMSL (C) Phil Burk, Larry Polansky and David Rosenboom. */ public class EventDistributionsDemo extends java.applet.Applet implements JMSLScrollbarProcessor { EventCanvas myCanvas; JMSLScrollbar meanScrollbar; JMSLScrollbar ratioScrollbar; Label meanLabel; Label ratioLabel; Label numDrawnLabel; boolean myhillFlag = false; double mean = 40.0; double ratio = 1.1; public void init() { JMSLRandom.randomize(); } public void start() { meanScrollbar = new JMSLScrollbar(this, (int) mean, 0, 100); meanScrollbar.setSize(200, 20); meanLabel = new Label(" Mean= " + meanScrollbar.getValue()); Panel p1 = new Panel(); p1.setLayout(new GridLayout(1, 2)); p1.add(meanScrollbar); p1.add(meanLabel); ratioScrollbar = new JMSLScrollbar(this, (int) ratio * 10, 11, 1280); // tenths ratioScrollbar.setSize(200, 20); ratioLabel = new Label(" Ratio= " + (ratioScrollbar.getValue() / 10.0)); Panel p2 = new Panel(); p2.setLayout(new GridLayout(1, 2)); p2.add(ratioScrollbar); p2.add(ratioLabel); numDrawnLabel = new Label("Myhill Dist. events posted (black) = Logarithmic Dist. Events posted (red) = "); Panel p = new Panel(); p.setLayout(new GridLayout(3, 1)); p.add(p1); p.add(p2); p.add(numDrawnLabel); add("North", p); myCanvas = new EventCanvas(this); myCanvas.setSize(EventCanvas.WIDTH, EventCanvas.HEIGHT); add("Center", myCanvas); } public void stop() { } // JMSLScrollbar style of handling scrollbar events public void JMSLScrollbarValueChanged(JMSLScrollbar source) { if (source == meanScrollbar) { handleMean(); } if (source == ratioScrollbar) { handleRatio(); } } public double getMean() { return mean; } void handleMean() { mean = (double) meanScrollbar.getValue(); meanLabel.setText(" Mean= " + (int) mean); myCanvas.repaint(); } public double getRatio() { return ratio; } private String prettyString(double val) { return val + ""; // what's so pretty about that? } void handleRatio() { ratio = ((double) ratioScrollbar.getValue()) / 10; ratioLabel.setText(" Ratio= " + prettyString(ratio)); myCanvas.repaint(); } public void tellNumDrawn(int my, int lo) { numDrawnLabel.setText("Total Myhill events (black) = " + my + ", Total Exp events (red) = " + lo); } public static void main(String args[]) { EventDistributionsDemo applet = new EventDistributionsDemo(); Frame f = new Frame("Event Distributions Demo"); f.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); f.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); f.setSize(800, 300); f.setVisible(true); } } class EventCanvas extends Canvas { public static final int WIDTH = 500; // corresponds to 1.0 unit time public static final int HEIGHT = 100; EventDistributionsDemo parent; EventCanvas(EventDistributionsDemo par) { parent = par; } public void paint(Graphics g) { int xmyh = 0; int xlog = 0; int numDrawnLog = 0; int numDrawnMyhill = 0; g.setColor(Color.blue); g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1); for (int i = 0; i < (int) parent.getMean(); i++) { double entryDelayLog = EventDistributions.genEntryDelayLog(parent.getMean()); double entryDelayMyhill = EventDistributions.genEntryDelayMyhill(parent.getMean(), parent.getRatio()); xmyh += Math.round(entryDelayMyhill * (double) WIDTH); xlog += Math.round(entryDelayLog * (double) WIDTH); g.setColor(Color.black); if (xmyh < WIDTH) { g.drawLine(xmyh, HEIGHT / 2, xmyh, HEIGHT - 2); numDrawnMyhill++; } g.setColor(Color.red); if (xlog < WIDTH) { g.drawLine(xlog, 1, xlog, HEIGHT / 2); numDrawnLog++; } } parent.tellNumDrawn(numDrawnMyhill, numDrawnLog); } }