Updating a number in a TextView every second - java

I'm writing my first app in which I need to update the number in a text view every second until a button is pressed. Using a handler seems to be impossible because the number is created and stored outside of the handler, but it can't be declared final because it needs to change. Thread.sleepalso seems to make the app hang indefinitely, before some code is executed that is written above it.
final TextView countView = (TextView) findViewById(R.id.counter);
countView.setText("100,000,000,000");
//set a Calendar object to be 00:00:00:00 on Jan 1, 2015
final Calendar startTime = Calendar.getInstance();
startTime.set(2015, 0, 0, 12, 0, 0);
startTime.set(Calendar.MILLISECOND, 0);
final Button startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//set a Calendar to be the time that the button is clicked and find the difference in ms between it and startTime
Calendar nowTime = Calendar.getInstance();
double milStart = startTime.getTimeInMillis();
double milNow = nowTime.getTimeInMillis();
double diff = (milNow-milStart)/1000; //difference in seconds between "start" date and current date
double total = 100000000000L; //# that needs to be updated
for(long i = 0L; i < diff; i++) {
total += 1.8;
}
countView.setText(NumberFormat.getInstance().format(total));
I need to continue to increment total by 1.8 every second or by 1 every 556 milliseconds.
while(true) {
countView.setText(NumberFormat.getInstance().format(total));
total += 1.8;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
Causes the app to hang indefinitely as soon as the button is clicked, so that even the first countView.setText(NumberFormat.getInstance().format(total)); doesn't execute.
Using a handler doesn't seem possible to me since total can't work if declared final, but creating a variable inside the handler and looping it would cause the value to never change.
Am I missing an obvious solution? This is my first real endeavor with Java so it's all new to me

you may use Handler
Handler h = new Handler();
final Runnable r = new Runnable() {
int count = 0;
#Override
public void run() {
count++;
textView.setText(""+count*1.8);
h.postDelayed(this, 1000); //ms
}
};
h.postDelayed(r, 1000); // one second in ms
for stopping you may use h.removeCallbacksAndMessages(null);

Related

How can I stop a Swing Timer after n seconds?

I'm trying to make an image twinkle with RaffleImage(); while I'm executing the timer, my character is immune to any collision, I want it to be immune only for 2 seconds, so the timer get execute only for 2 seconds and then get finished.
I've tried subtracting System.currentTimeMillis() but any variable I create from this method, have always the same value, making me get a zero from that subtracting.
Do you know how I can stop or pause the timer after any elapsed time in seconds?
immuneTimer = new Timer(50, new ActionListener() {
#Override
public synchronized void actionPerformed(ActionEvent e) {
long initMillis = System.currentTimeMillis();
if (System.currentTimeMillis() - initMillis > 2000 ) { // this substract gives me 0
initImages();
setImmune(false); // so this never reached
immuneTimer.stop();
} else {
raffleImage(); //its executing like forever;
}
}
});
The Swing timer fires a an ActionEvent. From the event you can use getSource() to get the source of the event. Cast that source to the swing timer object and use that to turn it off.
To know when to turn it off you need to have a variable count the number of times the swing timer is invoked. When the variable reaches that amount, turn it off.
int elapsedTime = 0;
int timerDelay = 50;
int max = 2000;
public void actionPerformed(ActionEvent ae) {
elapsedTime += timerDelay; // you could use getDelay here but it
// is in milliseconds.
if (elapsedTime >= max) {
Timer s = (Timer)ae.getSource();
s.stop();
}
// rest of code
}

Java - how to make a timer which displays elapsing time in JLabel

I'm writing the code of my first Java Game right now, so far I have built GUI and I want to add some logic. In my game, user should see elapsing time for his move (begin with 10s), like 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0. I created JLabel and would like to display that elapsing time on it. My program has 3 levels of difficulty, firstly user chooses one by clicking on appropriate JButton and then user should see the timer and some options to choice and play. How can i cope with this problem? I read about Timer class in Java but still don't know how can i display counting-down time on JLabel. Maybe should I implement a game loop, but to be honest I don't have any idea how to make it.
You can simply use a count down timer method and pass your JLabel to it along with the number of seconds to count down and a optional 'End Of Time' message.
There are lots of examples of this sort of thing on the internet but here is my quick rendition of one:
public static Timer CountdownTimer(JLabel comp, int secondsDuration, String... endOfTimeMessage) {
if (secondsDuration == 0) { return null; }
String endMsg = "~nothing~";
if (endOfTimeMessage.length>0) { endMsg = endOfTimeMessage[0]; }
final String eMsg = endMsg;
int seconds = secondsDuration;
final long duration = seconds * 1000;
JLabel label = (JLabel)comp;
final Timer timer = new Timer(10, new ActionListener() {
long startTime = -1;
#Override
public void actionPerformed(ActionEvent event) {
if (startTime < 0) {
startTime = System.currentTimeMillis();
}
long now = System.currentTimeMillis();
long clockTime = now - startTime;
if (clockTime >= duration) {
((Timer)event.getSource()).stop();
if (!eMsg.equals("~nothing~")) { label.setText(eMsg); }
return;
}
SimpleDateFormat df = new SimpleDateFormat("mm:ss:SSS");
label.setText(df.format(duration - clockTime));
}
});
timer.start();
return timer;
}
If you want to change the way the count-down is displayed within the JLabel then you can change the SimpleDateFormat string. This method returns the Timer object so...you figure out how to stop it whenever you want (before the duration has expired).

measuring the time is not working

I want to measure the time between the 1st button click and the 3rd button click. I'm not getting any sort of thext on the main screen, where the textView1 is placed. If i'm launching the app, I'm getting a nullpointer. What does thar mean?
#Override
public void onClick(View v) {
Random r = new Random();
int x = r.nextInt(800);
int y = r.nextInt(800);
long startTime = SystemClock.elapsedRealtime();
i++;
View b = findViewById(R.id.start_time);
b.setX(x);
b.setY(y);
if (i == 1 ) {
b.setX(+9);
b.setY(+5);
}
if (i == 2 ) {
b.setX(x);
b.setY(y);
}
if (i == 3 ) {
b.setX(x);
b.setY(y);
}
else if (i == 4) {
long difference = SystemClock.elapsedRealtime() - startTime;
Intent intent = new Intent(Game.this, MainScreen.class);
intent.putExtra("time",difference);
// Toast.makeText(getApplicationContext(), getIntent().getStringExtra("time"), Toast.LENGTH_LONG).show();
textview1.setText(getIntent().getStringExtra("time"));
finish();
}
}
Well, that function isn't doing what you think. startTime is a local variable and will be cleared every time the function exits. If you want to keep the time between button presses, you need to use a class variable. You would also not want to initialize startTime unless i==1. Right now you're doing it each time and that will cause it to always have a 0 (or very close to 0) difference.
Also why are you using an intent for the toast? At best that's a waste, at worst its a problem. There's no reason for it. Just convert the difference to a string.

how to stop a timer with 'if'

hi guys i am new to java... :(
i just want my button(start) to start a timer but i want to stop the timer automatically with 'if' so for example... when a user enters a time and the timer get's to the timer it stops... so far my coding is like this...
private void startTimerActionPerformed(java.awt.event.ActionEvent evt) {
javax.swing.Timer tm = new javax.swing.Timer(100, new ActionListener()
{
public void actionPerformed (ActionEvent evt) {
AddOneActionPerformed(evt);
}
});
tm.start();
int getTM,getM,getTS,getS,Secs,tenSec,Mins,tenMin;
getTM = Integer.parseInt(enterTenMins.getText());
getM = Integer.parseInt(enterOneMins.getText());
getTS = Integer.parseInt(enterTenSecs.getText());
getS = Integer.parseInt(enterOneSecs.getText());
tenMin = Integer.parseInt(tenMins.getText());
Mins = Integer.parseInt(oneMins.getText());
tenSec = Integer.parseInt(tenSecs.getText());
Secs = Integer.parseInt(oneSecs.getText());
}
and AddOneActionPerformed(evt) is
private void AddOneActionPerformed(java.awt.event.ActionEvent evt) {
int dd,Secs,tenSec,Mins,tenMin;
tenMin = Integer.parseInt(tenMins.getText());
Mins = Integer.parseInt(oneMins.getText());
tenSec = Integer.parseInt(tenSecs.getText());
Secs = Integer.parseInt(oneSecs.getText());
dd= Integer.parseInt(digitValue.getText());
dd= dd+1;
if (dd==10)
dd = 0;
if (Secs == 10)
Secs = 0;
if (dd==0)
Secs=Secs +1;
if (tenSec>=6)
tenSec = 0;
if (Secs==10)
tenSec=tenSec +1;
if (Mins==10)
Mins = 0;
if (tenSec==6)
Mins=Mins+1;
if (tenMin>=6)
tenMin=0;
if (Mins==10)
tenMin=tenMin+1;
String ss = Integer.toString(dd);
digitValue.setText(ss);
String ff = Integer.toString(Secs);
oneSecs.setText(ff);
String gg = Integer.toString(tenSec);
tenSecs.setText(gg);
String hh = Integer.toString(Mins);
oneMins.setText(hh);
String jj = Integer.toString(tenMin);
tenMins.setText(jj);
showDigitActionPerformed(evt);
showOneSecsActionPerformed(evt);
showTenSecsActionPerformed(evt);
showOneMinsActionPerformed(evt);
showTenMinsActionPerformed(evt);
}
You can get the Timer instance from the ActionEvent's getSource() method, and then call stop on it. So,...
// for your Timer's ActionListener
#Override
public void actionPerformed(ActionEvent evt) {
if (someStoppingConditionIsTrue) {
Timer timer = (Timer) evt.getSource();
timer.stop();
} else {
// code to call repeatedly
}
}
Note that you've got two separate issues going on, and should solve them separately. The code above is a way to stop a Timer from within its own ActionListener using some condition. Your other question is how to get user input to allow you to change that condition, and that will involve separate code that is independent of the Timer code above.
Consider in addtion:
Use two JSpinners to for the user's to input minutes and seconds.
Give your class an int totalTime field.
Set this value in your startTimerActionPerformed method.
Have the Timer's ActionListener decrement this value based on measured elapsed time using the differences in calls to System.getSystemTime().
Calculate get the difference between the totalTime and elapsedTime (it will be in milliseconds), say called timeLeft
Calculate your minutes and seconds from timeLeft.
Then display these values in a JLabel using a formatted String, say something like String.format("%02d:%02d", minutes, seconds).
When the timeLeft == 0, that's when you stop your Timer.

Pause Stopwatch Timer with Sleep Thread?

I'm trying to make a stopwatch in Java and don't know how to pause and continue my timer. Here is what I have done so far.
startButton.addActionListener(this);
stopButton.addActionListener(this);
pauseButton.addActionListener(this);
public void actionPerformed(ActionEvent e) {
Calendar aCalendar = Calendar.getInstance();
if (e.getSource() == startButton){
start = aCalendar.getTimeInMillis();
startButton.setBackground(Color.GREEN);
stopButton.setBackground(null);
pauseButton.setBackground(null);
} else if (e.getSource() == stopButton) {
stopButton.setBackground(Color.RED);
startButton.setBackground(null);
pauseButton.setBackground(null);
aJLabel.setText("Elapsed time is: " +
(double) (aCalendar.getTimeInMillis() - start) / 1000 );
} else if (e.getSource() == pauseButton) {
pauseButton.setBackground(Color.YELLOW);
stopButton.setBackground(null);
startButton.setBackground(null);
}
}
As you can see, I've only changed the colors for my pause button. I don't really know how to pause the thread by having the user click on the button. All examples I've found of thread.sleep() were with a specific time.
You can use the swing.Timer (not util.Timer) like this:
int interval = 100; // set milliseconds for each loop
Timer timer = new Timer(interval, (evt) -> repeatingProccess());
// create the method repeatingProccess() with your
// code that makes the clock tick
startButton.addActionListener(e -> timer.start());
stopButton.addActionListener( e -> {
timer.stop();
// here refresh your clock with some code...
};
pauseButton.addActionListener(e -> timer.stop());
You write a method called repeatingProccess() and it works in its own thread again and again every interval milliseconds. For a clock that counts seconds you can do this:
int interval = 1000;
int seconds = 0;
public void repeatingProccess() {
seconds++ ;
}
note:
The second will not be exactly 1000 milliseconds but around 1001 because of the time needed to run seconds++ but you can fix that as well by getting System time before and after and substracting the difference from your clock. You should use the Calendar API for this.

Categories