/* S0610.java * 1次元ランダムウォーク * (C) H.Ishikawa 2008 */ package simulation; import java.applet.*; import java.awt.*; import java.awt.event.*; import window.Window; public class S0610 extends Applet implements ActionListener { Button button0; public void init() { button0 = new Button(" 再実行 "); add(button0); button0.addActionListener(this); } public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); repaint(); } public void paint(Graphics g) { Window w ; w = new Window(); int SPACE = 30; int HIGHT = 400; int WIDTH = 640; long t = 0; long x = 0; long T_END = 1000; /* 終りの時刻 */ double P = 0.5; /* 確率 */ /*グラフィックの準備*/ w.setWindow(0, 0.0,-100.0,T_END,100.0, SPACE,HIGHT-SPACE,WIDTH-SPACE,SPACE); w.axis(0, "t", T_END/10, "x", 10, g); w.moveTo(0, 0.0, 0.0, g); /*メイン*/ for (t = 0; t < T_END; t ++) { if (P > Math.random()) { x = x + 1; } else { x = x - 1; } g.setColor(Color.green); w.lineTo(0, (double)(t), (double)(x), g); } stop(); } }