×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    Load an Image and Display

    Hey everybody

    Im creating a MIDlet that should display an image.
    But I get a l java.io.IOException.....
    I've placed the picture test_j2me.png in the folder where all my files are.

    My code looks like this :

    public class Board extends Canvas implements CommandListener
    {
    private Game the_game;

    public Graphics g;
    public Image image;

    private Command BACK = new Command("Back",Command.BACK,0);
    //private Command EXIT = new Command("Exit",Command.EXIT,0);
    private Command CLEAR = new Command("Clear",Command.OK,1);
    private Display display;
    private Displayable currentDisplayable;

    private static final int WHITE = 0x00FFFFFF;
    private static final int RED = 0x00FF0000;

    public Board(Game the_game)
    {
    // There must be a call to super because this is the Canvas.class no arg constructor
    super();

    addCommand(BACK);
    //addCommand(EXIT);
    addCommand(CLEAR);
    setCommandListener(this);
    // This lines parses the instance of Game to the var the_game
    // The instance of Game, instance_Game, is parsed to Board() - no arg constr - in Game.startApp()
    this.the_game= the_game;
    try
    {
    image = Image.createImage("C:/Documents and Settings/Christian V Petersen/JavaApplication5/src/BackG/test_j2me.png");
    //
    }
    catch (IOException ioe)
    {
    System.out.println(ioe.getMessage());
    ioe.printStackTrace();
    }

    }


    public void commandAction(Command c, Displayable d)
    {
    if(c==BACK)
    {
    the_game.display.setCurrent(the_game.playMenu);
    }

    else if(c==CLEAR)
    {

    }
    }


    // this func is used to clear the Canvas
    public void clear (Graphics g)
    {
    int clipX = g.getClipX();
    int clipY = g.getClipY();
    int clipH = g.getClipHeight();
    int clipW = g.getClipWidth();

    int color= g.getColor();
    g.setColor(WHITE);
    g.fillRect(clipX,clipY,clipW,clipH);

    g.setColor(color);
    }

    public void paint(Graphics g)
    {
    //graphics
    clear(g);

    g.drawImage(image,0,0,Graphics.TOP|Graphics.LEFT);


    }

    }

    Any help is fine

    Thanks
    CVP

  2. #2
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249

    Re: Load an Image and Display

    image = Image.createImage("C:/Documents and Settings/Christian V Petersen/JavaApplication5/src/BackG/test_j2me.png");
    Hi.

    Ok. Still pretty new at J2ME, so I will not make my answer a "matter of fact" one. I'm not certain that it is necessary (or valid for that matter) to describe the entire path as you have above.

    What I've done (and it works for me) is to place the image in my main source folder. And declare the path as such ("tesimg.png" being the filename):

    Image image = Image.createImage("/testimg.png");

    When I say the main source folder, in your case I believe it would be src (not within package BackG). Though if that fails, you can try recopying your image back in there I suppose.

    Hope that helps.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    Thanks

    Great it works

    I own a book on J2ME and it stated the same as you - except where to place the file

    CPV

  4. #4
    Registered User
    Join Date
    Nov 2005
    Location
    Oregon
    Posts
    40

    Post Re: Load an Image and Display

    I believe that the image file needs to be on the classpath. This will allow you to add the image file as a Resource via the JAD/JAR package building tool (which will place the image file in the jar file). The image will probably get added at the root directory, so you can reference it as:

    splashImage = Image.createImage("/msmSplash.PNG" );

    To keep my project clean, I created an image directory and put my images in that folder. Then, via Eclipse project settings, I added the image folder as a source folder. This just puts the folder on the classpath. I suspect that if I had a subdirectory in my images folder, then the image would go in a different place in the jar... but that is just an untested supposition at this point.

  5. #5
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249

    Re: Load an Image and Display

    If you put the image in a package in your src code(eg.P1), what you would need to do (an alternative to putting it as part of your classpath) is this:
    Code:
    Image image = Image.createImage("/P1/testimg.png");

    If you had a subdirectory within P1 (eg. sub):
    Code:
    Image image = Image.createImage("/P1/sub/testimg.png");

Posting Permissions

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