Help: Unable to load image

i’m trying to add a splash screen to a small java game i wrote, and i’m using the following code to get the file


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

Now the file is located in the same directory as the source code, and isn’t set to read-only or anything.

yet i get a IO exception when i try and run the midlet

it claims it cannot load the image.
Am i missing file permissions for the image, or is it even in the right place?

Thanks in advance for your help

send us the EX

Make sure the image is actually in the jar file using the command.

jar -tvf filename.jar

if you are using the SunWtk then the image needs to be placed in the res directory, not the src directory. if you are using netbeans, then I’m pretty sure the image can be in the src directory. I only use netbeans to develop, for my final jars, i use sunwtk.

Wood

ok, i’m using Ktoolbar to build the game, then running it from the command line with midp -classpath . filename

[i don’t have net access at home, so the RUN button just doesn’t work]

I’ve built the Jad and .Jar files and the splash.png is included.

yet no matter where i place the image file [in the rec or src folders, it matters not] i still get the IO Exception

For the sake of it, this is the Splash.java code i’m using


import javax.microedition.lcdui.*;

public class splash extends Canvas implements Runnable

{
   private boolean running = true;
   private Image osb;
   private Graphics osg;
   private Blockade theMidlet;
   private int starFieldViewY;
   private Image title;
   private int fontHeight;
   private Font font;
   private String filename;
   
   
   public splash(Blockade midlet)
   {
      theMidlet = midlet;
      initResources();

      // create the timer thread
      Thread t = new Thread(this);
      t.start();
   }

   private void initResources()
   {
      // setup the screen
      
      try
      {
      osb = Image.createImage(getWidth(), getHeight());
      osg = osb.getGraphics();

    
      
       filename =  "/splash.png";
      
       title = Image.createImage(filename);
      
      font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
      fontHeight = font.getHeight() + 2;

      osg.setFont(font);
          }
          catch(Exception e)
          {
              
          System.out.println("can't load file: " + filename + " why? ==> " + e);
         e.printStackTrace();  
          }
          
   }

   private static final int MAX_CPS = 100;
   private static final int MS_PER_FRAME = 1000 / MAX_CPS;

   public void run()
   {
      try
      {
         while (running)
         {
            // remember the starting time
            long cycleStartTime = System.currentTimeMillis();

            // do our work
            repaint();

            // sleep if we've finished our work early
            long timeSinceStart = (cycleStartTime - System.currentTimeMillis());
            if (timeSinceStart < MS_PER_FRAME)
            {
               try
               {
                  Thread.sleep(MS_PER_FRAME - timeSinceStart);
               }
               catch (java.lang.InterruptedException e)
               {
               }
            }
         }

         // fall back to the splash form at the end of our loop
         theMidlet.activateMenu();
      }
      catch (Exception e)
      {
         System.out.println("App exception2: " + e);
         e.printStackTrace();
      }

   }

   private void renderSplash()
   {
      // clear the background
      osg.setColor(0);
      osg.fillRect(0, 0, getWidth(), getHeight());

     

      osg.drawImage(title, getWidth() / 2, getHeight() / 2 - 10, Graphics.HCENTER | Graphics.VCENTER);

      // draw text
      osg.setColor(0x00888888);
      osg.drawString("© 2005", getWidth() / 2, getHeight() - fontHeight * 3,
                     Graphics.HCENTER | Graphics.TOP);
      osg.drawString(" Crontastic Production", getWidth() / 2, getHeight() - fontHeight * 2,
                     Graphics.HCENTER | Graphics.TOP);
      // draw the copy line
      osg.setColor(0x00ffffff);
      osg.drawString("Press Any Key", getWidth() / 2, getHeight() - fontHeight * 1,
                     Graphics.HCENTER | Graphics.TOP);
   }

   protected void paint(Graphics graphics)
   {
      renderSplash();
      graphics.drawImage(osb, 0, 0, Graphics.LEFT | Graphics.TOP);
   }

   protected void keyPressed(int keyCode)
   {
      running = false;
   }

   

}




The code looks fine to me. My guess would be a case sensitive file name? Maybe your file is named title.PNG or something like that? I’m pretty sure J2ME is case sensitive with file names, I know J2SE is, but I’m very careful to always use lowercase so I have never tested or cared to look it up.

If the case is correct, I think it would be helpful to see the jar -tvf output posted here.

Wood

I finally got it to work, i don’t know why or how this works, but i copied the splash.png file into every directory in the folder.
the classes, bin, lib src folders, all of them, and now it works. I’m sure that i’ve put it into more folder than is nessicary, but i’ll fix that through trial and error later.
but it works.

Or be more correct it doesn’t give me an IOException anymore.

on the downside it now displays an empty screen… so the code is probably b0rked somewhere.

Arse.