Solved

in J2ME , midp1, Image drawing

Posted on 2004-08-19
5
924 Views
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 ?

thanks

0
Comment
Question by:khamouda
[X]
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
5 Comments
 
LVL 4

Assisted Solution

by:bhpr
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.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
 
LVL 1

Author Comment

by:khamouda
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
0
 
LVL 15

Accepted Solution

by:
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).
0
 
LVL 1

Author Comment

by:khamouda
ID: 11862382

Thanks jim !
0
 
LVL 15

Expert Comment

by:jimmack
ID: 11869882
;-)
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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…
In this article I will describe how to setup a Cisco WLC 5508 to work with Apple's Bonjour protocol across VLANs.  I will also discuss using screen mirroring and Airplay on an AppleTV v3.  This article covers the wireless network only and requires m…
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…

630 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