annotate src/samer/applet/Sierpinski.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 package samer.applet;
samer@0 2
samer@0 3 import java.awt.*;
samer@0 4 import java.awt.event.*;
samer@0 5 import java.util.Random;
samer@0 6 import samer.core.*;
samer@0 7 import samer.core.util.heavy.VCanvas;
samer@0 8 import samer.tools.*;
samer@0 9
samer@0 10 public class Sierpinski extends VCanvas implements Task, Agent
samer@0 11 {
samer@0 12 Random rnd=new Random();
samer@0 13 RThread rthread;
samer@0 14 int x=0, y=0, w=0, h=0;
samer@0 15
samer@0 16 public Sierpinski()
samer@0 17 {
samer@0 18 Shell.trace("creating RThread");
samer@0 19 rthread = new RThread(this);
samer@0 20
samer@0 21 Shell.trace("obtaining window");
samer@0 22 Shell.Window win=Shell.getWindow("Sierpinski");
samer@0 23 win.container().setLayout(new BorderLayout());
samer@0 24 win.container().add(this,"Center");
samer@0 25 win.expose();
samer@0 26 win.addWindowListener( new WindowAdapter() {
samer@0 27 public void windowClosing(WindowEvent e) {
samer@0 28 Shell.print("Sierpinski terminating");
samer@0 29 // how can I destroy the RThread?
samer@0 30 // exposing commands creates actionListeners
samer@0 31 // which refer to the Agent, thus stopping the
samer@0 32 // Agent from being garbage-collected
samer@0 33 dispose();
samer@0 34 }
samer@0 35 } );
samer@0 36
samer@0 37 Shell.trace("exposing commands");
samer@0 38 exposeCommands(rthread);
samer@0 39 exposeCommands(this);
samer@0 40 Shell.registerAgent(this);
samer@0 41 Shell.exposeCommands(rthread);
samer@0 42 Shell.exposeCommands(this);
samer@0 43 }
samer@0 44
samer@0 45 public void dispose()
samer@0 46 {
samer@0 47 Shell.deregisterAgent(this);
samer@0 48 rthread.dispose();
samer@0 49 }
samer@0 50
samer@0 51 public void getCommands(Agent.Registry r) { r.add("clear"); }
samer@0 52 public void execute(String cmd, Environment env) throws Exception{
samer@0 53 if (cmd.equals("clear")) repaint();
samer@0 54 }
samer@0 55
samer@0 56 public void run() throws Exception
samer@0 57 {
samer@0 58 int j=Math.abs(rnd.nextInt())%3;
samer@0 59
samer@0 60 if (j==0) { x=x/2; y=y/2; }
samer@0 61 else if (j==1) { x=x/2; y=h+y/2; }
samer@0 62 else { x=w+x/2; y=y/2; }
samer@0 63
samer@0 64 graphics.fillRect( x, y, 1, 1);
samer@0 65 }
samer@0 66
samer@0 67 public void stopping() { Shell.print("stopping"); }
samer@0 68 public void starting() { Shell.print("starting"); }
samer@0 69 public void realized() { Shell.print("realized"); sized(); }
samer@0 70 public void sized() { w=width/2; h=height/2; }
samer@0 71 }