home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch04 / MultiThread.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  5.1 KB  |  233 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3.  
  4. /**
  5.  * class LineColors holds 24 color values
  6.  */
  7. class LineColors {
  8.  
  9. /**
  10.  * color[] array holds the colors to be used
  11.  */
  12. Color color[];
  13.  
  14. /**
  15.  * class constructor
  16.  * initializes the color array using an arbitrary algorithm
  17.  */
  18. public LineColors () {
  19.        color = new Color[24];
  20.        int i, rgb;
  21.  
  22.        rgb = 0xff;
  23.        for (i=0; i<24; i+=1) {
  24.               color[i] = new Color (rgb);
  25.               rgb <<= 1;
  26.               if ((rgb & 0x1000000) != 0) {
  27.                      rgb |= 1;
  28.                      rgb &= 0xffffff;
  29.               }
  30.        }
  31. }
  32. }
  33.  
  34. /**
  35.  * class describing one line segment
  36.  */
  37. class Segment {
  38.  
  39. /*
  40.  * x1, y1 - starting coordinates for this segment
  41.  * x2, y2 - ending coordinates for this segment
  42.  * dx1,...dy2 - velocities for the endpoints
  43.  * whichcolor - the current index into color array
  44.  * width, height - width and height of bounding panel
  45.  * LC - instance of LineColors class
  46.  */
  47. double x1, y1, x2, y2;
  48. double dx1, dy1, dx2, dy2;
  49. int whichcolor, width, height;
  50. LineColors LC;
  51.  
  52. /**
  53.  * class constructor
  54.  * initialize endpoints and velocities to random values
  55.  * @param w - width of bounding panel
  56.  * @param h - height of bounding panel
  57.  * @param c - starting color index
  58.  * @param lc - instance of LineColors class
  59.  */
  60. public Segment (int w, int h, int c, LineColors lc) {
  61.  
  62.        whichcolor = c;
  63.        width = w;
  64.        height = h;
  65.        LC = lc;
  66.        x1 = (double) w * Math.random ();
  67.        y1 = (double) h * Math.random ();
  68.        x2 = (double) w * Math.random ();
  69.        y2 = (double) h * Math.random ();
  70.  
  71.        dx1 = 5 - 10 * Math.random ();
  72.        dy1 = 5 - 10 * Math.random ();
  73.        dx2 = 5 - 10 * Math.random ();
  74.        dy2 = 5 - 10 * Math.random ();
  75. }
  76.  
  77. /*
  78.  * increment color index
  79.  * calculate the next endpoint position for this segment
  80.  */
  81. void compute () {
  82.  
  83.        whichcolor += 1;
  84.        whichcolor %= 24;
  85.        
  86.        x1 += dx1;
  87.        y1 += dy1;
  88.        x2 += dx2;
  89.        y2 += dy2;
  90.  
  91.        if (x1 < 0 || x1 > width) dx1 = -dx1;
  92.        if (y1 < 0 || y1 > height) dy1 = -dy1;
  93.        if (x2 < 0 || x2 > width) dx2 = -dx2;
  94.        if (y2 < 0 || y2 > height) dy2 = -dy2;
  95. }
  96.  
  97. /**
  98.  * draw the line segment using the current color
  99.  * @param g - destination graphics object
  100.  */
  101. void paint (Graphics g) {
  102.  
  103.        g.setColor (LC.color [whichcolor]);
  104.        g.drawLine ((int) x1, (int) y1, (int) x2, (int) y2);
  105. }
  106. }
  107.  
  108. /**
  109.  * The applet/application proper
  110.  */
  111. class Lines extends Panel implements Runnable {
  112.  
  113. /*
  114.  * width, height - width and height of bounding panel
  115.  * Nlines - number of line segments to be displayed
  116.  * lines - array of instances of Segment class
  117.  * LC - instance of LineColors class
  118.  */
  119. int width, height;
  120. final int NLines = 4;
  121. Segment lines[] = new Segment[NLines];
  122. LineColors LC = new LineColors ();
  123.  
  124. /*
  125.  * instance of thread for this line
  126.  */
  127. Thread thread;
  128.  
  129. /**
  130.  * init is called when the applet is loaded
  131.  * save the width and height
  132.  * create instances of Segment class
  133.  */
  134. public void init () {
  135.  
  136.        
  137.        width = 200;
  138.        height = 200;
  139.        
  140.        thread = new Thread (this);
  141.        thread.start ();
  142.  
  143.        int i;
  144.  
  145.       for (i=0; i<NLines; i+=1)
  146.               lines[i] = new Segment (width, height, (2*i) % 24, LC);
  147. }
  148.  
  149. /**
  150.  * recompute the next endpoint coordinates for each line
  151.  * invoke paint() method for each line
  152.  * @param g - destination graphics object
  153.  */
  154. public void paint (Graphics g) 
  155. {
  156.  
  157.        int i; 
  158.        g.setColor (Color.black);
  159.        g.drawRect (0, 0, width-1, height-1);
  160.        for (i=0; i<NLines; i+=1) {
  161.               lines[i].compute ();
  162.               lines[i].paint (g);
  163.        }
  164. }
  165.  
  166. /*
  167.  * the thread proper
  168.  * calls paint() every 50ms
  169.  */
  170. public void run()
  171. {
  172.        Graphics g = getGraphics();
  173.        while (true) {
  174.               paint (g);
  175.               try {
  176.                      Thread.sleep (50);
  177.               } catch(InterruptedException e) { }
  178.        }
  179. }
  180. }
  181.  
  182. /*
  183.  * the applet/application proper
  184.  * creates two instances of Lines and starts them
  185.  * as separate threads
  186.  */
  187. public class MultiThread extends Applet {
  188.  
  189. /*
  190.  * the instances of Lines
  191.  */
  192. Lines lines1; 
  193. Lines lines2;
  194.  
  195. /*
  196.  * called when the applet is loaded
  197. */
  198. public void init () {
  199.  
  200.        setLayout (new GridLayout (2, 1, 0, 0));
  201.  
  202.        lines1 = new Lines ();
  203.        lines2 = new Lines ();
  204.        add (lines1);
  205.        add (lines2); 
  206.        lines1.setSize (200, 100);
  207.        lines2.setSize (200, 100);
  208.        lines1.init ();
  209.        lines2.init ();
  210. }
  211.  
  212. /**
  213.  * application entry point, unused when run as an applet
  214.  * create window frame and add applet inside
  215.  * @param args[] - command-line arguments
  216.  */
  217. public static void main (String args[]) {
  218.  
  219.        Frame f = new Frame ("Colored lines"); 
  220.        f.setLayout (new GridLayout (2, 1, 0, 0));
  221.  
  222.        f.setSize (200, 200);
  223.  
  224.        Lines lines1 = new Lines ();
  225.        Lines lines2 = new Lines ();
  226.        f.add (lines1);
  227.        f.add (lines2);
  228.        f.show ();
  229.        lines1.init ();
  230.        lines2.init ();
  231. }
  232. }
  233.