in J2ME , midp1, Image drawing

Posted on 2004-08-19
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 25 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 100 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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dual Band Channels 7 47
CCNA WiFi Exam Question 2 63
Powerline adapter slow Mbps? 38 215
Regulatory "B" support - what is the minimum code for Cisco wireless controllers 2 33
Coaxial cable bending There are several factors that govern the selection of coaxial cable for your Machine to Machine (M2M) application: the location of cable runs, either indoor or outdoor, inside or outside an enclosure, maximum bending and the…
With the purchase of CloudCommand by Comcast customers are left in a bind as subscriptions expire and render the AP's disabled. The following will explain how to flash your Ubiquiti AP's with CloudCommand firmware back to Ubiquiti firmware. HOWTO…
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…

733 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