开发者

J2me+images in form

I want to make the clock application where user enters the number in the textbox and click ok then user get the number at every 1 sec. Example if user enter 5 then the timer start the display screen shows the number 1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,...so on.

Now i had taken form and text field for user to enter the number,then a timer which will change the number at every second.and 10 images of number (0-9).As i want to dispaly the number in very large size.Now i had implement this logic in below way:-

public class Clock extends MIDlet implements CommandListener {

public Command GO, Exit;
TextField TxtData;
protected Display display;
int number, counter;
Form form;
private Timer timer;
private TestTimerTask task;
boolean increment, time;
private StringItem s1 = new StringItem("", "");
Image image0;
Image image1;
Image image2;
Image image3;
Image image4;
Image image5;
Image image6;
Image image7;
Image image8;
Image image9;
Image[] secondAnimation;
prot开发者_开发技巧ected void startApp() {
    display = Display.getDisplay(this);
    increment = true;
    time = false;
    form = new Form("Clock");
    TxtData = new TextField("Number:-", "", 5, TextField.NUMERIC);
    try {
        image0 = Image.createImage("/images/0.png");
        image1 = Image.createImage("/images/1.png");
        image2 = Image.createImage("/images/2.png");
        image3 = Image.createImage("/images/3.png");
        image4 = Image.createImage("/images/4.png");
        image5 = Image.createImage("/images/5.png");
        image6 = Image.createImage("/images/6.png");
        image7 = Image.createImage("/images/7.png");
        image8 = Image.createImage("/images/8.png");
        image9 = Image.createImage("/images/9.png");
           secondAnimation = new Image[]{image0,image1,image2, image3, image4, image5, image6, image7, image8, image9};

    } catch (IOException ex) {
      System.out.println("exception");
    }
    GO = new Command("Go", Command.OK, 1);
    Exit = new Command("Exit", Command.EXIT, 2);
    form.append(TxtData);
    form.append(s1);

    form.addCommand(GO);
    form.addCommand(Exit);
    form.setCommandListener(this);
    display.setCurrent(form);
}

protected void pauseApp() {
}

protected void destroyApp(boolean unconditional) {
    timer.cancel();
    notifyDestroyed();
}

public void commandAction(Command cmnd, Displayable dsplbl) {
    String label = cmnd.getLabel();
    if (label.equals("Go")) {
        try {
            System.out.println("txt==" + (TxtData.getString()));
            if (!TxtData.getString().equalsIgnoreCase("")) {
                counter = Integer.parseInt(TxtData.getString());
                if (time) {
                    timer.cancel();
                    task.cancel();
                }
                number = 1;
                timer = new Timer();
                task = new TestTimerTask();
                timer.schedule(task, 1000, 1000);

            }
        } catch (NumberFormatException ex) {
            System.out.println("exception");
        }

    } else if (label.equals("Exit")) {
        destroyApp(true);
    }

}

private class TestTimerTask extends TimerTask {

    public final void run() {
        time = true;

        s1.setText(""+ number);
         if (counter < 10) {
            form.append(secondAnimation[0]);
            form.append(secondAnimation[0]);
            form.append(secondAnimation[number]);
        } else if (counter < 100) {
             form.append(secondAnimation[0]);
            form.append(secondAnimation[(number % 100) / 10]);
            form.append(secondAnimation[(number % 10)]);
        } else if (counter < 1000) {
            form.append(secondAnimation[(number % 10)]);
            form.append(secondAnimation[(number % 100) / 10]);
            form.append(secondAnimation[(number / 100)]);
        }
        number++;
        if (number == counter + 1) {
            number = 0;
        }
    }
} }

But as the form goes on appending the image as timer moves it is not showing the desired output!

I had tried to do it through LWUIT but as i had user 10 .png files and adding LWUIT.jar file make the size of .jar file 557kb which is very heavy.

So i want to do it through normal forms only.

I cant use canvas as the keypad can vary like (touch,qwerty etc).So i need to do normal form or LWUIT only.Can anyone please help me for this.


I noticed you only append items but never remove - is that intended?

Also, did you try two different forms to animate instead of one? For simple test, say, fill them in parallel, just call setCurrent for one that is not displayed in the moment of update

//...
private void appendTwice(Image image) {
    form1.append(image);
    form2.append(image);
}
//...
public final void run() {
    time = true;

    s1.setText(""+ number);
     if (counter < 10) {
        appendTwice(secondAnimation[0]);
        //...
    }
    display.setCurrent(number & 1 == 0 ? form1 : form2);
    number++;
    //...
}
//...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜