Threads1

File: java/Threads1/Main.java

import java.awt.*;
import javax.swing.*;
import java.util.*;

/**
 * Main program for Threads demo
 */
public class Main extends JFrame {
    /** Our Row's */
    private Vector<Row> rows = new Vector<Row> ();

    public static void main (String [] args) {
	new Main ();
    }

    public Main () {
	// Window setup
	setSize (500, 500);
	Container content = getContentPane();
	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

	// Our canvas
	MyCanvas canvas = new MyCanvas (this);
	content.add (canvas);

	// Show our window
	setVisible (true);

	// Now create our Row's and start() them
	for (int i=0; i<5; i++) {
	    Row r = new Row (new Rectangle (10, 100+i*75, 500-40, 40), canvas);
	    rows.add (r);
	}
    }

    public void drawCanvas (Graphics g) {
	// Draw each of our Row's
	for (Row r: rows) r.draw (g);
    }
}

File: java/Threads1/MyCanvas.java

import java.awt.*;
import javax.swing.*;

/**
 * Canvas, is pretty much a captive of Main
 */
public class MyCanvas extends JComponent {
    private Main parent;

    public MyCanvas (Main parent) {
	this.parent = parent;
    }

    /** Tell parent to do the job */
    public void paintComponent (Graphics g) {
	parent.drawCanvas (g);
    }
}

File: java/Threads1/Row.java

import java.awt.*;
import java.util.*;

/**
 * A process that grabs Box's and draws a row of them
 */
public class Row implements Runnable {
    private MyCanvas canvas;
    private Rectangle loc;
    private Vector<Box> boxes = new Vector<Box> ();

    public Row (Rectangle loc, MyCanvas 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.currentThread().sleep (1000); // 1 second
	    } 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;
	}
    }
}

File: java/Threads1/Box.java

import java.awt.*;
import javax.swing.*;

/**
 * A little box with a number inside it,
 * belongs to a Row
 */
public class Box {
    /** Classvar counter for next ID number */
    static private int nextID = 1;
    
    /** Our own ID number */
    private int id;

    public Box () {
	// Take next available ID number
	id = nextID++;
    }

    public void draw (Graphics g, Rectangle loc) {
	Graphics2D g2 = (Graphics2D) g;

	g2.draw (loc);
	g2.drawString (String.valueOf (id), loc.x+5, loc.y+15);
    }
}