Carregar uma imagem

1 resposta
D

Estou tendo problemas ao tentar carregar uma imagem, coloquei a mesma no diretorio raiz do meu projeto, o codigo abaixo eh o que esta dando problema, se alguem puder me ajudar eu agradeço !!!

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

public class Principal extends MIDlet {

public Principal() {

}

protected void startApp()
throws MIDletStateChangeException {

Display display = Display.getDisplay( this );

display.setCurrent( new GameScreen() );

}
protected void destroyApp( boolean unconditional )

throws MIDletStateChangeException {

}
protected void pauseApp(){

}

}

/*
Maurílio ‘TK2000’ Silva
[email removido]
Arcoverde-PE, 12/12/2004
Simples teste em J2ME
*/

class GameScreen extends Canvas implements Runnable {
// cria o objeto nave

Sprite nave = new Sprite( “iReport.png”, 0, 0);

// para o double buffering

Graphics buffer;

Image bufferImage;

// direcao da imagem
int imageDirection;

// cor do 'fundo’
int cor = 0xFFFFFF;

// construtor padra para nossa GameScreen class.

public GameScreen() {

// cria as imagens necessarias

System.out.println(Passo 1);

createImages();

System.out.println(Passo 2);

// posiciona a imagem

nave.x = getWidth()/2; // posicao x

nave.y = getHeight()/2;// posicao y

nave.w = nave.img.getWidth(); // largura

nave.h = nave.img.getHeight();// altura

System.out.println(Passo 3);

// cria um novo Thread e o inicia

new Thread( this ).start();

}
public void createImages() {

try {

// se o dispositivo não tem suporte automatico

// para double buffering

if( !isDoubleBuffered() ) {

// cria imagem offscreen

bufferImage = Image.createImage( getWidth(),

getHeight() );

buffer = bufferImage.getGraphics();

}

}

catch( Exception e ) {

e.printStackTrace();

}

}
public void run() {

while( true ) {

int loopDelay = 1000 / 15;

long loopStartTime = System.currentTimeMillis();

tick();

long loopEndTime = System.currentTimeMillis();

int loopTime = (int)(loopEndTime - loopStartTime);

if( loopTime < loopDelay )

{

try {

Thread.sleep( loopDelay - loopTime );

}

catch( Exception e ) {

}

}
}
}

// loop principal

public void tick() {

int myImageSpeed = 4;

int sW = this.getWidth();

int sH = this.getHeight();

int p = 10;

switch( imageDirection ) {

case LEFT:

if( nave.x > p)

nave.x -= myImageSpeed;

break;

case RIGHT:

if( nave.x < sW - p)

nave.x += myImageSpeed;

break;

case UP:

if( nave.y > p)

nave.y -= myImageSpeed;

break;

case DOWN:

if( nave.y < sH - p)

nave.y += myImageSpeed;

break;

}

repaint();

serviceRepaints();

}
protected void paint( Graphics g ) {

Graphics original = g;

if( !isDoubleBuffered() ) {

g = buffer;

}

g.setColor( cor );

g.fillRect( 0, 0, this.getWidth(), this.getHeight() );

g.drawImage( nave.img, nave.x, nave.y,

Graphics.VCENTER | Graphics.HCENTER);
if( !isDoubleBuffered() ) {

original.drawImage( bufferImage, 0, 0,

Graphics.TOP | Graphics.LEFT );

}

}
protected void keyPressed( int keyCode ) {

int gameAction = getGameAction( keyCode );

switch( gameAction ) {

case LEFT:

imageDirection = LEFT;

break;

case RIGHT:

imageDirection = RIGHT;

break;

case UP:

imageDirection = UP;

break;

case DOWN:

imageDirection = DOWN;

break;
case FIRE:

cor = (cor==0xFFFFFF)?0x2857B6:0xFFFFFF;

break;

}

}
protected void keyReleased( int keyCode ) {

imageDirection = 0;

}

}

class Sprite {

public Image img; // imagem
public int x, y, z; // coordenadas X,Y,Z do objeto
public int w, h; // largura e altura
public boolean vis; // visivel

/**

  • Inicializa um objeto Sprite fornecendo alguns dos
  • paramentros necessarios<br>
  • Onde:<br>
  • img -> imagem a ser carregada<br>
  • x -> coordenada X<br>
  • y -> coordenada Y<br>
    **/
    public Sprite( String nome, int x, int y) {
    try {

this.img = Image.createImage( nome);

}

catch( Exception e ) {

System.out.println(“Erro ao usar create Image”);

e.printStackTrace();

this.img = null;

}

this.x = x;

this.y = y;

this.z = 0;

this.w = 0;

this.h = 0;

this.vis = false;

}

}

1 Resposta

L

tenta coloca a imagem na pasta res du teu projeto, i coloca uma / antes do nome do arquivo

Criado 22 de dezembro de 2004
Ultima resposta 22 de dez. de 2004
Respostas 1
Participantes 2