Advertisement
Guest User

alcsnow.class

a guest
Dec 6th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.MediaTracker;
  6. import java.util.Random;
  7.  
  8. public class alcsnow extends Applet implements Runnable
  9. {
  10.   Thread mainThread;
  11.   Image offScrn;
  12.   Graphics offGrph;
  13.   Random rand;
  14.   int stopFlag;
  15.   long stopTime;
  16.   int[] snowX;
  17.   int[] snowY;
  18.   int snows;
  19.   int wind;
  20.   int threadSleep;
  21.   Dimension dim;
  22.   Image[] gAlc;
  23.   MediaTracker mt;
  24.  
  25.   public void init()
  26.   {
  27.     rand = new Random();
  28.     dim = size();
  29.     offScrn = createImage(dim.width, dim.height);
  30.     offGrph = offScrn.getGraphics();
  31.    
  32.  
  33.     String str = getParameter("snows");
  34.     if (str == null) {
  35.       snows = 100;
  36.     } else {
  37.       snows = Integer.valueOf(str).intValue();
  38.     }
  39.     if (snows > 500) {
  40.       snows = 500;
  41.     }
  42.    
  43.  
  44.     str = getParameter("threadsleep");
  45.     if (str == null) {
  46.       threadSleep = 80;
  47.     } else {
  48.       threadSleep = Integer.valueOf(str).intValue();
  49.     }
  50.     if (threadSleep < 10) {
  51.       threadSleep = 10;
  52.     } else if (threadSleep > 1000) {
  53.       threadSleep = 1000;
  54.     }
  55.    
  56.     snowX = new int[snows];
  57.     snowY = new int[snows];
  58.     for (int i = 0; i < snows; i++) {
  59.       snowX[i] =
  60.         (rand.nextInt() % (dim.width / 2) + dim.width / 2);
  61.       snowY[i] =
  62.         (rand.nextInt() % (dim.height / 2) + dim.height / 2);
  63.     }
  64.    
  65.  
  66.     str = getParameter("graphic");
  67.     if (str == null) {
  68.       str = getParameter("graph");
  69.       if (str == null) {
  70.         str = getParameter("grph");
  71.         if (str == null) {
  72.           str = "tento1.gif";
  73.         }
  74.       }
  75.     }
  76.     mt = new MediaTracker(this);
  77.     gAlc = new Image[1];
  78.     gAlc[0] = getImage(getDocumentBase(), str);
  79.     mt.addImage(gAlc[0], 0);
  80.     try {
  81.       mt.waitForID(0);
  82.     } catch (InterruptedException localInterruptedException) {
  83.       return;
  84.     }
  85.    
  86.     stopFlag = 0;
  87.   }
  88.  
  89.   public void start() {
  90.     if (mainThread == null) {
  91.       mainThread = new Thread(this);
  92.       mainThread.start();
  93.     }
  94.   }
  95.  
  96.   public void stop() {
  97.     mainThread = null;
  98.   }
  99.  
  100.   public void run() {
  101.     while (mainThread != null) {
  102.       try {
  103.         Thread.sleep(threadSleep);
  104.       }
  105.       catch (InterruptedException localInterruptedException) {
  106.         return;
  107.       }
  108.       repaint();
  109.     }
  110.   }
  111.  
  112.   public void paint(Graphics paramGraphics)
  113.   {
  114.     offGrph.setColor(java.awt.Color.black);
  115.     offGrph.fillRect(0, 0, dim.width, dim.height);
  116.    
  117.     offGrph.drawImage(gAlc[0], 0, 0, this);
  118.    
  119.     drawBackSnow();
  120.    
  121.     paramGraphics.drawImage(offScrn, 0, 0, null);
  122.   }
  123.  
  124.   public void update(Graphics paramGraphics)
  125.   {
  126.     paint(paramGraphics);
  127.   }
  128.  
  129.  
  130.  
  131.  
  132.   public void drawBackSnow()
  133.   {
  134.     offGrph.setColor(java.awt.Color.white);
  135.     for (int i = 0; i < snows; i++) {
  136.       offGrph.fillRect(snowX[i], snowY[i], 1, 1);
  137.       snowX[i] += rand.nextInt() % 2 + wind;
  138.       snowY[i] += (rand.nextInt() % 6 + 5) / 5 + 1;
  139.       if (snowX[i] >= dim.width) {
  140.         snowX[i] = 0;
  141.       }
  142.       if (snowX[i] < 0) {
  143.         snowX[i] = (dim.width - 1);
  144.       }
  145.       if ((snowY[i] >= dim.height) ||
  146.         (snowY[i] < 0)) {
  147.         snowX[i] = Math.abs(rand.nextInt() % dim.width);
  148.         snowY[i] = 0;
  149.       }
  150.     }
  151.     switch (rand.nextInt() % 100) {
  152.     case -2:
  153.       wind = -2;
  154.       return;
  155.     case -1:
  156.       wind = -1;
  157.       return;
  158.     case 0:
  159.       wind = 0;
  160.       return;
  161.     case 1:
  162.       wind = 1;
  163.       return;
  164.     case 2:
  165.       wind = 2; return;
  166.     }
  167.   }
  168.  
  169.   public alcsnow() {}
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement