这是一篇关于怎样使用JBuilder开发j2me程序的例子。本人使用JBuilder2005,2005自带WTK2.1,自己下载2.2版
本,2.2那个默认的模拟器很好用。另外,Jbuilder X下的WTK也是2.0的版本好象。Jbuilder 9的WTK是1.04。
首先开启Jbuilder.建立一个Project。
然后填写名字和路径。继续:
然后选择JDK路径,本身已有WTK2.1,你可以选择。但是你也可以自己选择其他的WTK版本。
点击jdk后面的路径按钮,继续:
然后ok,next.工程建立完毕.
接着点击file->New:
然后在对话框左边选择Micro,你就能看到右边出现MIDlet,Displayable.
接上面的:如图示
点击MIDlet or Displayable,则让你输入MIDlet的名字.如图:
继续下一步,让你输入一个Displayable的名字,包括类型(form,canvas等),是否自动加载Command
等等
然后Jbuilder自动生成所需代码,以及Jbuilder自加的一些东西.你可以添加或修改之.
这里我将本站一篇关于介绍Gamecanvas的文章的代码拿过来,做少量修改,作为本文章的一个演示;
TestMIDlet.java文件:
package temps;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* Title:
*
* Description:
*
* Copyright: Copyright (c) 2005
*
* Company:
*
* @author not attributable
* @version 1.0
*/
public class TestMIDlet extends MIDlet
{
private Display display;
public void startApp()
{
try
{
display = Display.getDisplay(this);
Displayable1 gameCanvas = new Displayable1();
gameCanvas.start();
display.setCurrent(gameCanvas);
}
catch (Exception ex)
{
System.out.println(ex);
}
}
public Display getDisplay()
{
return display;
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
exit();
}
public void exit()
{
System.gc();
destroyApp(false);
notifyDestroyed();
}
}
Displayable1.java内容:
package temps;
import javax.microedition.lcdui.*;
import java.util.Random;
import javax.microedition.lcdui.game.*;
/**
* Title:
*
* Description:
*
* Copyright: Copyright (c) 2005
*
* Company:
*
* @author not attributable
* @version 1.0
*/
public class Displayable1 extends GameCanvas implements Runnable
{
private boolean isPlay; // Game Loop runs when isPlay is true
private long delay; // To give thread consistency
private int currentX, currentY; // To hold current position of the 'X'
private int width; // To hold screen width
private int height; // To hold screen height
private Random random=new Random(System.currentTimeMillis());
private final int[] sequence={0,1,2,1,0,1,2,1,0,1,2,1,1,1,1,1,1};
private int counter=0;
private int enemyX,enemyY;
private int enemyDirect=0;
private boolean isdown=false;
private boolean isalive=true;
// Sprites to be used
private Sprite playerSprite;
private Sprite backgroundSprite;
private Sprite enemySprite;
// Layer Manager
private LayerManager layerManager;
// Constructor and initialization
public Displayable1() throws Exception
{
super(true);
width = getWidth();
height = getHeight();
currentX = width / 2;
currentY = height / 2;
enemyX=width/4;
enemyY=height/4;
delay = 100;
Image playerImage=null;
Image backgroundImage=null;
Image enemy=null;
// Load Images to Sprites
try
{
playerImage = Image.createImage("/transparent.png");
backgroundImage = Image.createImage("/background.png");
enemy=Image.createImage("/frames.png");
}
catch(Exception e)
{
System.out.println("get Image error!"+e.getMessage());
}
playerSprite = new Sprite(playerImage, 32, 32);
System.out.println(playerSprite.getRawFrameCount());
enemySprite=new Sprite(enemy,50,21);
backgroundSprite = new Sprite(backgroundImage);
layerManager = new LayerManager();
layerManager.append(playerSprite);
layerManager.append(enemySprite);
layerManager.append(backgroundSprite);
}
// Automatically start thread for game loop
public void start()
{
isPlay = true;
Thread t = new Thread(this);
t.start();
}
public void stop()
{
isPlay = false;
}
// Main Game Loop
public void run()
{
Graphics g = getGraphics();
while (isPlay == true)
{
input();
drawScreen(g);
try
{
Thread.sleep(delay);
}
catch (InterruptedException ie)
{
}
}
}
// Method to Handle User Inputs
private void input()
{
int keyStates = getKeyStates();
playerSprite.setFrame(0);
if(isalive)
{
if(counter==0)
{
enemyDirect=Math.abs(random.nextInt()%4);
}
counter++;
switch(enemyDirect)
{
case 0:
enemyY=Math.max(0,--enemyY);
break;
case 1:
enemyY=Math.min(++enemyY,height-enemySprite.getHeight());
break;
case 2:
enemyX=Math.max(0,--enemyX);
break;
case 3:
enemyX=Math.min(++enemyX,width-enemySprite.getWidth());
break;
}
counter=(counter++)%(sequence.length());
enemySprite.setFrame(sequence[counter]);
}
else
{
enemyY+=counter;
enemyY=Math.min(enemyY,height-enemySprite.getHeight());
if(enemyY
counter++;
enemySprite.setFrame(3);
}
// Left
if ((keyStates & LEFT_PRESSED) != 0)
{
currentX = Math.max(0, currentX - 1);
playerSprite.setFrame(1);
}
// Right
if ((keyStates & RIGHT_PRESSED) != 0)
if (currentX + 5 < width)
{
currentX = Math.min(width, currentX + 1);
playerSprite.setFrame(3);
}
// Up
if ((keyStates & UP_PRESSED) != 0)
{
currentY = Math.max(0, currentY - 1);
playerSprite.setFrame(2);
}
// Down
if ((keyStates & DOWN_PRESSED) != 0)
{
isdown=true;
}
if ((keyStates & DOWN_PRESSED) != 0)
{
if(isdown)
{
if (currentY + 10 < height)
{
currentY = Math.min(height, currentY + 1);
playerSprite.setFrame(4);
}
}
}
if((keyStates & FIRE_PRESSED)!=0)
{
isalive=!isalive;
counter=0;
}
if(!(enemyX+enemySprite.getWidth()currentX+playerSprite.getWidth() || enemyY>currentY+playerSprite.getHeight()) && isalive)
{
counter=0;
isalive=false;
}
}
// Method to Display Graphics
private void drawScreen(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
// updating player sprite position
playerSprite.setPosition(currentX, currentY);
enemySprite.setPosition(enemyX,enemyY);
layerManager.setViewWindow(0, 0, width, height);
layerManager.paint(g, 0,0);
g.setColor(0xffffff);
flushGraphics();
}
}
此程序里敌人随机向4个方向移动,而玩家控制的角色可任意移动, 如果两者发生碰撞,则敌人死亡.
另外按fire键可直接让敌人生或死
关于运行,Jbuilder里有个运行配置:
一般不用修改,如果多个Midlet,你可以在这里添加.
关于用Jbuilder打包.
一样file->new:
这个在2005和9,x里有所不同,选择Archive,右边选择MIDlet,然后ok.
这里选择要打包文件的目录,当前默认是所有的class文件和资源文件,你也可以删除掉,自己选择一部分.
然后next,直到最后. 然后运行,在运行配置中默认重新rebuild,打包等等.然后你能看到左上框里有jar文件和jad文件.
运行结果:
呵呵,用你控制的主角去碰撞这只大飞虫试试:
posted on 2006-05-09 18:31
wqwqwqwqwq 阅读(525)
评论(0) 编辑 收藏 所属分类:
Simple Java