作者使用JBuilder
然後填寫名字和路徑
然後選擇JDK路徑
點擊jdk後面的路徑按鈕
next
然後在對話框左邊選擇Micro
點擊MIDlet or Displayable
繼續下一步
然後Jbuilder自動生成所需代碼
TestMIDlet
package temps;
import javax
import javax
/**
*
Title:
*
*
Description:
*
*
Copyright: Copyright (c)
*
*
Company:
*
* @author not attributable
* @version
*/
public class TestMIDlet extends MIDlet
{
private Display display;
public void startApp()
{
try
{
display = Display
Displayable
gameCanvas
display
}
catch (Exception ex)
{
System
}
}
public Display getDisplay()
{
return display;
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
exit();
}
public void exit()
{
System
destroyApp(false);
notifyDestroyed();
}
}
Displayable
package temps;
import javax
import java
import javax
/**
*
Title:
*
*
Description:
*
*
Copyright: Copyright (c)
*
*
Company:
*
* @author not attributable
* @version
*/
public class Displayable
{
private boolean isPlay; // Game Loop runs when isPlay is true
private long delay; // To give thread consistency
private int currentX
private int width; // To hold screen width
private int height; // To hold screen height
private Random random=new Random(System
private final int[] sequence={
private int counter=
private int enemyX
private int enemyDirect=
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 Displayable
{
super(true);
width = getWidth();
height = getHeight();
currentX = width /
currentY = height /
enemyX=width/
enemyY=height/
delay =
Image playerImage=null;
Image backgroundImage=null;
Image enemy=null;
// Load Images to Sprites
try
{
playerImage = Image
backgroundImage = Image
enemy=Image
}
catch(Exception e)
{
System
}
playerSprite = new Sprite(playerImage
System
enemySprite=new Sprite(enemy
backgroundSprite = new Sprite(backgroundImage);
layerManager = new LayerManager();
layerManager
layerManager
layerManager
}
// Automatically start thread for game loop
public void start()
{
isPlay = true;
Thread t = new Thread(this);
t
}
public void stop()
{
isPlay = false;
}
// Main Game Loop
public void run()
{
Graphics g = getGraphics();
while (isPlay == true)
{
input();
drawScreen(g);
try
{
Thread
}
catch (InterruptedException ie)
{
}
}
}
// Method to Handle User Inputs
private void input()
{
int keyStates = getKeyStates();
playerSprite
if(isalive)
{
if(counter==
{
enemyDirect=Math
}
counter++;
switch(enemyDirect)
{
case
enemyY=Math
break;
case
enemyY=Math
break;
case
enemyX=Math
break;
case
enemyX=Math
break;
}
counter=(counter++)%(sequence
enemySprite
}
else
{
enemyY+=counter;
enemyY=Math
if(enemyY counter++;
enemySprite
}
// Left
if ((keyStates & LEFT_PRESSED) !=
{
currentX = Math
playerSprite
}
// Right
if ((keyStates & RIGHT_PRESSED) !=
if (currentX +
{
currentX = Math
playerSprite
}
// Up
if ((keyStates & UP_PRESSED) !=
{
currentY = Math
playerSprite
}
// Down
if ((keyStates & DOWN_PRESSED) !=
{
isdown=true;
}
if ((keyStates & DOWN_PRESSED) !=
{
if(isdown)
{
if (currentY +
{
currentY = Math.min(height, currentY + 1);
playerSprite.setFrame(4);
}
}
}
if((keyStates & FIRE_PRESSED)!=0)
{
isalive=!isalive;
counter=0;
}
if(!(enemyX+enemySprite.getWidth()
|| enemyY>currentY+playerSprite.getHeight()) && isalive)
{
counter=0;
isalive=false;
}
}
// Method to Display Graphics
private void
From:http://tw.wingwit.com/Article/program/Oracle/201311/17168.html