×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    May 2004
    Location
    La Rochelle
    Posts
    22

    put an image in MIDlet

    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.

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    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

  3. #3
    Registered User
    Join Date
    May 2004
    Location
    La Rochelle
    Posts
    22

    beginner.....

    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...

  4. #4
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    The code is:
    Code:
    try {
      myImage = Image.createImage(filename);
    }
    catch (Exception e) {
    }
    Where exactly you put this image depends on how your environment is set up. How are you building your MIDlets?

    shmoove

  5. #5
    Registered User
    Join Date
    May 2004
    Location
    La Rochelle
    Posts
    22
    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-

  6. #6
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    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");
    shmoove

  7. #7
    Registered User
    Join Date
    May 2004
    Location
    La Rochelle
    Posts
    22
    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-

  8. #8
    Registered User
    Join Date
    May 2004
    Location
    La Rochelle
    Posts
    22
    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-

  9. #9
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    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

  10. #10
    Registered User
    Join Date
    May 2004
    Location
    La Rochelle
    Posts
    22
    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.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    Smile Re: put an image in MIDlet

    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •