0

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:

form1.append(imageitem);
//delay 
//and after changing the image
form1.append(imageitem);

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

Thread.sleep(delay);

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;

for(i=0<=1000000;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.

Thanks

Edited by Software guy: n/a

2
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by Software guy
0

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

0

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

0

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() {
        try
        {
            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");

         function1(image1);
         /*Need delay here,But Thread sleep, for loop or System.mill.... 
          doesnot give me the required result*/
         function1(image2);
         Display.getDisplay(this).setCurrent(form1);


    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        } 
    }

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


}

Thanks for help in advance

1

Sorry for delay, here is a working example
HelloMIDlet.java

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(){
        destroyApp(true);
        notifyDestroyed();
    }
}

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;
    init();
  }

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

  public void commandAction(Command c, Displayable d) {
    if (c == exitCommand) {
      midlet.close();
    }
  }

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

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

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

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

Votes + Comments
Perfect Example for my Question
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.