public ExponentialDistributionDemo() {
    super(new BorderLayout());

    Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
    for (int i = 0; i <= 50; i += 10) {
      labelTable.put(new Integer(i), new JLabel(String.valueOf(i / 10)));
    }

    lambdaSlider = new JSlider(0, 50, (int) Math.round(lambda * 10));
    lambdaSlider.addChangeListener(this);
    lambdaSlider.setLabelTable(labelTable);
    lambdaSlider.setMajorTickSpacing(10);
    lambdaSlider.setMinorTickSpacing(2);
    lambdaSlider.setPaintTicks(true);
    lambdaSlider.setPaintLabels(true);

    optionPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
    optionPane.setBorder(BorderFactory.createRaisedBevelBorder());
    optionPane.add(new JLabel("\u03BB:"));
    optionPane.add(lambdaSlider);

    add(optionPane, BorderLayout.NORTH);

    canvas = new JPanel(new GridLayout(2, 2));
    add(canvas, BorderLayout.CENTER);

    ExponentialDistribution dist = new ExponentialDistribution(lambda);

    double[][] p = new double[100][2];
    double[][] q = new double[100][2];
    for (int i = 0; i < p.length; i++) {
      p[i][0] = i / 20.0;
      p[i][1] = dist.p(p[i][0]);
      q[i][0] = i / 20.0;
      q[i][1] = dist.cdf(p[i][0]);
    }

    pdf = LinePlot.plot(p, Line.Style.SOLID, Color.BLUE);
    pdf.setTitle("PDF");
    canvas.add(pdf);

    cdf = LinePlot.plot(q, Line.Style.SOLID, Color.BLUE);
    cdf.setTitle("CDF");
    canvas.add(cdf);

    double[] data = new double[500];
    for (int i = 0; i < data.length; i++) {
      data[i] = dist.rand();
    }

    histogram = Histogram.plot(data, 20);
    histogram.setTitle("Histogram");
    canvas.add(histogram);

    qqplot = QQPlot.plot(data, dist);
    qqplot.setTitle("Q-Q Plot");
    canvas.add(qqplot);
  }
  @Override
  public void stateChanged(ChangeEvent e) {
    if (e.getSource() == lambdaSlider) {
      lambda = lambdaSlider.getValue() / 10.0;
      if (lambda == 0) lambda = 0.01;

      ExponentialDistribution dist = new ExponentialDistribution(lambda);

      double[][] p = new double[100][2];
      double[][] q = new double[100][2];
      for (int i = 0; i < p.length; i++) {
        p[i][0] = i / 20.0;
        p[i][1] = dist.p(p[i][0]);
        q[i][0] = i / 20.0;
        q[i][1] = dist.cdf(p[i][0]);
      }

      pdf.clear();
      pdf.line(p, Line.Style.SOLID, Color.BLUE);

      cdf.clear();
      cdf.line(q, Line.Style.SOLID, Color.BLUE);

      double[] data = new double[500];
      for (int i = 0; i < data.length; i++) {
        data[i] = dist.rand();
      }

      histogram.clear();
      histogram.histogram(data, 20, Color.BLUE);

      qqplot.clear();
      qqplot.add(new QQPlot(data, dist));
      canvas.repaint();
    }
  }