Trabalhar com Thread em J2ME

1 resposta
G

Olá amigos, estou com uma dúvida, estou montando um jogo, por enquanto estou iniciando e quero que a ao clicar numa determinada tecla, uma imagem comece a ir de um lado para o outro, já tentei sem thread mas o simulador trava, alguém poderia me explicar como eu trabalho com thread em j2me, abaixo segue parte do meu codigo.

import javax.microedition.midlet.<em>;

import javax.microedition.lcdui.</em>;

public class TesteCanvas extends Canvas implements CommandListener

{

private Command cmExit;  // Exit midlet

private TesteGabriel midlet;

private Image im = null;

private int translatex = 0, translatey = 0;

private boolean mover = false;
public TesteCanvas(TesteGabriel midlet)

{

this.midlet = midlet;
// Create exit command & listen for events
cmExit = new Command("Exit", Command.EXIT, 1);
addCommand(cmExit);
setCommandListener(this);

try
{
  // Create immutable image
  im = Image.createImage("/bolt.png");
}
catch (java.io.IOException e)
{
  System.err.println("Unable to locate or read .png file");
}

}

protected void paint(Graphics g)

{

if (im != null)

{

// Clear the background

g.setColor(255, 255, 255);

g.fillRect(0, 0, getWidth(), getHeight());
// Translate coordinates
  g.translate(translatex, translatey);      

  // Always draw at 0,0 - desenha imagem na
  g.drawImage(im, 0, 0, Graphics.LEFT | Graphics.TOP);
}

}

public void commandAction(Command c, Displayable d)

{

if (c == cmExit)

midlet.exitMIDlet();

}
protected void keyPressed(int keyCode)

{

switch (getGameAction(keyCode))

{

case RIGHT:
mover = true;
  	moverFigura();
    // If scrolling off the right, bring around to left                  
    break;
}

}

public void moverFigura(){

if(mover){

while(true){

if ((translatex + im.getWidth() + translatex) > getWidth())

translatex = 0;

else

translatex += im.getWidth();
repaint();
	}
}

}
}

Obrigado,
Gabriel

1 Resposta

C

A apostila de J2ME aqui do PJ tem um capitulo sobre Canvas que traz eexempo bem parecido com o que vc precisa… usand oThread.

Falow!

Criado 26 de maio de 2006
Ultima resposta 27 de mai. de 2006
Respostas 1
Participantes 2