home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue153 / java / PizzaSag.java < prev    next >
Encoding:
Java Source  |  1999-04-04  |  2.4 KB  |  98 lines

  1. /*
  2.  * @(#)PizzaSag.java    1.0 1999/04/03
  3.  * 
  4.  * Copyright (c) 1999, David Griffiths. All Rights Reserved.
  5.  * 
  6.  * This software is the proprietary information of David Griffiths.
  7.  * This source code may not be published or redistributed without the 
  8.  * express permission of the author. 
  9.  * 
  10.  * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 
  11.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  12.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  13.  * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
  14.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  15.  * THIS SOFTWARE OR ITS DERIVATIVES.
  16.  * 
  17.  */
  18.  
  19. import java.awt.Graphics;
  20. import java.awt.Image;
  21. import java.awt.Color;
  22. import java.lang.Math;
  23. import java.awt.*;
  24.  
  25. public class PizzaSag extends java.applet.Applet implements Runnable {
  26.     Image origImg, backImg;
  27.     int w1, h1;
  28.     Thread thrPizzaSag;
  29.  
  30.     public void start () {
  31.         if (thrPizzaSag == null) {
  32.             thrPizzaSag = new Thread (this);
  33.             thrPizzaSag.start ();
  34.         }
  35.     }
  36.  
  37.     public void stop () {
  38.         if (thrPizzaSag != null) {
  39.             thrPizzaSag.stop ();
  40.             thrPizzaSag = null;
  41.         }
  42.     }
  43.  
  44.     public void run () {
  45.         loadImage ();
  46.         int a, b, temp;
  47.         backImg = createImage (w1, h1);
  48.         Graphics backG = backImg.getGraphics();
  49.         backG.setColor(Color.white);
  50.         backG.drawImage(origImg, 0, 0, this);
  51.         while (thrPizzaSag != null) {
  52.             a = (int)(Math.random() * (double)w1);
  53.             b = (int)(Math.random() * (double)w1);
  54.             if (b < a) {
  55.                 temp = a;
  56.                 a = b;
  57.                 b = temp;
  58.             }
  59.             droop (backG, a, b);
  60.             try {
  61.                  Thread.sleep (50);
  62.             }
  63.             catch (InterruptedException e) {}
  64.             repaint ();
  65.         }
  66.     }
  67.  
  68.     public void loadImage () {
  69.         MediaTracker tracker = new MediaTracker(this);
  70.         String nextImage = getParameter ("image");
  71.         origImg = getImage (getDocumentBase(), nextImage);
  72.  
  73.         tracker.addImage(origImg, 0);
  74.  
  75.         try {
  76.             tracker.waitForID(0);
  77.         }
  78.         catch(InterruptedException e) {}
  79.  
  80.         w1 = origImg.getWidth (this);
  81.         h1 = origImg.getHeight (this);
  82.     }
  83.  
  84.     public void droop(Graphics g, int a, int b) {
  85.         g.copyArea (a, 0, b - a, h1, 0, 1);
  86.         g.drawLine (a, 0, b, 0);
  87.     }
  88.  
  89.     public void update (Graphics g) {
  90.         paint (g);
  91.     }
  92.  
  93.     public void paint (Graphics g) {
  94.         if (backImg != null)
  95.             g.drawImage (backImg, 0, 0, this); 
  96.     }
  97. }
  98.