469,588 Members | 2,778 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

J2ME: Loading an image using Canvas class

Hi...i am trying to load an image that has been saved at the following path: C:\WTK22\apps\MyProject\res After exectuing the program, the NullPointerException is thrown...Kindly let me know how to load the image without any exceptions being thrown...

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

public class ImmutableImageExample extends MIDlet
{
private Display display;
private MyCanvas canvas;


public ImmutableImageExample()
{
display= Display.getDisplay(this);
canvas=new MyCanvas(this);
}//end of ImmutableImageExample



public void startApp()throws MIDletStateChangeException
{
display.setCurrent(canvas);
}//end of startApp

public void pauseApp(){}

public void destroyApp(boolean abc)throws MIDletStateChangeException {
destroyApp(true);
notifyDestroyed();
}

public void exitMIDlet()
{

}//end of exitMIDlet

}//end of class

class MyCanvas extends Canvas implements CommandListener
{
private Command exit;
private ImmutableImageExample immutableImageExample;
private Image image=null;
private Display display;
public MyCanvas(ImmutableImageExample iie)
{
this.immutableImageExample=immutableImageExample;
exit=new Command("Exit", Command.EXIT,1);
addCommand(exit);
setCommandListener(this);

try
{
image=Image.createImage("/myImage.png");
}//end of try
catch(Exception err)
{
Alert alert=new Alert("Failure","Can't open the image file",null,null);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}//end of catch
}//end of constructor MyCanvas
protected void paint(Graphics g)
{
if(image!=null)
{
g.drawImage(image,0,0,Graphics.VCENTER|Graphics.HC ENTER);
}

}//end of paint

public void commandAction(Command command,Displayable display)
{

if(command==exit)
{
immutableImageExample.exitMIDlet();
}

}//end of commandAction
}//end of class

Thanx, Namrata
Nov 4 '06 #1
1 15510
ikaro
1
Expand|Select|Wrap|Line Numbers
  1. public class ImmutableImageExample extends MIDlet
  2. {
  3. public Display display; //<-- do it public o friendly
  4. ...
  5.  
Expand|Select|Wrap|Line Numbers
  1.     public MyCanvas(ImmutableImageExample iie)
  2.     {
  3.     this.immutableImageExample=iie; //<-change to this
  4.     this.display = iie.display; //<-add this line
  5.  
Suerte!
Dec 8 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by agatha.life | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.