view src/samer/applet/Sierpinski.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line source
package samer.applet;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import samer.core.*;
import samer.core.util.heavy.VCanvas;
import samer.tools.*;

public class Sierpinski extends VCanvas implements Task, Agent
{
	Random		rnd=new Random();
	RThread		rthread;
	int			x=0, y=0, w=0, h=0;

	public Sierpinski()
	{
		Shell.trace("creating RThread");
		rthread = new RThread(this);

		Shell.trace("obtaining window");
		Shell.Window win=Shell.getWindow("Sierpinski");
		win.container().setLayout(new BorderLayout());
		win.container().add(this,"Center");
		win.expose();
		win.addWindowListener( new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				Shell.print("Sierpinski terminating");
				// how can I destroy the RThread?
				// exposing commands creates actionListeners
				// which refer to the Agent, thus stopping the
				// Agent from being garbage-collected
				dispose();
			}
		} );

		Shell.trace("exposing commands");
		exposeCommands(rthread);
		exposeCommands(this);
		Shell.registerAgent(this);
		Shell.exposeCommands(rthread);
		Shell.exposeCommands(this);
	}

	public void dispose()
	{
		Shell.deregisterAgent(this);
		rthread.dispose();
	}

	public void getCommands(Agent.Registry r) { r.add("clear"); }
	public void execute(String cmd, Environment env) throws Exception{
		if (cmd.equals("clear")) repaint();
	}

	public void run() throws Exception
	{
		int    j=Math.abs(rnd.nextInt())%3;

		if (j==0)      { x=x/2; y=y/2; }
		else if (j==1) { x=x/2; y=h+y/2; }
		else           { x=w+x/2; y=y/2; }

		graphics.fillRect( x, y, 1, 1);
	}

 	public void stopping() { Shell.print("stopping"); }
	public void starting() { Shell.print("starting"); }
	public void realized() { Shell.print("realized"); sized(); }
	public void sized() { w=width/2; h=height/2; }
}