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();
Related
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);
I have a problem and i can not find any solution to this problem.
I am using an animationTimer to update everything and i try to fix it on 60fps as it should do it by itself. The Problem is that on my first screen it runs on 144 fps and on my second with 60fps. I have a Gsync monitor as my first screen and i think because i force Gsync the animationTimer is fixed to 144fps. Is there any method to prevent the animationTimer go further 60fps?
AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long now) {
long elapsedNanosPerFrame = 0;
long oldFrameTime = frameTimes[frameTimeIndex] ;
double frameRate = 0;
frameTimes[frameTimeIndex] = now ;
frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length ;
if (frameTimeIndex == 0) {
arrayFilled = true ;
}
if (arrayFilled) {
long elapsedNanos = now - oldFrameTime ;
elapsedNanosPerFrame = elapsedNanos / frameTimes.length ;
while () {
}
frameRate = 1_000_000_000.0 / elapsedNanosPerFrame ;
label.setText(String.format("Current frame rate: %.3f", frameRate));
label.setTextFill(Color.GREEN);
}
update(elapsedNanosPerFrame);
}
};
And yes it is not a visible Label bug or something, the gameplay is faster.
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
ns is 1000000000 / amountOfTicks;
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1) {
tick(glad);
updates++;
delta--;
fps_counter = 0;
}
if (delta >= fps_counter / FramesPerTick) { // render
fps_counter++;
frames++;
render(glad);
}
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
frame.setTitle("Ticks: " + updates + " Fps: " + frames);
updates = 0;
frames = 0;
}
So if i have 20 ticks and 3 FramesPerTick i get 60fps
it checks whenever delta is bigger than 1/3, 2/3 and 3/3, so every tick has 3 frames
Everything is jittery, movement, mouse look.
Animation gets more and more jittery the lower the FPS, i dont understand why.
Also when i take the render(glad) method out of the if, it goes up to 400 fps and runs very smoothly.
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;