hi, i'm writing a program in j2me which uses the canvas. I'm trying to insert an image into the canvas, but my WTK 2.5.2 returns an error about loading the image.
this is the code:
the image is in the correct folder, end this is the error:PHP Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class ProvaGrafica1 extends MIDlet implements CommandListener
{
Command exitCommand;
Form form;
public ProvaGrafica1() { }
private Image image;
private final static String IMAGE_PATH = "/baby1.png";
public void setFullScreenMode(boolean mode) { }
public void startApp()
{
exitCommand = new Command("exit",Command.EXIT,0);
image = null;
try
{ image = Image.createImage(IMAGE_PATH);
System.out.println("immagine caricata");}
catch(IOException ioe)
{
System.out.println("errore nel caricare l'immagine");
Alert errorAlert = new Alert("Errore","Image not present",null,AlertType.ERROR);
errorAlert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(errorAlert);
}
Canvas1 movableCanvas = new Canvas1();
movableCanvas.setFullScreenMode(true);
Display.getDisplay(this).setCurrent(movableCanvas);
}
public Form getForm()
{
if (form == null)
{
form = new Form("Welcome", new Item[] {});
form.setCommandListener(this);
}
return form;
}
public void commandAction(Command c, Displayable d)
{
if (c==exitCommand && d==getForm()){Display.getDisplay(this).setCurrent(getForm());}
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
class Canvas1 extends Canvas{
private final static int RAGGIO = 20;
private final static int STEP = 5;
private int x,y;
public void paint(Graphics g){
// Determiniamo le dimensioni del Canvas
int canvasWidth = getWidth();
int canvasHeight = getHeight();
g.setColor(0x0000ff);
g.fillRect(0,0,canvasWidth,canvasHeight);
g.setColor(0xff0000);
g.fillArc(x-RAGGIO,y-RAGGIO,RAGGIO*2,RAGGIO*2,0,360);
if (image== null) System.out.println("immagine inesistente");
g.drawImage(image, (360-(360/20)-40), (20+40), Graphics.BASELINE | Graphics.LEFT);
}// fine
protected void pointerPressed(int x, int y){
move(x, y);
}// fine
public void move(int moveX, int moveY)
{
x=moveX;
y=moveY;
if ((x >= (360-(360/20)-40)) && (x <= (360-(360/20))) && (y >= 20) && (y <= 60)) commandAction(exitCommand, getForm());
repaint();
serviceRepaints();
}
}
public void exitMIDlet()
{
destroyApp(true);
notifyDestroyed();
}
}
java.lang.IllegalArgumentException:
at ProvaGrafica1$Canvas1.paint(+92)
at javax.microedition.lcdui.Canvas.callPaint(+85)
at javax.microedition.lcdui.Display.repaint(+82)
at javax.microedition.lcdui.Display.registerNewCurrent(+235)
at javax.microedition.lcdui.Display.access$700(+6)
at javax.microedition.lcdui.Display$DisplayAccessor.foregroundNotify(+46)
at javax.microedition.lcdui.Display$DisplayManagerImpl.notifyWantsForeground(+152)
at javax.microedition.lcdui.Display$DisplayManagerImpl.access$100(+6)
at javax.microedition.lcdui.Display.setCurrent(+70)
at ProvaGrafica1.startApp(+102)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)
i made some tests and i noticed that the image image is loaded and the error is in the function g.drawImage(); why? (the image is smaller than the canvas!!)
how can I do?? help me please!!
(sorry for my english, but i'm italian...!)

Reply With Quote


