线程在j2me开发中是不可或缺的一部分,j2me继承了j2se中关于java.lang中的runnable接口,以及thread类。但是,由于j2me应用的特殊性,j2me
程序中去除了部分api,没有线程组的概念,也没有daemon线程。
今天,我们从一个例子出发,来学习j2me当中的线程的概念。我们选取的例子是俄罗斯方块。首先,有一些要注意的事项:
1.注意一点,要注意在j2me中不要使用浮点数,这样可以通过编译,但是不能通过预验证。因为一般手持设备都无法负担浮点运算的高负荷。
2.在j2me程序当中,绝大多数的空间为图片所占有,我们可以看到,今天我们的例子没有任何图片,仅仅5k,如果是开发产品,不可避免的要使用图片,
但是尽量使用压缩率高的png图片,而且不要太过复杂,因为复杂的图片会使得图片变得很大。
3.在程序中尽量使用常量特别是位置信息,这样当作修改的时候只要改一个量就可以了,而且当移植到其他平台的时候也会减少很多工作量.还有就是颜色
信息等.不用每次记忆,重新构造,因为j2me中的颜色和j2se的不太一样.没有常量定义.
4.游戏产品经常需要保护版权,而当今的很多反编译工具可以轻而易举地把jar文件的内容反编译过来,因此可以对程序进行模糊化处理,使得无法反编译
或者反编译后无法理解.可以右键点击项目,在属性中选择build|obfuscating,选择模糊化级别.
5.讲解中我们都使用netbeans作为开发平台,有关安装事宜请访问www.netbeans.org.

好,我们开始吧。
a. 首先,建立一个新的移动应用程序项目,取名tetris, 不要自动创建hello程序,选取midp1.0和cldc1.0.
b. 新建一个包,方法是右键点击项目,选取new|java package,取名tetris.
c. 新建一个midlet,同上,选取new|java midlet, 取名tetrismidlet.
d. 我们需要一个能够显示游戏的canvas, 因此新建一个class名叫tetriscanvas, 在tetrismidlet.java中将tetriscanvas作为当前可以显示的元素:
现在的tetrismidlet.java如下:
package tetris;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author lin
* @version
*/
public class tetrismidlet extends midlet {
public void startapp() {
display display = display.getdisplay( this );
// tetriscanvas extends canvas which extends displayable so it can
// be displayed directly
display.setcurrent( new tetriscanvas());
}
public void pauseapp() {
}
public void destroyapp(boolean unconditional) {
}
}
由于tetriscanvas继承了canvas,所以可以被tetrismidlet所显示.
e. 这里,我们需要将tetriscanvas继承canvas,并且实现canvas的接口函数paint(),我们现在有了一个tetriscanvas的框架了。
package tetris;
import javax.microedition.lcdui.*;
public class tetriscanvas extends canvas {
/** creates a new instance of tetriscanvas */
public tetriscanvas() {
}
protected void paint(graphics g){
}
}
下面我们需要使得tetriscanvas具有thread的特性,这里有两种方法,一种是让tetriscanvas继承thread类,然后生成它的实例,但是由于它已经
继承了canvas类,而java中不允许多重继承,因此,我们在编程当中通常采取第二种做法,也就是让它实现runnable接口,在成员中声明一个thread
成员,实例生成指向自己,然后实现run方法。
也就是这样:
public class tetriscanvas extends canvas implements runnable {
private thread blocker = null;
…
public tetriscanvas(){
blocker = new thread(this);
blocker.start();
}
…
public void run(){
while (blocker != null) {
}
}
…
}
f. 程序逻辑:下面给出程序清单。程序中我们使用一个数组来存储方块的信息,一共有十九种,还有一个数组来存储当前的画面方格的内容.在程序中
有一个paint方法来实现重画,注意绘制的先后次序,当程序规模变得很大的时候,重画的效率就非常重要,需要进行优化.我们在程序中使用了背景,
在没有背景的情况下,程序仅5k,采用背景后,程序47k,可见对图片的优化至关重要.
/*
* tetriscanvas.java
*
* created on 2005年7月13日, 上午11:31
*
* to change this template, choose tools | options and locate the template under
* the source creation and management node. right-click the template and choose
* open. you can then make changes to the template in the source editor.
*/
package tetris;
import java.util.*;
import java.lang.math;
import javax.microedition.lcdui.*;
/**
*
* @author lin
*/
public class tetriscanvas extends canvas implements runnable{
private thread blocker = null;
private random generator;
private int futureblocktype, blocktype,lasttype,lastx,lasty,blockx,blocky ;
private int blocklines,blockscore;
private int blockspeed,curspeed;
private static final int color_gray = 0x00eeeeee;
private static final int color_red = 0x00ff0000;
private static final int color_black = 0x00000000;
private static final int color_white = 0x00ffffff;
private static final int color_blue = 0x000000ff;
private static final int color_light_blue= 0x0089a5d1;
private static final int color_dark_gray = 0x00808080;
private static final int color_background= color_light_blue;
private static final int block_size = 7;
private static final int canvas_size_width = 12;
private static final int canvas_size_height = 22;
private static final int canvas_offset_x = 5;
private static final int canvas_offset_y = 7;
/**
* the paint status.
*/
boolean isclear = false;
boolean isdown = false;
boolean isdel = false;
/**
* the block information matrix.
*/
int blockinfo[][]={{1,0,1,1,1,2,1,3,0xff0000,2},
{0,1,1,1,2,1,3,1,0xff0000,4},
{0,0,0,1,1,1,1,2,0x0000ff,2},
{0,1,1,0,1,1,2,0,0x0000ff,3},
{0,1,0,2,1,0,1,1,0x00ff00,2},
{0,0,1,0,1,1,2,1,0x00ff00,3},
{0,0,0,1,1,0,1,1,0xffff00,2},
{0,1,1,0,1,1,1,2,0x00ffff,2},
{0,1,1,0,1,1,2,1,0x00ffff,3},
{1,0,1,1,1,2,2,1,0x00ffff,3},
{0,1,1,1,1,2,2,1,0x00ffff,3},
{0,1,0,2,1,1,2,1,0xff00ff,3},
{0,0,1,0,1,1,1,2,0xff00ff,3},
{0,1,1,1,2,0,2,1,0xff00ff,3},
{1,0,1,1,1,2,2,2,0xff00ff,3},
{0,0,0,1,1,1,2,1,0xffffff,3},
{1,0,1,1,1,2,2,0,0xffffff,3},
{0,1,1,1,2,1,2,2,0xffffff,3},
{0,2,1,0,1,1,1,2,0xffffff,3},
};
// gridmatrix 中只存储颜色信息
int gridmatrix[][]=new int[canvas_size_height][canvas_size_width];
/**
* initialize the applet. resize and load images.
*/
public void init() {
blocktype=math.abs(generator.nextint()%19);
futureblocktype=math.abs(generator.nextint()%19);
lasttype=blocktype;
blocklines=0;
blockscore=0;
blockspeed=1;
curspeed=blockspeed;
blockx=4; lastx=blockx;
blocky=0; lasty=blocky;
//初始化gridmatrix矩阵,内容为带边框的主绘图区。
for(int i=0;i<canvas_size_height;i++)
for(int j=0;j<canvas_size_width;j++)
gridmatrix[i][j]=0;
for(int i=0;i<canvas_size_width;i++)
gridmatrix[canvas_size_height-1][i]=color_dark_gray;
for(int i=0;i<canvas_size_height;i++) {
gridmatrix[i][0]=color_dark_gray;
gridmatrix[i][11]=color_dark_gray;
}
}
/** creates a new instance of tetriscanvas */
public tetriscanvas() {
generator = new random( system.currenttimemillis() );
init();
blocker = new thread(this);
blocker.start();
}
private void draw3dblock(graphics g, int c, int x, int y, int width, int height){
int color = g.getcolor();
g.setcolor( color_white );
g.drawrect( x, y, width, height );
g.setcolor(c);
g.fillrect( x + 1, y + 1, width-2, height-2 );
g.setcolor( color_black );
g.drawline( x + width-1, y, x + width-1, y + height-1 );
g.drawline( x, y + height-1, x + width-1, y + height-1 );
g.setcolor(color);
}
public static boolean drawtext(graphics g, string str, int x, int y, int anchor, int color, int size) {
font f_old,f_new;
int c_old;
try {
f_old = g.getfont();
f_new = font.getfont(font.face_system,font.style_bold,size);
g.setfont(f_new);
c_old = g.getcolor();
g.setcolor(color);
g.drawstring(str, x, y, anchor );
g.setcolor(c_old);
g.setfont(f_old);
return true;
}catch (exception ex) {
return false;
}
}
protected void paint(graphics g){
//画背景
try{
image image_splash = image.createimage(“/back.png”);
g.drawimage(image_splash, 0, 0,graphics.top | graphics.left);
}
catch(exception ex) {
}
//画下一个要出现的方块
drawtext(g, “下一个”, 91, 5, graphics.top| graphics.left, color_blue, font.size_small);
g.setcolor(color_gray);
g.drawroundrect(91, 18, 26, 30, 2, 2);
g.setcolor(color_dark_gray);
g.fillroundrect(92, 19, 24, 28, 2, 2);
for(int i=0;i<=3;i++)
draw3dblock(g, blockinfo[futureblocktype][8],
93+blockinfo[futureblocktype][i*2+1]*block_size,
20+blockinfo[futureblocktype][i*2]*block_size,
block_size,block_size);
drawtext(g, “速度:”+string.valueof(curspeed), 91, 60, graphics.top| graphics.left, color_blue, font.size_small);
drawtext(g, “行数:”+string.valueof(blocklines), 91, 75, graphics.top| graphics.left, color_blue, font.size_small);
drawtext(g, “成绩:”, 91, 90, graphics.top| graphics.left, color_blue, font.size_small);
g.setcolor(color_gray);
g.drawroundrect(91, 105, 26, 20, 2, 2);
g.setcolor(color_dark_gray);
g.fillroundrect(92, 106, 24, 18, 2, 2);
drawtext(g, string.valueof(blockscore), 93, 107, graphics.top| graphics.left, color_white, font.size_medium);
//画当前战况
for(int i=0;i<canvas_size_height-1;i++)
for(int j=1;j<canvas_size_width-1;j++)
if (gridmatrix[i][j]!=0)
draw3dblock(g,gridmatrix[i][j],canvas_offset_x+j*block_size,
canvas_offset_y+i*block_size,
block_size,block_size);
if (!isdown){
//画上新的方块
lastx=blockx; lasty=blocky; lasttype=blocktype;
for(int i=0;i<=3;i++)
draw3dblock(g,blockinfo[blocktype][8],
canvas_offset_x+blockx*block_size+blockinfo[blocktype][i*2+1]*block_size,
canvas_offset_y+blocky*block_size+blockinfo[blocktype][i*2]*block_size,
block_size,block_size);
}
}
private boolean feasible(){
for(int i=0;i<=3;i++)
if (gridmatrix[blocky+blockinfo[blocktype][i*2]][blockx+blockinfo[blocktype][i*2+1]]!=0)
return false;
return true;
}
private void delline(){
for(int i=0;i<=3;i++)
gridmatrix[blocky+blockinfo[blocktype][i*2]][blockx+blockinfo[blocktype][i*2+1]]=blockinfo[blocktype][8];
int temp=4;
boolean canskip=false;
int i=canvas_size_height-2;
while((temp>0)&&(i>=1)){
canskip=false;
label1: for(int j=1;j<=canvas_size_width-2;j++){
if (gridmatrix[i][j]==0) {canskip=true; i–; break label1;}
}
if (!canskip){
temp–;
for(int k=i;k>=1;k–)
for(int l=1;l<=canvas_size_width-2;l++)
gridmatrix[k][l]=gridmatrix[k-1][l];
blocklines++;
blockscore+=200;
if((blockscore%2000)<200) curspeed++;
}
}
}
public void run() {
while (blocker != null) {
if(!isdown){
blocky++;
if (!feasible()) {
isdown=true; blocky–; delline();
try {thread.sleep(400);} catch (interruptedexception e){}
}
else{
repaint();
try {thread.sleep(950-100*(int)blockspeed);} catch (interruptedexception e){}
}
}
else{ blockscore+=50;
if((blockscore%2000)<50) curspeed++;
isdown=false;
repaint();
blockspeed=curspeed;
blocktype=futureblocktype;
futureblocktype=math.abs(generator.nextint()%19);
blockx=4; lastx=blockx;
blocky=0; lasty=blocky;
if (!feasible()) { init();}
}
}
blocker = null;
}
protected void keypressed(int keycode) {
//处理按下键盘的事件,这是canvas的实例方法
switch (getgameaction(keycode)) {//将按键的值转化成方向常量
case canvas.up://向上
break;
case canvas.down://向下
blocky++;
if (!feasible()) blocky–;
repaint();
blockspeed=9;
//blocker.run();
break;
case canvas.left://向左
blockx–;
if (!feasible()) blockx++;
break;
case canvas.right://向右
blockx++;
if (!feasible()) blockx–;
break;
case canvas.fire:
int tempblocktype=blocktype;
if (blocktype==1) blocktype=-1;
else if (blocktype==3) blocktype=1;
else if (blocktype==5) blocktype=3;
else if (blocktype==6) blocktype=5;
else if (blocktype==10) blocktype=6;
else if (blocktype==14) blocktype=10;
else if (blocktype==18) blocktype=14;
blocktype++;
if (!feasible()) blocktype=tempblocktype;
break;
default:
break;
}
repaint(); return;
}
}
