Membuat Program StopWatch di J2ME


di sini saya akan mencoba membuat aplikasi sederhana program stopwatch dengan mennggunakan J2ME yang berjalan di aplikasi netbeans 6.5. mungkin dapat dkembangkan lagi pemogramman ini.

Ini source code nya silahkan di pelajari Smile

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
* by Indra Joni Pratama
*/
public class stopwatch extends MIDlet implements CommandListener {
    Display layar;
    Form fm;
    Timer tm;
    TimerJalan task;
    TextField tf;
    Command start,pause;

    public void startApp() {
        layar=Display.getDisplay(this);
        fm=new Form("Pemakaian Timer");

        tf=new TextField("Stop Watch","",50,TextField.ANY);
        start=new Command("Start",Command.OK,0);
        pause=new Command("pause",Command.OK,0);

        fm.append(tf);
        fm.addCommand(start);
        fm.setCommandListener((CommandListener) this);
        layar.setCurrent(fm);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if(c==start){
            fm.removeCommand(start);
            fm.addCommand(pause);
             tm=new Timer();
             task=new TimerJalan();
             tm.schedule(task,0,100);
        }
        if(c==pause){
            fm.removeCommand(pause);
            fm.addCommand(start);
            tm.cancel();
        }
    }
    public class TimerJalan extends TimerTask {
        int i,mdetik,detik,menit,jam;
        public final void run(){
            ++mdetik;
            if (mdetik>=9){
                detik=detik+1;
                mdetik=0;
            }
            if(detik>59){
                menit=menit+1;
                detik=0;
            }
              if(menit>59){
                  jam=jam+1;
                  menit=0;
              }
              tf.setString(jam+":"+menit+":"+detik+":"+mdetik);

              }
        }

}

Silahkan di coba program codingnya..

kalau yang mau coba langsung aplikasinya di hanphone berbasis java silahkan dowload disini

ini nih gambar aplikasi stopwatch sederhana

image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s