Ocorre um “java.io.IOException”;
O arquivo sprite.png está dentro da pasta src, onde ficam os arquivos .java no NetBeans. Então carreguei a imagem assim:
Image imagem = Image.createImage(“sprite.png”); // Pois os arquivos .java estão nesse diretório.
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class ExampleGameCanvas 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
// Sprites to be used.
private Sprite nonTransparentSprite;
// Constructor and initialization
public ExampleGameCanvas() throws Exception {
super(true);
width = getWidth();
height = getHeight();
currentX = width / 2;
currentY = height / 2;
delay = 20;
// Load image to sprite.
Image imagem = Image.createImage("sprite.png");
nonTransparentSprite = new Sprite(imagem, 32, 32);
}
// 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();
nonTransparentSprite.setFrame(0);
// Left
if ((keyStates & LEFT_PRESSED) != 0) {
currentX = Math.max(0, currentX - 1);
nonTransparentSprite.setFrame(1);
}
// Right
if ((keyStates & RIGHT_PRESSED) !=0 )
if ( currentX + 5 < width) {
currentX = Math.min(width, currentX + 1);
nonTransparentSprite.setFrame(3);
}
// Up
if ((keyStates & UP_PRESSED) != 0) {
currentY = Math.max(0, currentY - 1);
nonTransparentSprite.setFrame(2);
}
// Down
if ((keyStates & DOWN_PRESSED) !=0)
if ( currentY + 10 < height) {
currentY = Math.min(height, currentY + 1);
nonTransparentSprite.setFrame(4);
}
}
// Method to Display Graphics
private void drawScreen(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
// Display sprites.
nonTransparentSprite.setPosition(currentX, currentY);
nonTransparentSprite.paint(g);
flushGraphics();
}
}
A classe principal:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ExampleGameCanvasMidlet extends MIDlet {
private Display display;
public void startApp() {
try {
display = Display.getDisplay(this);
ExampleGameCanvas gameCanvas = new ExampleGameCanvas();
gameCanvas.start();
display.setCurrent(gameCanvas);
}
catch(Exception e) {
System.out.println(e);
}
}
public Display getDisplay() {
return display;
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
exit();
}
public void exit() {
System.gc();
destroyApp(false);
notifyDestroyed();
}
}
Alguma dica? Por que esse java.io.IOException ??? Como corrigir?