⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snowcrash.java

📁 一个采用J2ME的游戏程序,对游戏开发者很具有参考价值,适合用于二次开发
💻 JAVA
字号:
package rgbimagedemo;
import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class SnowCrash
    extends Canvas
    implements Runnable {
  private boolean mTrucking;
  private int[] mRGB;
  private Random mRandom;
  
  public SnowCrash() {
    mTrucking = true;
    mRandom = new Random();
    
    Thread t = new Thread(this);
    t.start();
  }
  
  protected void randomize() {
    if (mRGB == null) return;
    int bitCounter = 0;
    int r = 0;
    for (int i = 0; i < mRGB.length; i++) {
      // Get the next random int if necessary.
      if (bitCounter == 0) {
        r = mRandom.nextInt();
        bitCounter = 32;
      }
      // Get the next bit.
      int bit = r % 2;
      r = (r >> 1);
      bitCounter--;
      // Set the color to black or white.
      mRGB[i] = (bit == 0) ? 0xff000000 : 0xffffffff;
    }
  }
  
  public void stop() { mTrucking = false; }
  
  // Canvas abstract method
  
  public void paint(Graphics g) {
    int w = getWidth();
    int h = getHeight();
    int rw = 50;
    int rh = 50;
    int rx = (w - rw) / 2;
    int ry = (h - rh) / 2;
    
    if (mRGB == null) mRGB = new int[rw * rh];
    
    // Clear the screen.
    g.setColor(0xffffffff);
    g.fillRect(0, 0, w, h);
    
    // Draw the outline.
    g.setColor(0xff000000);
    g.drawRect(rx, ry, rw + 1, rh + 1);
    
    // Draw the snow.
    g.drawRGB(mRGB, 0, rw, rx + 1, ry + 1, rw, rh, false);
  }
  
  // Runnable method
  
  public void run() {
    // Attempt 12 fps.
    int interval = 1000 / 12;
    
    while (mTrucking) {
      randomize();
      repaint();
      try { Thread.sleep(interval); }
      catch (InterruptedException ie) {}
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -