import java.applet.Applet; import java.awt.event.*; import java.awt.*; import java.util.*; public class randoma extends Applet{ TextField txt; Button b0; MyCanvas cnv; public void init() { setLayout(new BorderLayout()); String s = getParameter("comment"); if(s==null) s = "Good morning."; add("North", new Label(s)); cnv= new MyCanvas(170, 200); add("Center", cnv); txt = new TextField("1000"); add("South", txt); b0 = new Button("again"); b0.addActionListener(new myListener(this)); add("West", b0); } } //JDK1.1.のevent model class myListener implements ActionListener{ randoma a; myListener(randoma a){ this.a = a;} public void actionPerformed(ActionEvent e){ a.cnv.set(Integer.parseInt(a.txt.getText())); } } class MyCanvas extends Canvas{ int i, x, y, widthx, widthy, counter; Random r; /* public void MyCanvas(int widthx, int widthy) { * この宣言では、voidとして戻り値の型を宣言しているので * コンストラクタではなくメソッドになります. */ MyCanvas(int widthx, int widthy) { r = new Random(); // Randomクラスのインスタンス作成 this.widthx = widthx; this.widthy = widthy; setSize(widthx, widthy); setBackground(Color.white); setForeground(Color.darkGray); } void set(int counter){ this.counter = counter; repaint(); } public void paint(Graphics g) { g.drawRect(0,0,widthx,widthy); for(i = 0; i < counter; i=i+1) { x = (int)(widthx * (0.2 * r.nextGaussian() + 0.5)); y = (int)(widthy * (0.2 * r.nextGaussian() + 0.5)); g.fillOval(x,y, 3, 3); } } }