I have already written a 60 seconds countdown timer but I would like to transform this to have a minutes and seconds timer like mm:ss.
Can I rearrange this existing code to get that ?
Timer timer = new Timer();
TimerTask task = new TimerTask() {
int i = 60;
public void run(){
if (i >= 0) {
lab3.setText("Timer " + i--);
}
}
};
timer.scheduleAtFixedRate(task, 0, 1000);
You need to display remaining number of second i in format mimutes:seconds format. If you assume that there are always 60 seconds in a minute:
String time = String.format("%02d:%02d", i / 60, i % 60);
System.out.println(time);
Related
I am wondering if there is a way to update two different operations (in this case, updating and rendering) within a single thread / loop.
In my current scenario I have a TPS (ticks per second) that I want to have set to 64, and FPS (frames per second) that I want to be modifiable by the player.
The reason they cannot be in separate threads like I have been doing is because it causes a inconsistent flickering effect because of the timing between rendering and updating.
I am drawing with BufferStrategy and Canvas
This is what I was doing previously:
// The TPS = 64, the FPS = anything
public void init() {
tick_thread = new Thread(() -> {
long start, elapsed, wait;
while(alive) {
start = System.nanoTime();
input();
tick();
elapsed = System.nanoTime() - start;
wait = (1000 / TPS) - (elapsed / 1000000);
try {
Thread.sleep(wait <= 0 : 1 ? wait);
} catch(Exception e) {
e.printStackTrace(System.out);
}
}
});
render_thread = new Thread(() -> {
long start, elapsed, wait;
while(alive) {
start = System.nanoTime();
render();
elapsed = System.nanoTime() - start;
wait = (1000 / FPS) - (elapsed / 1000000);
try {
Thread.sleep(wait <= 0 : 1 ? wait);
} catch(Exception e) {
e.printStackTrace(System.out);
}
}
});
}
This works well, but I want to move from one thread to a single thread and still update them at a different rate. Like...
public void init() {
game_thread = new Thread(() -> {
long start, elapsed, wait, // etc
while(alive) {
// Some calculations
input(); // RUNS 64 TIMES A SECOND
tick(); // RUNS 64 TIMES A SECOND
render(); // Runs 30, 60, 120 times a second
// Some calculations
}
});
}
If you could help or try helping, it would mean a lot!
I have a timer, which is refreshing a TextView with the current timer value. I also have a EditText.
So, when the timer is running, and I want to write something in the EditText, the TextView freezes, and I can't see any text in the EditText while I'm writing. So, I press the back button to hide the keyboard and stop focusing the EditText, and the TextView just get again the normal, and in this moment I can see what I have written inside the EditText.
Here goes the runnable. The EditText is a simple EditText.
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
timerValue.setText(mins+":"+secs);
customHandler.postDelayed(this, 0);
}
};
change the time of the delay, it's set on zero now
customHandler.postDelayed(this, 0);
to something like this:
customHandler.postDelayed(this, 1000);//every second
Hi, I would like to run method interval of every 15 minutes .
Example: If my application initial server start time example 0:12 it
won't be call timer scheduler run method.
so my timer scheduler run method particular interval of every
hourly 15 minutes like :
0:15,0:30,0:45,1:00,1:15,1:30,1:45,2:00,2:15,2:30,2:45,.....etc.
Below sample snippet code always run method executed when initial
application start please let me know where I have made mistaken?
As per my requirement I need to implement in Timer-Task Scheduler.
private class TimerExample{
private static Timer timer = new Timer();
private static Calendar getFirstTime() {
Calendar cal = Calendar.getInstance();
int currentMinute = cal.get(Calendar.MINUTE);
if (currentMinute < 45) {
cal.set(Calendar.MINUTE, 45);
}
if (currentMinute < 30) {
cal.set(Calendar.MINUTE, 30);
}
if (currentMinute < 15) {
cal.set(Calendar.MINUTE, 15);
}
if (currentMinute >= 45) {
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + 1);
cal.set(Calendar.MINUTE, 0);
}
cal.set(Calendar.SECOND, 0);
return cal;
}
public static void main(String... args) {
Calendar firstTaskTime = getFirstTime();
System.out.println("Task will start at: " + firstTaskTime.getTime());
timer.schedule(new MyTask(), firstTaskTime.getTime(), 1000 * 60 * 15);
}
}
I have a Timer working with JavaFX but all it will count is seconds. How would I get it to do Minutes and Seconds.
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.getKeyFrames().add(
new KeyFrame(Duration.seconds(1),
//new {
new EventHandler<ActionEvent>() {
// KeyFrame event handler
public void handle(ActionEvent event) {
remaining--;
// update timerLabel
timeStamp.setText(
remaining.toString());
if (remaining <= 0) {
timeline.stop();
}
}
}));
timeline.playFromStart();
Duration newValue=timeline.getCurrentTime();
int hTime = (int) newValue.toHours();
int minTime = (int) newValue.toMinutes();
int secTime= (int) newValue.toSeconds();
if(secTime/60>=1){ // this are to display later something like a clock 19:02:20
secTime%=60; //if you want just the time in minutes use only the toMinutes()
}
if(minTime/60>=1){
minTime%=60;
}
hTime = time in hours
minTime = time in minutes
secTime = time in seconds
usually the timeline works with millis so you have to be careful ;)
You Use getTime Method
http://docs.oracle.com/javafx/2/api/javafx/animation/KeyFrame.html
I think this will help you .
You may use java.util.concurrent.TimeUnit class which provides nice conversion facilities
int min = 3;
int sec = 14;
double totalSec = TimeUnit.MINUTES.toSeconds(min) + sec;
I am trying to display a countup timer with format 1.00 resolution. basically seconds and with hundredths of second or 10ms resolution.
I have tried the chronometer function but still no luck, it seems it can only track 1 second resolution.
here is some of my code:
public void stopwatch() {
stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
#Override
public void onChronometerTick(Chronometer arg0) {
countUpmilli = (Long) ((SystemClock.elapsedRealtime() -arg0.getBase())/ 100);
countUpSec = (Long) ((SystemClock.elapsedRealtime() -arg0.getBase()) / 1000);
//asText = (countUp / 60) + ":" + (countUp % 60);
asText = (countUpSec / 1) + "." + (countUpmilli%100);
timertext.setText(asText);
}
});
stopWatch.start();
}
I think you have to use System.nanoTime();