Image and ImageItem sample program in J2ME

By: Norman Chap Emailed: 1605 times Printed: 2081 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Several MIDP UI components support the display of an image. Here, a form contains an ImageItem component, which displays an image.

The constructor creates an image object and passes it to the UI component for display. Notice that the path specification for the image is relative to the resource directory of this project under the J2ME Wireless Toolkit installation.

The program below demonstrates the use of the ImageItem MIDP UI component class. An ImageItem is a subclass of Item, so it must be placed in a Form as demonstrated by the listing. Before you can display an image, you must first create an image object. The javax.microedition.lcdui.Image class defines images. To instantiate Image, specify the path name of an image file. Image files must be stored in the Portable Network Graphics (PNG) format. J2ME supports the manipulation of images in this format only.

Notice in the program below that the path name of the image file is relative to the res/ directory of the UIComponents project directory. The res/ directory contains all resource files, including image files. If you place your images elsewhere, they will not be found, and your program will throw an IOException when it tries to open the file.

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import java.io.IOException;
/**
This class demonstrates the use of the MIDP UI
ImageItem class.
@see javax.microedition.lcdui.ImageItem
95
*/
public class ImageItemDemo extends Form
implements CommandListener
{
...
private ImageItem imageItem;
/**
Constructor.
@throws IOException if the specified image resource
cannot be found.
*/
public ImageItemDemo() throws IOException
{
super("ImageItem Demo");
String path = "/bottle80x80.png";
Image image = Image.createImage(path);
imageItem = new ImageItem("Ship in a bottle",
image,
ImageItem.LAYOUT_CENTER,
"Image not found");
append(imageItem);
addCommand(back);
setCommandListener(this);
instance = this;
}
...
}

The constructor builds an ImageItem using the Image object just created. The constructor parameters are the title string that displays above the image, the image object, the image placement directive, and a text string to be displayed in case the image can't be displayed for some reason.

The ImageItem class is the only class that provides layout control for images, but several other MIDP UI components use images, too.


J2ME Home | All J2ME Tutorials | Latest J2ME Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

i did the same thing..but it s nt working.. :(

View Tutorial          By: dEviL at 2008-12-03 10:20:31
2. View Comment

Hi,

Are you getting any specific error? As mentioned, did you check the file path properly?


View Tutorial          By: Norman Chap at 2008-12-04 02:42:47
3. View Comment

It is throwing exception....Image is not displaying even i used try block
Wat could i do to display image?


View Tutorial          By: Aswin at 2012-02-12 08:07:41

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Norman Chap
The break Statement example in Java
Stored Procedures example in SQL
JDBC and Tomcat context settings
File read and write - sample program in C
Pointers and Function Arguments in C
Writing the first Struts application
goto and labels in C
External Variables and Scope in C
Multiple inheritance example in C++
qsort() sample program in C++
Image and ImageItem sample program in J2ME
SimpleDateFormat sample program in Java
Observable class and the sample program in Java

More Tutorials in J2ME
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
POST UTF-8 encoded data to the server in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Using List to create a Menu and Menu items in J2ME
Using alerts and tickers in J2ME
J2ME Canvas sample to show games programming in J2ME
Timer and TimerTask example in J2ME
List of GPRS Access points for all countries

More Latest News
Most Viewed Articles (in J2ME )
Download a file over a network in J2ME midlet
TextBox sample program in J2ME
POST UTF-8 encoded data to the server in J2ME
Client Server in J2ME (Socket Programming sample)
GUI components and menu based J2ME Applications.
'double buffering' Sample program in J2ME
Types of configurations in J2ME
getPosition() the right strategy in getting GPS
Adding your own Application icon for your J2ME application (jar file)
Y.S. Sun Green Building Research Center
Sample Java program shows how to Read a file over a network using J2ME midlet
DateField sample program in J2ME
Gauge sample program in J2ME
Sample program to draw a rectangle in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Most Emailed Articles (in J2ME)
How to load J2ME applications to the IDEN handsets
Gauge sample program in J2ME
What is J2ME?
Y.S. Sun Green Building Research Center
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Download a file over a network in J2ME midlet
DateField sample program in J2ME
Ticker sample program in J2ME
paint() sample program to draw a line in J2ME
RMS Basics in J2ME
Sample J2ME code that shows various functionality of RMS.
GUI components and menu based J2ME Applications.
java.lang.SecurityException: MIDP lifecycle does not support system exit.
TextBox sample program in J2ME
Sample program to draw a rectangle in J2ME