| Reply | « Previous Thread | Next Thread » |
|
Hi all,
how can we add an image (formatted in PNG) into our MIDlet program? Where do we have to put the image? Is it in the same directory or file than the .java program? Thanks before for the respons. |
|
You can put it anywhere you want as long as it get's packaged in the jar, and you use the correct path to load it (ie, if it's in the "res" directory of the jar you load it with "/res/image.png").
What files or directories find their way into the jar depends on the development environment. shmoove |
|
i'm a beginner in this field. i started to make a Midlet's application since a month ago.
So, can you help me step by step how can i put an image in my MIDlet. have u any example code for that? thx b4... |
|
The code is:
Code:
try {
myImage = Image.createImage(filename);
}
catch (Exception e) {
}
shmoove |
|
i develop my application using J2ME WTK 2.1.
my Midlet program (.java) is in the src directory while the image " coque1.png (80x77 pixels; 1,94 kB) " is separated in res directory. those 2 directories positioned under the same root directory. here is the extract of the program ========================================= public void testList () { choose = new List("Choose items", List.MULTIPLE); choose.setTicker(new Ticker("Testing List")); choose.addCommand (backCommand); choose.setCommandListener(this); try { image = Image.createImage("/res/coque1.png"); } catch (Exception e) { } choose.append("Item 1 ", image); choose.append("Item 2 ", null); choose.append("Item 3 ", null); display.setCurrent(choose); currentMenu = "list"; } ============================================ it compiled and runned well but there was no image appears in the Nokia S40 emulator's screen. u have any idea why did the image don't appear? thanks 4 ur help. -ash- |
|
It's been a while since I messed around with the Wireless Toolkit, but if memory serves me right, it packages the images in the project's "res" folder under the root directory of the jar. You can test this easily by opening up the jar it creates (using WinZip, or WinRAR, or the zip utility of your choice), and checking to see what is the path to the image within the jar.
If my suspicion is correct (and the image is being packaged in the root directory of the jar), then you can load the image with: Code:
image = Image.createImage("/coque1.png");
|
|
code :
============================================= public void testAlert2() { try { //myImage = Image.createImage("/TestGUI/res/coque1.png"); //myImage = Image.createImage("/res/coque1.png"); myImage = Image.createImage("/coque1.png"); } catch (Exception e) { } alert = new Alert ("Alert Image"); alert.setImage(myImage); alert.setTimeout(Alert.FOREVER); alert.addCommand(backCommand); display.setCurrent(alert); currentMenu="alert"; } ============================================== i've tried all of the three possibility above but nothing appears. it's, however, weird. have any further idea? -ash- |
|
in all three cases above, J2ME WTK2.1 shows me the message :
====================== java.lang.NullPointerException ====================== i read in the documentation that it was caused by the null photo ressource. i don't understand this becoz i've rightly put my image (coque1.png) which values 12 KB and 80 x 77 pixels but still no image appear. any further idea? thx -ash- |
|
Have you tried opening the jar file and checking what's the path to the image in the jar (or if it is in there at all) ?
shmoove |
|
thanks shmoove, it worked.
finally, it was placed under the root directory of the jar file. code ============================================== try { myImage = Image.createImage("/coque1.png"); } catch { } ============================================== A new problem comes now that my application executed well using all emulators in J2ME WTK 2.1 except the Nokia s40 dp2.0. i mean the image appeared fine. but while launching the application using Nokia S40 DP2.0 the messages below have appeared. messages of exception ============================================== Exception: java/io/IOException at com.sun.cldc.io.ResourceInputStream.<init>(+14) at java.lang.Class.getResourceAsStream(+83) at javax.microedition.lcdui.Image.createImage(+24) at com.nokia.mid.impl.isa.ui.MIDletRTInfo.setIcon(+9) at com.nokia.mid.impl.isa.ui.MIDletRTInfo.<init>(+113) at com.nokia.mid.impl.isa.ui.MIDletManager.s_loadMIDletSuite(+57) at com.nokia.mid.impl.isa.ui.MIDletManager.main(+80) Exception: java/io/IOException at javax.microedition.lcdui.Image.createImage(+55) at com.nokia.mid.impl.isa.ui.MIDletRTInfo.setIcon(+9) at com.nokia.mid.impl.isa.ui.MIDletRTInfo.<init>(+113) at com.nokia.mid.impl.isa.ui.MIDletManager.s_loadMIDletSuite(+57) at com.nokia.mid.impl.isa.ui.MIDletManager.main(+80) Exception: java/lang/ClassNotFoundException at com.nokia.mid.impl.isa.ui.MIDletRTInfo.<init>(+120) at com.nokia.mid.impl.isa.ui.MIDletManager.s_loadMIDletSuite(+57) at com.nokia.mid.impl.isa.ui.MIDletManager.main(+80) ============================================== have any idea why it happened? they (nokia and provided emulators from J2ME WTK 2.1) dispose the same API right?? thx before. |
|
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*; import java.io.IOException; public class ImageItemDemo extends MIDlet implements CommandListener { private Display display; private Command exit; private ImageItem imageItem; private Form frmMain; public ImageItemDemo() throws IOException { display=Display.getDisplay(this); frmMain=new Form("\t\t\t--Apple--"); Image image = Image.createImage("/a.png"); imageItem = new ImageItem("Apple Drive",image,ImageItem.LAYOUT_CENTER,"Image not found"); frmMain.append(imageItem); exit=new Command("Exit",Command.EXIT,1); frmMain.addCommand(exit); frmMain.setCommandListener(this); } public void startApp() { display.setCurrent(frmMain); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if(c==exit) { destroyApp(true); notifyDestroyed(); } } } This is the full code for Display any type of image in MIDlet.... Drag the image into the "src> package" tab in the window in package explorer in Eclipse IDE... U ll get the display... |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|