I'm trying to make a stopwatch that constantly updates to the screen what the time is. The time object is a JLabel that should display the time elapsed. I have everything imported and set up right, but whenever the start button is clicked to activate the timer, the program freezes.
public class Timing
{
public void Timer()
{
startTime = System.currentTimeMillis();
while(isTiming == true)
{
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
time.setText("" + elapsedTime);
info.validate();
}
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
time.setText("" + elapsedTime);
info.validate();
}
}
Here's the Action Listener part:
public void actionPerformed(ActionEvent arg0)
{
if(((JButton) arg0.getSource()).getText().equals("Start"))
{
isTiming = true;
new Timing().Timer();
}
You are trying to synchronously update your label in a loop in the UI thread, the same thread which is used for applying UI updates. By doing this you are blocking the execution which results into the freeze you are describing.
To resolve this, you could use the javax.swing.Timer to schedule the updates so that they would be run in the same UI thread, but only when needed, i.e.
timer = new javax.swing.Timer(10, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
...
time.setText("" + elapsedTime);;
}
});
...
public void actionPerformed(ActionEvent arg0)
{
if(((JButton) arg0.getSource()).getText().equals("Start"))
{
isTiming = true;
timer.start();
}
}
Related
I'm trying to close a SWT application after certain period of inactive time. I'm finding difficulty in calculating the inactivity period.All that I've tried just shuts the application after the specified amount of time, irrespective of whether I'm still working on the Application.
Below is the code of what I've tried
Timer timer = new Timer();
display.asyncExec(new Runnable(){
public void run() {
while(!shell.isDisposed()){
if (!display.readAndDispatch()){ //if the system is idle
timer.schedule(new TimerTask() { //schedule timer which will trigger after 1min.
#Override
public void run() {
logout();
}
}, 60000);
}
else{ // reschedule the timer
timer.cancel();
Timer timer = new Timer();
}
}
}
});
with this I'm just able to logout after 1 min even if the user is performing something. Is there a way we can check the inactivity time in SWT ?
You can use Display.addFilter to listen for various events that make the app 'active'. Use Display.timerExec to run code in the UI thread repeatedly to check the time since the last event.
Maybe something like:
private Instant lastActive;
....
lastActive = Instant.now();
final Listener listener = new Listener()
{
#Override
public void handleEvent(final Event event)
{
lastActive = Instant.now();
}
};
display.addFilter(SWT.MouseMove, listener);
display.addFilter(SWT.KeyDown, listener);
// TODO more filters if required
final Runnable timer = new Runnable()
{
#Override
public void run()
{
final Instant now = Instant.now();
if (ChronoUnit.SECONDS.between(lastActive, now) > xxx) { // Number of inactive seconds here
System.out.println("inactive"); // Inactive code here
}
else {
display.timerExec(1000, this);
}
}
};
display.timerExec(1000, timer);
... create/open shell ...
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
I want to print the time between two buttons clicks (ButtonA and ButtonB). This is part of my code (which doesn't work because only the current time is printed):
class ClicksReporter implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
long startTime = 0;
long endTime = 0;
if ( e.getActionCommand().equals("ButtonA") ) {
System.out.println("ButtonA was clicked");
startTime = System.currentTimeMillis()/1000;
}
if ( e.getActionCommand().equals("ButtonB") ) {
System.out.println("ButtonB was clicked");
endTime = System.currentTimeMillis()/1000 - startTime;
System.out.println("Time: "+endTime);
}
}
How can I solve this?
startTime and endTime are local variables that are lost once the method returns. So, every time ButtonB is clicked, the value of startTime is zero. You need to make start time a field of the class instead.So, something like this:
class ClicksReporter implements ActionListener {
long startTime=0;
#Override
public void actionPerformed(ActionEvent e) {
if ( e.getActionCommand().equals("ButtonA") ) {
System.out.println("ButtonA was clicked");
this.startTime = System.currentTimeMillis()/1000;
}
if ( e.getActionCommand().equals("ButtonB") ) {
System.out.println("ButtonB was clicked");
long endTime = System.currentTimeMillis()/1000 - startTime;
System.out.println("Time: "+endTime);
}
}
Also, make sure that the same instance of ClicksReporter is set as the action listener for both button A and B.
I want to add possibility to pause/resume to my game. Unfortunately my solution only pauses the game, but does not resume it (when I click resume button nothing happens - the image is still). I also tried to call wait/notify methods on thread, but it also did not work - I got Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException. What is the best solution to make pause/resume?
Game Loop
public void run() {
init();
long startTime;
long reTime;
long waitTime;
while (running) {
startTime = System.nanoTime();
int CarPosX = car.zwrocPolozenieX();
int CarPosY = car.zwrocPolozenieY();
update(b, CarPosX, CarPosY);
render(b);
//draw();
repaint();
if(b<4390) {
b=b+szybkoscMapy;
}
reTime = System.nanoTime() - startTime;
waitTime = targetTime - reTime/100000000;
try {
Thread.sleep(waitTime);
}
catch(Exception e) {
}
}
}
public void init() {
if(thread == null) {
thread = new Thread(this);
thread.start();
}
b=0;
running = true;
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
map = new Map(levelNumber);
car = new Car(map);
car.setxpos(338);
car.setypos(150);
}
Listeners
pause_button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bp.running = false;
}
});
resume_button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bp.running = true;
}
});
Use javax.swing.Timer to pace the animation. Typical controls that invoke the timer's start() and stop() methods are examined here. This fleet simulation, which uses such a Timer, may also be if interest.
I've created GUI timer, it runs exactly how I wanted it to. I have a stop and pause button, when I stop or pause the timer and restart a new one I get Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Task already scheduled or cancelled
I'm unsure what I'm suppose to do I've read you cant reuse tasks, but I have no clue to solve this. Can someone PLEASE help me out I'm going crazy over this, I always seem to fix one problem but another one pops up.
Heres part of my code which does the countdown
private TimerTask task = new TimerTask(){
#Override
public void run(){
if (countdown()) {
if(minutes < 9 && seconds < 9)
timerOutput.setText("0"+minutes + ": 0" + seconds);
else if(minutes < 9)
timerOutput.setText("0"+minutes + ":" + seconds);
else if(seconds < 9)
timerOutput.setText(minutes + ": 0" + seconds);
}
else
{
System.out.println("Finish!");
timerOutput.setText("Time is up!");
timer.cancel();
startBut.setEnabled(true);
}
}
};
private boolean countdown(){
seconds --;
if (seconds < 0){
minutes--;
seconds = 59;
if (minutes == -1){
return false;
}
}
return true;
}
Well TimerTasks aren't designed to be reused. The best you can do is create a new TimerTask every time you're going to reschedule it.
Although you can't simply restart a Timer, you could create a Timer wrapper class which would act exactly like a Timer but allow a simple restart method that would instantiate a new Timer in the background. For example;
public class RestartableTimer{
private Timer timer;
private long delay, period;
public RestartableTimer(){
timer = new Timer();
}
public void scheduleAtFixedRate(TimerTask task, long delay, long period){
this.delay = delay;
this.period = period;
timer.scheduleAtFixedRate(task, delay, period);
}
public void restart(TimerTask task){
timer.cancel();
timer = new Timer();
timer.scheduleAtFixedRate(task, delay, period);
}
}
A fair warning, this would not allow for polymorphism. You couldn't for example store a RestartableTimer in a Timer reference. You will also still need to instantiate a new TimerTask when you restart. If you wanted (and you knew you would only be reusing the same TimerTask), you could declare a custom and private embedded class in the above and let the wrapper class handle the creation of the new TimerTask. Alternatively you could have the class methods take a TimerTaskFactory which would implement an Interface which required a method that returned a TimerTask.
Below, an example of using the above class;
public static void main(String[] args) throws InterruptedException{
TimerTask task = new TimerTask(){
#Override
public void run() {
System.out.println("Running");
}};
RestartableTimer rt = new RestartableTimer();
System.out.println("Timer starting with one task");
rt.scheduleAtFixedRate(task, 1000, 1000);
Thread.sleep(5000);
System.out.println("Timer restarting with another task");
rt.restart(new TimerTask(){
int count = 0;
#Override
public void run() {
if(count>4) {
System.out.println("Done");
this.cancel();
} else {
System.out.println("Running 2");
count++;
}
}});
}
I created a game and in my swing GUI interface I want to put a timer. The way I do this at the moment is have a field with the current time , gotten with System.currentTimeMillis() which gets it's value when the game starts .In the method of my game i put the System.currentTimeMillis()- field; and it tells you the current time passed since the game started.
Nevertheless, how do get this to update itself every second lets say, so the JLabel will have : timePassed: 0s , timePassed: 1s and so on. Have in mind that i don't use threads in my game at any point.
EDIT: thank you all for your kind suggestions. I used a combination of your answers please give me some feedback.
I have the JLabel as a field called time. (else i cant handle it).
time = new JLabel("Time Passed: " + timePassed() + " sec");
panel_4.add(time);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
time.setText("Time Passed: " + timePassed() + " sec");
}
};
Timer timer = new Timer(1000, actionListener);
timer.start();
Have a look at the swing timer class. It allows to setup recurring tasks quite easily.
This is how I would set my JLabel to update with time & date.
Timer SimpleTimer = new Timer(1000, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
jLabel1.setText(SimpleDay.format(new Date()));
jLabel2.setText(SimpleDate.format(new Date()));
jLabel3.setText(SimpleTime.format(new Date()));
}
});
SimpleTimer.start();
This is then added to your main class and the jlabel1/2/3 get updated with the timer.
new Thread(new Runnable
{
public void run()
{
long start = System.currentTimeMillis();
while (true)
{
long time = System.currentTimeMillis() - start;
int seconds = time / 1000;
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
label.setText("Time Passed: " + seconds);
}
});
try { Thread.sleep(100); } catch(Exception e) {}
}
}
}).start();
wirite this in Constructor
ActionListener taskPerformer = new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
jMenu11.setText(CurrentTime());
}
};
Timer t = new Timer(1000, taskPerformer);
t.start();
And this Write out Constructor
public String CurrentTime(){
Calendar cal = new GregorianCalendar();
int second = cal.get(Calendar.SECOND);
int min = cal.get(Calendar.MINUTE);
int hour = cal.get(Calendar.HOUR);
String s=(checkTime(hour)+":"+checkTime(min)+":"+checkTime(second));
jMenu11.setText(s);
return s;
}
public String checkTime(int t){
String time1;
if (t < 10){
time1 = ("0"+t);
}
else{
time1 = (""+t);
}
return time1;
}