icepricessa
asked on
how to get the time out of the stopwatch code in J2ME
i there i am new to this and have a piece of code on a stopwatch
[code]
public void paint(Graphics g)
{
if(!reset)
{
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
g.setColor(0);
g.setFont(defaultFont);
long temp = currentTime - startTime;
hh = temp / 0x36ee80L;
mm = (temp - hh * 3600L * 1000L) / 60000L;
ss = (temp - hh * 3600L * 1000L - mm * 60L * 1000L) / 1000L;
ii = (temp % 1000L) / 10L;
if(hh < 10L)
hhStr = "0" + hh;
else
hhStr = String.valueOf(hh);
if(mm < 10L)
mmStr = "0" + mm;
else
mmStr = String.valueOf(mm);
if(ss < 10L)
ssStr = "0" + ss;
else
ssStr = String.valueOf(ss);
if(ii < 10L)
iiStr = "0" + ii;
else
iiStr = String.valueOf(ii);
str = hhStr + ":" + mmStr + ":" + ssStr + ":" + iiStr;
g.drawString(str, xStr, yStr, 65);
} else
{
reset = false;
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
g.setColor(0);
g.setFont(defaultFont);
g.drawString("00:00:00:00" , xStr, yStr, 65);
}
}
[/code]
hmm.. right now i've got to use this piece of code and extract out the time when the application has stopped and store it then i have to compare it with previous timings... how do i achieve that? currently i've added this method
[code] public void getTime()
{
System.out.println("getTim e() was invoked");
long temp = currentTime - startTime;
hh = temp / 0x36ee80L;
mm = (temp - hh * 3600L * 1000L) / 60000L;
ss = (temp - hh * 3600L * 1000L - mm * 60L * 1000L) / 1000L;
ii = (temp % 1000L) / 10L;
if(hh < 10L)
hhStr = "0" + hh;
else
hhStr = String.valueOf(hh);
if(mm < 10L)
mmStr = "0" + mm;
else
mmStr = String.valueOf(mm);
if(ss < 10L)
ssStr = "0" + ss;
else
ssStr = String.valueOf(ss);
if(ii < 10L)
iiStr = "0" + ii;
else
iiStr = String.valueOf(ii);
//build string to display
str = hhStr + ":" + mmStr + ":" + ssStr + ":" + iiStr;
System.out.println(str);
}
[/code]
but i dont think it is correct pls someone enlighten me
[code]
public void paint(Graphics g)
{
if(!reset)
{
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
g.setColor(0);
g.setFont(defaultFont);
long temp = currentTime - startTime;
hh = temp / 0x36ee80L;
mm = (temp - hh * 3600L * 1000L) / 60000L;
ss = (temp - hh * 3600L * 1000L - mm * 60L * 1000L) / 1000L;
ii = (temp % 1000L) / 10L;
if(hh < 10L)
hhStr = "0" + hh;
else
hhStr = String.valueOf(hh);
if(mm < 10L)
mmStr = "0" + mm;
else
mmStr = String.valueOf(mm);
if(ss < 10L)
ssStr = "0" + ss;
else
ssStr = String.valueOf(ss);
if(ii < 10L)
iiStr = "0" + ii;
else
iiStr = String.valueOf(ii);
str = hhStr + ":" + mmStr + ":" + ssStr + ":" + iiStr;
g.drawString(str, xStr, yStr, 65);
} else
{
reset = false;
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
g.setColor(0);
g.setFont(defaultFont);
g.drawString("00:00:00:00"
}
}
[/code]
hmm.. right now i've got to use this piece of code and extract out the time when the application has stopped and store it then i have to compare it with previous timings... how do i achieve that? currently i've added this method
[code] public void getTime()
{
System.out.println("getTim
long temp = currentTime - startTime;
hh = temp / 0x36ee80L;
mm = (temp - hh * 3600L * 1000L) / 60000L;
ss = (temp - hh * 3600L * 1000L - mm * 60L * 1000L) / 1000L;
ii = (temp % 1000L) / 10L;
if(hh < 10L)
hhStr = "0" + hh;
else
hhStr = String.valueOf(hh);
if(mm < 10L)
mmStr = "0" + mm;
else
mmStr = String.valueOf(mm);
if(ss < 10L)
ssStr = "0" + ss;
else
ssStr = String.valueOf(ss);
if(ii < 10L)
iiStr = "0" + ii;
else
iiStr = String.valueOf(ii);
//build string to display
str = hhStr + ":" + mmStr + ":" + ssStr + ":" + iiStr;
System.out.println(str);
}
[/code]
but i dont think it is correct pls someone enlighten me
ASKER
ok... erm.. how am i supposed to implemet is?
is it like this?
public void getTime(){
return currentTimeMillis();
}
is it like this?
public void getTime(){
return currentTimeMillis();
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
here's the rest of the code
class GameTimer implements Runnable
{
private Thread thread;
private int height, width;
private int yStr, xStr;
private boolean goOn, reset, pause;
private long currentTime, startTime, hh, mm, ss, ii;
private Font defaultFont;
private String str, hhStr, mmStr, ssStr, iiStr;
private GameCanvas canvas;
GameTimer(GameCanvas c)
{
canvas = c;
reset = false;
width = canvas.getWidth();
height = canvas.getHeight();
xStr = (width/2)+30;
yStr = (height/2)-54;
//sets the default font type, size and style
defaultFont = Font.getFont(Font.FACE_PRO PORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
}
public void startThread()
{
System.out.println("thread started");
goOn = true;
thread = new Thread(this);
thread.start();
pause= true;
}
public void run()
{
while(goOn)
{
if(pause)
synchronized(this)
{
try
{
System.out.println("about to invoke the wait() method");
wait();
}
catch(Exception exception) { }
}
try {
currentTime = System.currentTimeMillis() ;
canvas.repaint();
}catch(Exception exception) { }
}
}
void setTimeMs(long l)
{
}
public long getTimeMs()
{
return 0L;
}
public void restartThread()
{
if(thread != null)
stopThread();
startThread();
}
public void stopThread()
{
System.out.println("thread stopped");
goOn = false;
if(thread != null)
thread = null;
}
public void startStopWatch()
{
pause = false;
System.out.println("startS topWatch() was invoked");
startTime = System.currentTimeMillis() ;
synchronized(this)
{
notify();
}
}
public void stopStopWatch()
{
System.out.println("stopSt opWatch() was invoked");
pause = true;
canvas.repaint();
}
public void resetStopWatch()
{
System.out.println("resetS topWatch() was invoked");
currentTime = 0L;
startTime = 0L;
reset = true;
canvas.repaint();
}
class GameTimer implements Runnable
{
private Thread thread;
private int height, width;
private int yStr, xStr;
private boolean goOn, reset, pause;
private long currentTime, startTime, hh, mm, ss, ii;
private Font defaultFont;
private String str, hhStr, mmStr, ssStr, iiStr;
private GameCanvas canvas;
GameTimer(GameCanvas c)
{
canvas = c;
reset = false;
width = canvas.getWidth();
height = canvas.getHeight();
xStr = (width/2)+30;
yStr = (height/2)-54;
//sets the default font type, size and style
defaultFont = Font.getFont(Font.FACE_PRO
}
public void startThread()
{
System.out.println("thread
goOn = true;
thread = new Thread(this);
thread.start();
pause= true;
}
public void run()
{
while(goOn)
{
if(pause)
synchronized(this)
{
try
{
System.out.println("about to invoke the wait() method");
wait();
}
catch(Exception exception) { }
}
try {
currentTime = System.currentTimeMillis()
canvas.repaint();
}catch(Exception exception) { }
}
}
void setTimeMs(long l)
{
}
public long getTimeMs()
{
return 0L;
}
public void restartThread()
{
if(thread != null)
stopThread();
startThread();
}
public void stopThread()
{
System.out.println("thread
goOn = false;
if(thread != null)
thread = null;
}
public void startStopWatch()
{
pause = false;
System.out.println("startS
startTime = System.currentTimeMillis()
synchronized(this)
{
notify();
}
}
public void stopStopWatch()
{
System.out.println("stopSt
pause = true;
canvas.repaint();
}
public void resetStopWatch()
{
System.out.println("resetS
currentTime = 0L;
startTime = 0L;
reset = true;
canvas.repaint();
}
ASKER
haha yes managed to get it to work :) thanks alot... hmm
well... there a small problem i was wondering if it could be solved...
when the value gets printed out if it is 2.65sec displayed on the stopwatch 2265 would get printed out instead
is therea way to solve this? =)
well... there a small problem i was wondering if it could be solved...
when the value gets printed out if it is 2.65sec displayed on the stopwatch 2265 would get printed out instead
is therea way to solve this? =)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
haha ok so i did this
public void getTime()
{
System.out.println("getTim e() was invoked");
long temp = currentTime - startTime;
hh = temp / 0x36ee80L;
mm = (temp - hh * 3600L * 1000L) / 60000L;
ss = (temp - hh * 3600L * 1000L - mm * 60L * 1000L) / 1000L;
ii = (temp % 1000L) / 10L;
if(hh < 10L)
hhStr = "0" + hh;
else
hhStr = String.valueOf(hh);
if(mm < 10L)
mmStr = "0" + mm;
else
mmStr = String.valueOf(mm);
if(ss < 10L)
ssStr = "0" + ss;
else
ssStr = String.valueOf(ss);
if(ii < 10L)
iiStr = "0" + ii;
else
iiStr = String.valueOf(ii);
//build string to display
str = hhStr + ":" + mmStr + ":" + ssStr + ":" + iiStr;
System.out.println(str);
}
and it works thanks you so much =)
just one other question... when i use the get time it'll be displayed as 00:00:04:09
so when i store this in recordstore or database how would i use this method to compare with a previous timing for example compare 00:00:04:09 and 00:00:01:29 to see which is a longer time
public void getTime()
{
System.out.println("getTim
long temp = currentTime - startTime;
hh = temp / 0x36ee80L;
mm = (temp - hh * 3600L * 1000L) / 60000L;
ss = (temp - hh * 3600L * 1000L - mm * 60L * 1000L) / 1000L;
ii = (temp % 1000L) / 10L;
if(hh < 10L)
hhStr = "0" + hh;
else
hhStr = String.valueOf(hh);
if(mm < 10L)
mmStr = "0" + mm;
else
mmStr = String.valueOf(mm);
if(ss < 10L)
ssStr = "0" + ss;
else
ssStr = String.valueOf(ss);
if(ii < 10L)
iiStr = "0" + ii;
else
iiStr = String.valueOf(ii);
//build string to display
str = hhStr + ":" + mmStr + ":" + ssStr + ":" + iiStr;
System.out.println(str);
}
and it works thanks you so much =)
just one other question... when i use the get time it'll be displayed as 00:00:04:09
so when i store this in recordstore or database how would i use this method to compare with a previous timing for example compare 00:00:04:09 and 00:00:01:29 to see which is a longer time
ASKER
i''ve tried to do this in the method
str = hhStr + "." + mmStr + "." + ssStr + "." + iiStr;
long s = parseLong(str);
but i get a compilation error :x
str = hhStr + "." + mmStr + "." + ssStr + "." + iiStr;
long s = parseLong(str);
but i get a compilation error :x
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:°)
ASKER
=)
System.currentTimeMillis()