Question

I'm trying to make a image as follows:-

    try 
    {
        strom = Image.createImage("stromB.png");
        map0 =  Image.createImage("map0.png");
    } 
    catch (Exception e) 
    {
        System.out.println("image creatino faild");
        System.out.println(e.getMessage());
    }

strom works fine but map0 always throw an exception. My guess is that the size is bigger and may be that's why.

  • How can I use a larger png image in the midlet?
  • Are there any limitation for the midlet?
  • What is the maximum size I can use for a midlet?
Was it helpful?

Solution

map0 throw exception may be because of large size, use following method to larger images to resize it according to screen size

 /**
  * This methog resizes an image by resampling its pixels
  * @param src The image to be resized
  * @return The resized image
  */

  private Image resizeImage(Image src) {
      int srcWidth = src.getWidth();
      int srcHeight = src.getHeight();
      Image tmp = Image.createImage(screenWidth, srcHeight);
      Graphics g = tmp.getGraphics();
      int ratio = (srcWidth << 16) / screenWidth;
      int pos = ratio/2;

      //Horizontal Resize        

      for (int x = 0; x < screenWidth; x++) {
          g.setClip(x, 0, 1, srcHeight);
          g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
          pos += ratio;
      }

      Image resizedImage = Image.createImage(screenWidth, screenHeight);
      g = resizedImage.getGraphics();
      ratio = (srcHeight << 16) / screenHeight;
      pos = ratio/2;        

      //Vertical resize

      for (int y = 0; y < screenHeight; y++) {
          g.setClip(0, y, screenWidth, 1);
          g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
          pos += ratio;
      }
      return resizedImage;

  }//resize image    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top