// JRobo Example showing collection of JRobo's
// each one moves to a random position
// & draws a random sized rectangle
// Note: You may need to flush the cache in order to see changes after recompiling
// to do this, open the Java Console (from task bar) & press "x".
// Author: David 16/12/06
import java.awt.*;
import java.applet.*;
import java.util.*;
public class JRoboTest extends Applet
{
static Random r = new Random();
ArrayList workers;
public void paint( Graphics g ) {
// create a collection of JRobo workers
workers = new ArrayList();
for ( int i = 0; i < 15; i++)
{
workers.add( new JRobo(g) );
}
// and have them move to random places
// & draw a random sized rectangle
for ( int i = 0; i < workers.size(); i++)
{
JRobo tmpJRobo = (JRobo) workers.get(i);
move( tmpJRobo, r.nextInt(500) - 250, r.nextInt(500) - 250 );
tmpJRobo.rect( r.nextInt(100), r.nextInt(100) );
// System.out.println( tmpJRobo.x + ":" + tmpJRobo.y);
}
System.out.println( "Number of workers = " + workers.size() );
} // end of paint method
// move robo invisibly +x, +y from current location,
// pre-cond: facing north, in drawing mode
// post-cond: as pre but moved +x +y.
private void move( JRobo robo, int x, int y)
{
robo.p();
robo.r( 90);
robo.f( x);
robo.l( 90);
robo.f( y);
robo.p();
} // end of move method
} // end JRoboTest class