一个圣诞动画的实现

图片加载类:

import javax.microedition.lcdui.*;

public class ImageAccess {
Image bg = null;
Image merry = null;
Image logo = null;
Image control = null;
Image context = null;

public ImageAccess() {
try{
bg = Image.createImage("/bg.png");
merry = Image.createImage("/merry.png");
logo = Image.createImage("/logo.png");
control = Image.createImage("/control.png");
context = Image.createImage("/context.png");
}catch(java.io.IOException e){
e.printStackTrace();
}
}
}
地图数据类:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MerryData {
private int lay = 3;
private int position = 1;
// y,ix,iy,w,h
// 四个下标为图片的位移和宽度(ix,iy,w,h)
public int[][] layout = new int[][]{{0,0,24,38},{24,0,46,38},{70,0,16,18}};
public int[][] contet = new int[][]{{0,0,59,25},{59,0,59,25},{0,25,59,15},{60,25,59,25}};
// 三个下标为当前的图片和它的坐标
public int[][] bulet = new int[][]{{0,8,83},{2,81,100},{0,150,80},{1,215,86},{0,295,84},{1,210,85},{1,350,86},{0,410,87},{2,500,90}};
public int[][] jokul = new int[][]{{0,85,10,10,0},{58,88,55,50,1},{114,85,30,30,0},{170,85,5,30,0},{223,87,65,30,0},{280,87,65,30,0},{335,87,65,30,0},{390,87,65,30,0},{450,87,65,30,0},{500,87,65,30,0}};

public MerryData() {
}

public void move() {
for(int i = 0; i < bulet.length; i ++) {
bulet[i][1]--;
}
for(int i = 0; i < jokul.length; i ++) {
jokul[i][0]--;
}
}
}

第一个Canvas类:

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class GscreenBuffer extends Canvas implements Runnable {
private int screenw = 128,screenh = 128;
private boolean logoover=false;
private Image off_screen_buffer;
private Graphics off_screen;
private boolean visible = false;
public ImageAccess ia;
public MerryData data;
Thread thread = new Thread(this);

public GscreenBuffer() {
off_screen_buffer = Image.createImage(screenw,screenh);
off_screen = off_screen_buffer.getGraphics();
init();
}

protected void init() {
ia = new ImageAccess();
data = new MerryData();
}

public void run() {
while(true) {
data.move();
try {
repaint();
thread.sleep(300);
}catch(InterruptedException e) {}
}
}

public void paint(Graphics g) {
drawMapLayout();
// 障碍物
for(int i = 0; i < data.bulet.length; i ++) {
drawLayout(i);
}

off_screen.setColor(0,0,0);
off_screen.drawRect(0,0,126,126);
g.drawImage(off_screen_buffer, 0, 0, 20);

}

private void drawImg(Graphics g ,Image allimg,int ix,int iy,int bx,int by ,int width,int height,int gra){
g.setClip(ix,iy,width,height);
g.drawImage(allimg,ix-bx,iy-by,g.LEFT|gra);
g.setClip(0,0,128,128);
}

protected void drawMapLayout() {
off_screen.drawImage(ia.bg,0,0,20);
// 雪山
for(int i = 0; i < data.jokul.length; i ++) {
drawJokul(i);
}
}

private void drawLayout(int x) {
drawImg(off_screen,ia.control,data.bulet[x][1],data.bulet[x][2],data.layout[data.bulet[x][0]][0],data.layout[data.bulet[x][0]][1],data.layout[data.bulet[x][0]][2],data.layout[data.bulet[x][0]][3],20);
}

protected void drawJokul(int x) {
drawImg(off_screen,ia.context,data.jokul[x][0],data.jokul[x][1],data.contet[x%2][0],data.contet[x%2][1],data.contet[x%2][2],data.contet[x%2][3],20);
drawImg(off_screen,ia.context,data.jokul[x][0],3,data.contet[3][0],data.contet[3][1],data.contet[3][2],data.contet[3][3],20);
drawImg(off_screen,ia.context,data.jokul[x][0],40,data.contet[3][0],data.contet[3][1],data.contet[3][2],data.contet[3][3],20);
drawImg(off_screen,ia.context,data.jokul[x][0],data.jokul[x][1]+15,data.contet[2][0],data.contet[2][1],data.contet[2][2],data.contet[2][3],20);
}
}

第二个Canvas类:

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;

public class GameStart extends Canvas implements Runnable {
GscreenBuffer buffer= new GscreenBuffer(); //这里把第一个Canvas类加进来,从而在同一个线程里运行
int executenumber=0;
MerryXmas merry= null;
Thread thread = new Thread(this);
long starttime = System.currentTimeMillis();
boolean logoover=false;

public GameStart(MerryXmas merry) {
this.merry=merry;
}

public void run(){
while(System.currentTimeMillis()-starttime<2000){
this.repaint();
try{
thread.sleep(30L);
}catch(Exception e){
e.printStackTrace();
}
}
logoover=true;
this.repaint();
}

public void paint(Graphics g){
if(this.logoover==true){
g.drawImage(buffer.ia.merry,0,0,20);
//gr.aa.logo=null;
System.gc();
}else {
g.setColor(255,255,255);
g.fillRect(0,0,128,128);
g.drawImage(buffer.ia.logo,20,40,20);

}
}
public void keyPressed(int keyCode){
buffer.thread.start();
Display.getDisplay(merry).setCurrent(buffer);
}
}

主类MIDlet:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MerryXmas extends MIDlet {
private static MerryXmas Xmas;
GameStart gs= new GameStart(this);

public MerryXmas() {
Xmas = this;
}

public static MerryXmas getmidlet(){
if(Xmas != null) {
return Xmas;
}
return null;
}

public void startApp() {
gs.thread.start();
Display.getDisplay(this).setCurrent(gs);
}

public void pauseApp() {}
public void destroyApp(boolean unconditional) {}

public static void quitApp() {
Xmas.destroyApp(true);
Xmas.notifyDestroyed();
Xmas = null;
}
}

你可能感兴趣的:(thread)