Ola pessoal,
Estou começando com canvas, porém estou tendo dificuldades para carregar uma imagem.
Podem me ajudar?
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class TesteCanvas extends Canvas{
Image fundo;
int posX = 50;
int posY = 50;
public TesteCanvas(){
try{
fundo = Image.createImage("/fundo.png");
}catch(IOException e){
System.out.println("Imagem não pode ser carregada");
}
}
public void paint(Graphics g){
g.setColor(0,0,0);
g.drawRect(50,5,60,20);// posX, posY, largura, altura
g.setColor(255,0,0);
String teste="Testando";
g.drawString(teste,60,7,Graphics.LEFT|Graphics.TOP);
}
public void keyPressed(int key){
if(key == KEY_NUM2){
System.out.println("Tecla 2");
posY--;
}
if(key == KEY_NUM4){
System.out.println("Tecla 4");
posX--;
}
if(key == KEY_NUM5){
System.out.println("Tecla 5");
}
if(key == KEY_NUM6){
System.out.println("Tecla 6");
posX++;
}
if(key == KEY_NUM8){
System.out.println("Tecla 8");
posY++;
}
}
}
Principal
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class MidletCanvas extends MIDlet implements CommandListener{
Display tela;
Command sair;
TesteCanvas canvas;
public MidletCanvas(){
tela = Display.getDisplay(this);
sair = new Command("Sair",Command.EXIT,0);
canvas = new TesteCanvas();
canvas.addCommand(sair);
canvas.setCommandListener(this);
}
public void startApp(){
tela.setCurrent(canvas);
}
public void pauseApp(){
}
public void destroyApp(boolean b){
}
public void commandAction(Command c, Displayable d) {
if(c==sair){
destroyApp(true);
notifyDestroyed();
}
}
}