A stopwatch in Java ME
(→StopWatchThread.java) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Fix categories) |
||
| (6 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
| − | [[Category:Java ME]][[Category:Code Examples]][[Category: | + | [[Category:Java ME]][[Category:Code Examples]][[Category:UI]] |
| + | {{ArticleMetaData | ||
| + | |sourcecode=[[Media:StopWatch.zip]] | ||
| + | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
| + | |devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') --> | ||
| + | |sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) --> | ||
| + | |platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later --> | ||
| + | |devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) --> | ||
| + | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | ||
| + | |signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer --> | ||
| + | |capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> | ||
| + | |keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase --> | ||
| + | |id= <!-- Article Id (Knowledge base articles only) --> | ||
| + | |language=<!-- Language category code for non-English topics - e.g. Lang-Chinese --> | ||
| + | |review-by=<!-- After re-review: [[User:username]] --> | ||
| + | |review-timestamp=<!-- After re-review: YYYYMMDD --> | ||
| + | |update-by=<!-- After significant update: [[User:username]]--> | ||
| + | |update-timestamp=<!-- After significant update: YYYYMMDD --> | ||
| + | |creationdate=20080911 | ||
| + | |author=[[User:Bogdan.galiceanu]] | ||
| + | }} | ||
| − | + | {{Abstract|The article contains an example stopwatch application, which has the ability to pause/resume and reset.}} | |
| + | |||
| + | [[File:StopWatch_screenshot1.jpg]] [[File:StopWatch_screenshot2.jpg]] | ||
==StopWatch.java== | ==StopWatch.java== | ||
| Line 251: | Line 273: | ||
} | } | ||
</code> | </code> | ||
| − | |||
| − | |||
| − | |||
| − | |||
==Download== | ==Download== | ||
| − | [ | + | * [[Media:StopWatch.zip|Project containing source code, JAR and JAD]] |
| + | <!-- Translation --> [[pt:Cronômetro em JavaME]] | ||
Latest revision as of 08:40, 5 July 2012
Article Metadata
Code Example
Source file: Media:StopWatch.zip
Article
Created: bogdan.galiceanu
(11 Sep 2008)
Last edited: hamishwillee
(05 Jul 2012)
The article contains an example stopwatch application, which has the ability to pause/resume and reset.
Contents |
StopWatch.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class StopWatch extends MIDlet {
private Display display;
private StopWatchCanvas canvas;
public StopWatch() {
display=Display.getDisplay(this);
canvas=new StopWatchCanvas(this);
}
public void startApp() {
display.setCurrent(canvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
StopWatchCanvas.java
import javax.microedition.lcdui.*;
public class StopWatchCanvas extends Canvas implements CommandListener {
private Command exit;
private Command start;
private Command pause;
private Command reset;
private StopWatch midlet;
private StopWatchThread thread;
private final String startValue="00:00:00";
private Image image=null;
public StopWatchCanvas(StopWatch midlet){
this.midlet=midlet;
//Start the thread, stopwatch will remain paused
thread=new StopWatchThread(this);
thread.start();
//Create the commands
exit=new Command("Exit", Command.EXIT, 1);
start=new Command("Start", Command.ITEM, 1);
pause=new Command("Pause", Command.ITEM, 1);
reset=new Command("Reset", Command.SCREEN, 2);
addCommand(exit);
addCommand(start);
setCommandListener(this);
}
public void setImage(Image img) {
this.image=img;
}
protected void paint(Graphics g) {
if(image!=null)
//Draw text updated by the thread
g.drawImage(image, getWidth()/2, getHeight()/2, Graphics.VCENTER|Graphics.HCENTER);
else {
//Display a white rectangle and black text
g.setColor(255,255,255);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(0, 0, 0);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString(startValue, this.getWidth()/2, this.getHeight()/2, Graphics.BASELINE|Graphics.HCENTER);
}
}
public void commandAction(Command c, Displayable d) {
if(c==exit) {
//Stop the thread
thread.killThread();
//Exit the midlet
midlet.notifyDestroyed();
}
else if(c==start) {
//Start or resume the stopwatch
thread.startWatch();
removeCommand(start);
addCommand(pause);
addCommand(reset);
}
else if(c==pause) {
//Pause the stopwatch
thread.pauseWatch();
removeCommand(pause);
addCommand(start);
addCommand(reset);
}
else {
//Reset the counter
if(!thread.isPaused()) {
thread.pauseWatch();
thread.resetWatch();
removeCommand(pause);
removeCommand(reset);
addCommand(start);
}
else {
thread.resetWatch();
removeCommand(reset);
}
//Clear the image
setImage(null);
}
repaint();
}
}
StopWatchThread.java
import java.util.*;
import javax.microedition.lcdui.*;
public class StopWatchThread extends Thread {
private StopWatchCanvas canvas;
boolean paused=true;
boolean stopped=false;
private Date startTime, pauseTime;
private long totaltime=0;
private Image img;
public StopWatchThread(StopWatchCanvas canvas) {
this.canvas=canvas;
}
//Stops the thread
public synchronized void killThread() {
stopped=true;
}
//Determines whether or not the thread is running
public synchronized boolean threadIsRunning() {
return !stopped;
}
//Starts the stopwatch (or resumes after being paused)
public synchronized void startWatch() {
if(startTime==null)
startTime=new Date();
if ( img == null) img = Image.createImage(240, 160);
paused=false;
//Wake up the thread
notifyAll();
}
//Pauses the stopwatch
public synchronized void pauseWatch() {
paused=true;
pauseTime=new Date();
totaltime=pauseTime.getTime()-startTime.getTime()+totaltime;
//Make startTime null so that on startWatch() we get a new startTime
startTime=null;
}
//Resets the stopwatch
public synchronized void resetWatch() {
//Make startTime null
startTime=null;
//Reset totaltime
totaltime=0;
}
public synchronized boolean isPaused() {
return paused;
}
private String getTime() {
Calendar c=Calendar.getInstance();
Date rightNow=new Date();
c.setTime(new Date(rightNow.getTime()-startTime.getTime()+totaltime));
//We work with minutes, seconds and centiseconds
int min=c.get(c.MINUTE);
int sec=c.get(c.SECOND);
int csec=c.get(c.MILLISECOND)/10;
String smin;
String ssec;
String scsec;
//Format the values so that they will look appropriate when displayed
if(min<10)
smin="0"+min;
else
smin=String.valueOf(min);
if(sec<10)
ssec="0"+sec;
else
ssec=String.valueOf(sec);
if(csec<10)
scsec="0"+csec;
else
scsec=String.valueOf(csec);
return smin+":"+ssec+":"+scsec;
}
public void run() {
while(threadIsRunning()) {
try {
if(!isPaused()) {
try {
Graphics g=img.getGraphics();
g.setColor(255,255,255);
g.fillRect(0, 0, img.getWidth(), img.getHeight());
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString(getTime(), img.getWidth()/2, img.getHeight()/2, Graphics.BASELINE|Graphics.HCENTER);
canvas.setImage(img);
canvas.repaint();
}
catch(Exception e) {
}
Thread.sleep(10);
}
else {
synchronized(this) {
wait();
}
}
}
catch(InterruptedException e) {
}
}
}
}



