Hi Everyone,
Recently i have started a small J2ME project for my personal use. I have developed J2ME application based on calculations which doesnot require "DELAY". Currently i am facing a problem, where i have to implement a delay between two commands.

The way I want this delay to work is something like this:

//and after changing the image

I have tried two ways in implementing this, the first one is:


But the problem i am facing using this is that my application stops for 1 second or so right at the start,this can also be termed as loading time and hence i dont get any delay between changing of pictures.

The second method is;

I made a function named as delay_c which does the following operation:

double i;


//Depending on the clock frequency of the phone i can choose the final limit

But sadly this method is behaving the same way as Thread.sleep. I was wondering if I am missing a trick here, because I used Robot class Delay in JAVA applications, as Robot class is not available in J2ME, I am struggling to solve this delay problem.
By the way i have simulated the application on simulator at first and then tried it on phone and it gives the same result.


Recommended Answers

All 5 Replies

Luckily i have found a way:
It can be done using System.currentTimeMillis()
If there is a way better than this, then please let me know thanks

Looks like logic is not correct. I guess you are building form and appending images on build. What you need is build form with one image and display it, in same time set sleep thread which will then call method to append new image

ok That makes sense, Here is the code I am trying to make, the line commented between two function(line 36) is my problem, if I use any delay I just get a long wait at the start of the Application:

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand; // The exit command
    private Display display;     // The display for this MIDlet
    public ImageItem im_item;
    public Form form1;
    public Image image1,image2,image3,image4,image5,image6;
    public HelloMIDlet() {
            image1 = Image.createImage("/pic1.jpg");
            image2 = Image.createImage("/pic2.jpg");
            image3 = Image.createImage("/pic14.jpg");
            image4 = Image.createImage("/pic21.jpg");
            image5 = Image.createImage("/pic113.jpg");
            image6 = Image.createImage("/s.jpg");
        catch(Exception e)
        {/*DO nothing*/}

        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 0);

    public void startApp() {
        form1 = new Form("APP");

         /*Need delay here,But Thread sleep, for loop or System.mill.... 
          doesnot give me the required result*/


    public void pauseApp() {

    public void destroyApp(boolean unconditional) {

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {

 public void function1(Image a)
         im_item = new ImageItem("",a, ImageItem.LAYOUT_CENTER, "image");


Thanks for help in advance

Sorry for delay, here is a working example

package hello;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

public class HelloMIDlet extends MIDlet{
  private Display display;

  public void startApp() {

        if(display == null){
            display = Display.getDisplay(this);
        display.setCurrent(new MainScreen(this));

  public void pauseApp(){}

    public void destroyApp(boolean unconditional){}

    public void close(){

and form view MainScreen.java

package hello;

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;

public class MainScreen extends Form implements CommandListener, Runnable {
  private HelloMIDlet midlet;
  private Command exitCommand;
  private ImageItem im_item;
  private Image image1, image2;
  private String[] imgSrc = {"/res/r001.png", "/res/r012.png",
    "/res/r024.png", "/res/r033.png", "/res/r039.png", "/res/r048.png"};

  public MainScreen(HelloMIDlet midlet) {
    super("My Form");
    this.midlet = midlet;

  private void init() {
    image1 = imageLoader(imgSrc[0]);
    im_item = new ImageItem("", image1, ImageItem.LAYOUT_CENTER, "");
    exitCommand = new Command("Exit", Command.EXIT, 0);
    Thread t = new Thread(this);

  public void commandAction(Command c, Displayable d) {
    if (c == exitCommand) {

  private Image imageLoader(String src) {
    Image img = null;
    try {
      img = Image.createImage(src);
    catch (IOException e) {
    return img;

  public void run() {
    try {
      Thread.sleep(3000);//set for 3 seconds
    catch (InterruptedException e) {

  private void appendForm(){
    image2 = imageLoader(imgSrc[1]);
    ImageItem ii = new ImageItem("", image2, ImageItem.LAYOUT_CENTER, "");

PS: Try to keep image outside of Java package for better organization.

commented: Perfect Example for my Question +3

Thank you very much for the example now i can mark this thread as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.