SnowCrash.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:2k
源码类别:

J2ME

开发平台:

Java

  1. package rgbimagedemo;
  2. import java.util.Random;
  3. import javax.microedition.lcdui.*;
  4. import javax.microedition.midlet.*;
  5. public class SnowCrash
  6.     extends Canvas
  7.     implements Runnable {
  8.   private boolean mTrucking;
  9.   private int[] mRGB;
  10.   private Random mRandom;
  11.   
  12.   public SnowCrash() {
  13.     mTrucking = true;
  14.     mRandom = new Random();
  15.     
  16.     Thread t = new Thread(this);
  17.     t.start();
  18.   }
  19.   
  20.   protected void randomize() {
  21.     if (mRGB == null) return;
  22.     int bitCounter = 0;
  23.     int r = 0;
  24.     for (int i = 0; i < mRGB.length; i++) {
  25.       // Get the next random int if necessary.
  26.       if (bitCounter == 0) {
  27.         r = mRandom.nextInt();
  28.         bitCounter = 32;
  29.       }
  30.       // Get the next bit.
  31.       int bit = r % 2;
  32.       r = (r >> 1);
  33.       bitCounter--;
  34.       // Set the color to black or white.
  35.       mRGB[i] = (bit == 0) ? 0xff000000 : 0xffffffff;
  36.     }
  37.   }
  38.   
  39.   public void stop() { mTrucking = false; }
  40.   
  41.   // Canvas abstract method
  42.   
  43.   public void paint(Graphics g) {
  44.     int w = getWidth();
  45.     int h = getHeight();
  46.     int rw = 50;
  47.     int rh = 50;
  48.     int rx = (w - rw) / 2;
  49.     int ry = (h - rh) / 2;
  50.     
  51.     if (mRGB == null) mRGB = new int[rw * rh];
  52.     
  53.     // Clear the screen.
  54.     g.setColor(0xffffffff);
  55.     g.fillRect(0, 0, w, h);
  56.     
  57.     // Draw the outline.
  58.     g.setColor(0xff000000);
  59.     g.drawRect(rx, ry, rw + 1, rh + 1);
  60.     
  61.     // Draw the snow.
  62.     g.drawRGB(mRGB, 0, rw, rx + 1, ry + 1, rw, rh, false);
  63.   }
  64.   
  65.   // Runnable method
  66.   
  67.   public void run() {
  68.     // Attempt 12 fps.
  69.     int interval = 1000 / 12;
  70.     
  71.     while (mTrucking) {
  72.       randomize();
  73.       repaint();
  74.       try { Thread.sleep(interval); }
  75.       catch (InterruptedException ie) {}
  76.     }
  77.   }
  78. }