Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


in J2ME , midp1, Image drawing

Posted on 2004-08-19
Medium Priority
Last Modified: 2008-03-17

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 ?


Question by:khamouda
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Assisted Solution

bhpr earned 100 total points
ID: 11855755
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.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);
    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;                  
            } catch (IOException ex) {



'Knowledge is power'

Author Comment

ID: 11857704
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
LVL 15

Accepted Solution

jimmack earned 400 total points
ID: 11861351
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).

Author Comment

ID: 11862382

Thanks jim !
LVL 15

Expert Comment

ID: 11869882

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Need WiFi? Often, there are perfectly good networks that don't have WiFi capability - and there's a need to add it.  - Perhaps you have an Ethernet port into a network but no WiFi nearby. - Perhaps you have a powerline extender and no WiFi at the…
This subject  of securing wireless devices conjures up visions of your PC or mobile phone connecting to the Internet through some hotspot at Starbucks. But it is so much more than that. Let’s look at the facts: devices#sthash.eoFY7dic.
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question