in J2ME , midp1, Image drawing


Dears, can Iload an image ( from a jpg file ) in the background and write a text over it or draw over it ?,
I tried but it didn't work,

I appreciate if u show me how ?

Also, I put a jpg file in the JAD, to be as an icon in the mobile for my application, but still not shown ?
any ideas ?

thanks

LVL 1
khamoudaAsked:
Who is Participating?
 
jimmackConnect With a Mentor Commented:
MIDP 1.0 only has support for PNG as a mandatory graphics file type.  Some, but not all, devices do support other types (eg. JPG, GIF).

A mutable image is one that you can change.  When you load an image from a resource using

    Image loadedImage = Image.createImage("images/myImg.png");

 or similar, this creates an immutable image, ie. you can't change it.  If you want to add further graphics/text to this image, you need to create a mutable copy using something like:

    Image editableImage = Image.createImage(loadedImage.getWidth(), loadedImage.getHeight());   // Creates an empty, but mutable image of the same size
    Graphics graphics = editableImage.getGraphics();
    graphics.drawImage(editableImage, 0, 0, Graphics.TOP | Graphics.LEFT);

At this point, you have a mutable copy of the original, so from this point you can drawStrings or whatever you need.

For super-safety, you should catch the IOException that may occur when you load the image from the resource.  It is possible that the decoding of the JPG may fail on some handsets and you should handle this elegantly.

When you say logo, do you mean icon?  If so, it may or may not show depending upon the size of the icon and the device that you're trying to display it on.

Using the * in an import doesn't make any difference except for readability.  The Sun coding guidelines say that you should list each class that is imported separately (ie. don't use the *), however, this tends to get a bit impractical ;-)  Personally, if I use more than two or three classes from a package, I tend to use the * unless there is a good reason not to (eg. if I'm using classes with names that clash).
0
 
bhprConnect With a Mentor Commented:
This code may help you.
And as i know MIDP1 does not supports jpg images.
May be this is reason your icon is not displaying.

import javax.microedition.io.*;
import java.io.*;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;


public class ShowImage extends MIDlet {
      
      private Display myDisplay;
      private Canvas myCanvas;
      
      public ShowImage() {
            myCanvas = new ImageCanvas();
      }

      
    void init() {}
    void destroy() {}
    public void startApp() throws MIDletStateChangeException {
            myDisplay = Display.getDisplay(this);
            myDisplay.setCurrent(myCanvas);
    }
    public void pauseApp() {}
 
    public void destroyApp(boolean cond) {}
      
      
}

class ImageCanvas extends Canvas {
      /*
     * Ask to paint itself
     */      

      protected void paint(Graphics g) {
            
            try {
                  Image dispImage = null;
                  dispImage = Image.createImage("/bhpr.png");
            
                  //to put image in the center
                  int X,Y,x,y;
                  X=0; Y = 0; x =0; y =0;
                  X = this.getWidth();
                  Y = this.getHeight();
                  x = dispImage.getWidth();
                  y = dispImage.getHeight();
                  X = (X -x) / 2;
                  Y = (Y -y) / 2;                  
                  g.drawImage(dispImage,X,Y,Graphics.TOP|Graphics.LEFT);
                  g.drawString("bhpr",this.getHeight()/2,this.getWidth()/2,Graphics.TOP|Graphics.HCENTER);
            } catch (IOException ex) {
                  System.out.println("imageException");
                  ex.printStackTrace();
            }
            

      }

}

bhpr
'Knowledge is power'
0
 
khamoudaAuthor Commented:
Thanks, this is pretty smilar to what I've done,

by the way midp1 supports jpg, but when I tried in a Canvas it didn't work !. By the way, what is the difference between mutable & immutable image ?

so according to you I can draw over an image file ?,

- The other question is the application logo, I put a logo in the res directory and also in the jad file, but not being shown in the mobile I don't know why ?

- one more question, in the importing statements above does it matter if you put an "*" instead of that details ?, does it save memory ?

thanks for the help
0
 
khamoudaAuthor Commented:

Thanks jim !
0
 
jimmackCommented:
;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.