samer@0: package samer.applet; samer@0: samer@0: import java.awt.*; samer@0: import java.awt.event.*; samer@0: import java.util.Random; samer@0: import samer.core.*; samer@0: import samer.core.util.heavy.VCanvas; samer@0: import samer.tools.*; samer@0: samer@0: public class Sierpinski extends VCanvas implements Task, Agent samer@0: { samer@0: Random rnd=new Random(); samer@0: RThread rthread; samer@0: int x=0, y=0, w=0, h=0; samer@0: samer@0: public Sierpinski() samer@0: { samer@0: Shell.trace("creating RThread"); samer@0: rthread = new RThread(this); samer@0: samer@0: Shell.trace("obtaining window"); samer@0: Shell.Window win=Shell.getWindow("Sierpinski"); samer@0: win.container().setLayout(new BorderLayout()); samer@0: win.container().add(this,"Center"); samer@0: win.expose(); samer@0: win.addWindowListener( new WindowAdapter() { samer@0: public void windowClosing(WindowEvent e) { samer@0: Shell.print("Sierpinski terminating"); samer@0: // how can I destroy the RThread? samer@0: // exposing commands creates actionListeners samer@0: // which refer to the Agent, thus stopping the samer@0: // Agent from being garbage-collected samer@0: dispose(); samer@0: } samer@0: } ); samer@0: samer@0: Shell.trace("exposing commands"); samer@0: exposeCommands(rthread); samer@0: exposeCommands(this); samer@0: Shell.registerAgent(this); samer@0: Shell.exposeCommands(rthread); samer@0: Shell.exposeCommands(this); samer@0: } samer@0: samer@0: public void dispose() samer@0: { samer@0: Shell.deregisterAgent(this); samer@0: rthread.dispose(); samer@0: } samer@0: samer@0: public void getCommands(Agent.Registry r) { r.add("clear"); } samer@0: public void execute(String cmd, Environment env) throws Exception{ samer@0: if (cmd.equals("clear")) repaint(); samer@0: } samer@0: samer@0: public void run() throws Exception samer@0: { samer@0: int j=Math.abs(rnd.nextInt())%3; samer@0: samer@0: if (j==0) { x=x/2; y=y/2; } samer@0: else if (j==1) { x=x/2; y=h+y/2; } samer@0: else { x=w+x/2; y=y/2; } samer@0: samer@0: graphics.fillRect( x, y, 1, 1); samer@0: } samer@0: samer@0: public void stopping() { Shell.print("stopping"); } samer@0: public void starting() { Shell.print("starting"); } samer@0: public void realized() { Shell.print("realized"); sized(); } samer@0: public void sized() { w=width/2; h=height/2; } samer@0: }