Image and ImageItem sample program in J2ME
By: Norman Chap
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.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
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
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
Client Server in J2ME (Socket Programming sample)
Datagrams in J2ME (UDP Programming sample)
POST UTF-8 encoded data to the server in J2ME
Using alerts and tickers in J2ME
Using List to create a Menu and Menu items in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Timer and TimerTask example in J2ME
Archived Comments
1. i did the same thing..but it s nt working.. :(
View Tutorial By: dEviL at 2008-12-03 10:20:31
2. Hi,
Are you getting any specific er
View Tutorial By: Norman Chap at 2008-12-04 02:42:47
3. It is throwing exception....Image is not displayin
View Tutorial By: Aswin at 2012-02-12 08:07:41