SnowCrash.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:2k
源码类别:
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) {}
- }
- }
- }