Saturday 28 July 2012

Membuat Stopwatch Sederhana dengan java2ME

No comments
Langsung saja saya kasihkan source code'y..

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

public class stopwatch extends MIDlet implements CommandListener{
int i, mdetik, detik, menit, jam;

 Display layar;
 Form frm;
 Timer tm;
 TextField tf;
 TimerJalan task;
 Command start, pause, reset;

public void startApp(){
layar=Display.getDisplay(this);
frm=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);
reset=new Command("reset",Command.OK,0);

frm.append(tf);
frm.addCommand(start);
tm=new Timer();
frm.setCommandListener((CommandListener)this);
layar.setCurrent(frm);
}


public void puseApp(){
}


public void destroyApp(boolean unconditional){
}

public void commandAction(Command c, Displayable d){
if(c==start){
frm.removeCommand(start);
frm.removeCommand(reset);
frm.addCommand(pause);
tm=new Timer();
task=new TimerJalan();
tm.schedule(task, 0, 100);
}


if(c==pause){
frm.removeCommand(pause);
frm.addCommand(start);
frm.addCommand(reset);
tm.cancel();
}
if(c==reset){
i=0;
mdetik=0;
detik=0;
menit=0;
jam=0;
frm.removeCommand(pause);
frm.addCommand(start);
tf.setString("0:0:0:0");
tm=new Timer();
}
}
protected void pauseApp() {
}
public class TimerJalan extends TimerTask{
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);
}
}
}





selesai....semoga bermanfaat

No comments :

Post a Comment