import java.awt.*; import java.util.concurrent.*; /** * A process that grabs Box's and draws a row of them */ public class Row implements Runnable { private Canvas canvas; private Rectangle loc; private CopyOnWriteArrayList boxes = new CopyOnWriteArrayList (); public Row (Rectangle loc, Canvas canvas) { this.loc = loc; this.canvas = canvas; // Start a new thread to run us Thread thread = new Thread (this); thread.start(); } /** * Method for Runnable */ public void run () { for (int i=0; i<10; i++) { boxes.add (new Box ()); // Since we've just changed our box list, request a repaint canvas.repaint (); try { Thread.sleep (2000 + (int) (500 * Math.random ())); // 2-2.5 seconds } catch (InterruptedException e) { } } System.out.println ("Row at y = " + loc.y + " finished"); } public void draw (Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.draw (loc); // We tell each Box where to draw itself int boxsize = loc.height-2*5; Rectangle boxloc = new Rectangle (loc.x+10, loc.y+5, boxsize, boxsize); // Draw each of our Box's for (Box b: boxes) { b.draw (g, boxloc); boxloc.x = boxloc.x + boxsize + 10; } } }