Solved

in J2ME , midp1, Image drawing

Posted on 2004-08-19
5
859 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today sees the launch of a new case study, focusing on BYOD technologies we have been working with for some time now.  But with the advent of 802.11ac wireless technologies and the story behind our landmark developments, we would like to share this …
Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now