001/* 002 * RandomNormalDistribution.java 003 * Copyright (c) 2002 Tom B. Gutwin P.Eng 004 * $Header: f:/cvsroot2/open/projects/WebARTS/ca/bc/webarts/widgets/random/RandomNormalDistribution.java,v 1.1 2002/07/11 17:34:45 tgutwin Exp $ 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License 008 * as published by the Free Software Foundation; either version 2 009 * of the License, or any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 019 */ 020 021package ca.bc.webarts.widgets.random; 022 023import java.applet.*; 024import java.awt.*; 025 026import ca.bc.webarts.widgets.WrapperApp; 027 028/** 029 * This class allows you to randomly select numbers from a normally 030 * distributed sample. 031 * 032 * @created May 20, 2002 033 */ 034public class RandomNormalDistribution extends Applet 035{ 036 Scrollbar scrollbar1; 037 Checkbox checkbox1; 038 Panel panel1; 039 String s; 040 String s2; 041 int x; 042 int y; 043 int oldx; 044 int oldy; 045 int value; 046 int value1; 047 boolean nflag; 048 double d; 049 double d1; 050 double dummy; 051 double dubx; 052 double gama = 9.02; 053 int xaxis = 250; 054 int yaxis = 240; 055 056 057 /** Applet Init gets everything init and creates the distribution. */ 058 public void init() 059 { 060 System.out.println("Init"); 061 BorderLayout borderlayout = new BorderLayout(); 062 this.setLayout(borderlayout); 063 scrollbar1 = new Scrollbar(0, 2, 0, 5, 99); 064 checkbox1 065 = new Checkbox("Click to change ", null, true); 066 this.add("South", scrollbar1); 067 this.add("North", checkbox1); 068 s = "1"; 069 s2 = "1"; 070 Font font = new Font("TimesRoman", 1, 12); 071 this.setFont(font); 072 //this.resize(300, 300); 073 } 074 075 public void start() 076 { 077 System.out.println("Start"); 078 repaint(); 079 } 080 081 /** 082 * Drawns the Normal Dist. 083 * 084 * @param graphics Description of the Parameter 085 */ 086 public void paint(Graphics graphics) 087 { 088 graphics.drawLine(20, 20, 20, yaxis); 089 graphics.drawLine(20, yaxis, 250, yaxis); 090 boolean bool = checkbox1.getState(); 091 if (bool) 092 { 093 checkbox1.setLabel("Normal"); 094 nflag = true; 095 } 096 else 097 { 098 checkbox1.setLabel("Pearson3"); 099 nflag = false; 100 } 101 x = 0; 102 while (x < 240) 103 { 104 x++; 105 if (nflag) 106 { 107 d1 = 280.0 / Math.sqrt(6.283185307 * d); 108 y = (int) (d1 * Math.exp(-0.5 * (0.1 * (double) x - 10.0) 109 * (0.1 * (double) x - 10.0) 110 / (d * d))); 111 } 112 else 113 { 114 gama = 20.5 * d; 115 dubx = (double) x; 116 dubx = dubx - 75.5 + 5.0; 117 dummy = 1.0 + dubx / 41.0; 118 dummy 119 = 50.0 * Math.pow(dummy, gama); 120 y = (int) (4.0 * dummy * Math.exp(-0.5 * d * dubx)); 121 } 122 graphics.drawLine(20 + oldx, yaxis - oldy, 20 + x, yaxis - y); 123 oldx = x; 124 oldy = y; 125 } 126 oldx = 0; 127 oldy = 0; 128 if (nflag) 129 { 130 graphics.drawString("sigma = " + s, 95, 282); 131 } 132 else 133 { 134 s = String.valueOf(d / 2.0); 135 graphics.drawString("gamma = " + s, 95, 282); 136 } 137 graphics.drawString 138 ("0 Number 10", 139 18, yaxis + 18); 140 graphics.drawString("s", 1, yaxis / 2); 141 } 142 143 144 /** 145 * Gets the input from the scrollbar to set the Normal Dist Parameters. 146 * 147 * @param event Description of the Parameter 148 * @return Description of the Return Value 149 */ 150 public boolean handleEvent(Event event) 151 { 152 int i = scrollbar1.getValue(); 153 d = (double) i / 25.0; 154 s = String.valueOf(d); 155 this.repaint(); 156 return true; 157 } 158 159 160 static public void main(String[] args) 161 { 162 System.out.println("Drawing Normal"); 163 WrapperApp appFrame = new WrapperApp("Normal Distribution", 164 new RandomNormalDistribution()); 165 //appFrame.pack(); 166 //appFrame.show(); 167 //appFrame.toFront(); 168 appFrame.setSize(300,300); 169 //appFrame.setVisible(true); 170 171 } 172 173} 174 175