Using Timer To Pause Program Execution [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1
















This question already has an answer here:




  • Repaint is not functioning properly as required

    1 answer




I want to pause the execution of a Swing Program for a specified amount of time. Naturally the first thing that I used was Thread.sleep(100) (since, I am a noob). Then I got to know that my program is not thread safe so I decided to use Timer with some suggestions from fellow programmers. The problem is I am unable to get any sources from where I can learn how to delay the thread, using Timer. Most of them use Timer for delaying execution. Please help me solve this problem. I have provided a compileable code snippet below.



import javax.swing.*;
import java.awt.*;

public class MatrixBoard_swing extends JFrame{

public static void main(String args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MatrixBoard_swing b = new MatrixBoard_swing();
}
});
}

MatrixBoard_swing(){
this.setSize(640, 480);
this.setVisible(true);
while(rad < 200){
repaint();
rad++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

int rad = 10;

public void paint(Graphics g){
super.paint(g);
g.drawOval(400-rad, 400-rad, rad, rad);
}

}


EDIT: My trial for a Timer implementation(please tell me if it is wrong):



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MatrixBoard_swing extends JFrame implements ActionListener{

Timer timer;

public static void main(String args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MatrixBoard_swing b = new MatrixBoard_swing();
}
});
}

MatrixBoard_swing(){
this.setSize(640, 480);
this.setVisible(true);
timer = new Timer(100, this);
timer.start();
}

int rad = 10;

public void paint(Graphics g){
super.paint(g);
g.drawOval(400-rad, 400-rad, rad, rad);
}

@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
rad++;
if(rad >= 200){
timer.stop();
}
}









share|improve this question















marked as duplicate by Hovercraft Full Of Eels, Joeri Hendrickx, crockeea, Clockwork-Muse, Pshemo Apr 23 '14 at 14:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    You're still calling Thread.sleep(...) on the Swing event thread. I thought that we had this ironed out in your previous similar question. What gives?

    – Hovercraft Full Of Eels
    Feb 20 '14 at 5:23








  • 4





    "Using Timer To Pause Program Execution" Pause what exactly, the GUI rendering? The GUI doing some long running operation? Allowing user input? Note that while(rad < 200){ ... Thread.sleep(100); indicates a common, classic mistake with rendering animation in a GUI.

    – Andrew Thompson
    Feb 20 '14 at 5:24











  • I am unable to understand how to use the Timer. You had advised me to provide a simple, compileable code. So I posted it here.

    – Crystal Meth
    Feb 20 '14 at 5:24











  • @AndrewThompson: he's already been told all of this.

    – Hovercraft Full Of Eels
    Feb 20 '14 at 5:24











  • @AndrewThompson: Yes, I want to pause the GUI rendering. I know what you are saying. I just want an example as to how.

    – Crystal Meth
    Feb 20 '14 at 5:25




















1
















This question already has an answer here:




  • Repaint is not functioning properly as required

    1 answer




I want to pause the execution of a Swing Program for a specified amount of time. Naturally the first thing that I used was Thread.sleep(100) (since, I am a noob). Then I got to know that my program is not thread safe so I decided to use Timer with some suggestions from fellow programmers. The problem is I am unable to get any sources from where I can learn how to delay the thread, using Timer. Most of them use Timer for delaying execution. Please help me solve this problem. I have provided a compileable code snippet below.



import javax.swing.*;
import java.awt.*;

public class MatrixBoard_swing extends JFrame{

public static void main(String args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MatrixBoard_swing b = new MatrixBoard_swing();
}
});
}

MatrixBoard_swing(){
this.setSize(640, 480);
this.setVisible(true);
while(rad < 200){
repaint();
rad++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

int rad = 10;

public void paint(Graphics g){
super.paint(g);
g.drawOval(400-rad, 400-rad, rad, rad);
}

}


EDIT: My trial for a Timer implementation(please tell me if it is wrong):



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MatrixBoard_swing extends JFrame implements ActionListener{

Timer timer;

public static void main(String args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MatrixBoard_swing b = new MatrixBoard_swing();
}
});
}

MatrixBoard_swing(){
this.setSize(640, 480);
this.setVisible(true);
timer = new Timer(100, this);
timer.start();
}

int rad = 10;

public void paint(Graphics g){
super.paint(g);
g.drawOval(400-rad, 400-rad, rad, rad);
}

@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
rad++;
if(rad >= 200){
timer.stop();
}
}









share|improve this question















marked as duplicate by Hovercraft Full Of Eels, Joeri Hendrickx, crockeea, Clockwork-Muse, Pshemo Apr 23 '14 at 14:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    You're still calling Thread.sleep(...) on the Swing event thread. I thought that we had this ironed out in your previous similar question. What gives?

    – Hovercraft Full Of Eels
    Feb 20 '14 at 5:23








  • 4





    "Using Timer To Pause Program Execution" Pause what exactly, the GUI rendering? The GUI doing some long running operation? Allowing user input? Note that while(rad < 200){ ... Thread.sleep(100); indicates a common, classic mistake with rendering animation in a GUI.

    – Andrew Thompson
    Feb 20 '14 at 5:24











  • I am unable to understand how to use the Timer. You had advised me to provide a simple, compileable code. So I posted it here.

    – Crystal Meth
    Feb 20 '14 at 5:24











  • @AndrewThompson: he's already been told all of this.

    – Hovercraft Full Of Eels
    Feb 20 '14 at 5:24











  • @AndrewThompson: Yes, I want to pause the GUI rendering. I know what you are saying. I just want an example as to how.

    – Crystal Meth
    Feb 20 '14 at 5:25
















1












1








1


1







This question already has an answer here:




  • Repaint is not functioning properly as required

    1 answer




I want to pause the execution of a Swing Program for a specified amount of time. Naturally the first thing that I used was Thread.sleep(100) (since, I am a noob). Then I got to know that my program is not thread safe so I decided to use Timer with some suggestions from fellow programmers. The problem is I am unable to get any sources from where I can learn how to delay the thread, using Timer. Most of them use Timer for delaying execution. Please help me solve this problem. I have provided a compileable code snippet below.



import javax.swing.*;
import java.awt.*;

public class MatrixBoard_swing extends JFrame{

public static void main(String args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MatrixBoard_swing b = new MatrixBoard_swing();
}
});
}

MatrixBoard_swing(){
this.setSize(640, 480);
this.setVisible(true);
while(rad < 200){
repaint();
rad++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

int rad = 10;

public void paint(Graphics g){
super.paint(g);
g.drawOval(400-rad, 400-rad, rad, rad);
}

}


EDIT: My trial for a Timer implementation(please tell me if it is wrong):



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MatrixBoard_swing extends JFrame implements ActionListener{

Timer timer;

public static void main(String args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MatrixBoard_swing b = new MatrixBoard_swing();
}
});
}

MatrixBoard_swing(){
this.setSize(640, 480);
this.setVisible(true);
timer = new Timer(100, this);
timer.start();
}

int rad = 10;

public void paint(Graphics g){
super.paint(g);
g.drawOval(400-rad, 400-rad, rad, rad);
}

@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
rad++;
if(rad >= 200){
timer.stop();
}
}









share|improve this question

















This question already has an answer here:




  • Repaint is not functioning properly as required

    1 answer




I want to pause the execution of a Swing Program for a specified amount of time. Naturally the first thing that I used was Thread.sleep(100) (since, I am a noob). Then I got to know that my program is not thread safe so I decided to use Timer with some suggestions from fellow programmers. The problem is I am unable to get any sources from where I can learn how to delay the thread, using Timer. Most of them use Timer for delaying execution. Please help me solve this problem. I have provided a compileable code snippet below.



import javax.swing.*;
import java.awt.*;

public class MatrixBoard_swing extends JFrame{

public static void main(String args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MatrixBoard_swing b = new MatrixBoard_swing();
}
});
}

MatrixBoard_swing(){
this.setSize(640, 480);
this.setVisible(true);
while(rad < 200){
repaint();
rad++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

int rad = 10;

public void paint(Graphics g){
super.paint(g);
g.drawOval(400-rad, 400-rad, rad, rad);
}

}


EDIT: My trial for a Timer implementation(please tell me if it is wrong):



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MatrixBoard_swing extends JFrame implements ActionListener{

Timer timer;

public static void main(String args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MatrixBoard_swing b = new MatrixBoard_swing();
}
});
}

MatrixBoard_swing(){
this.setSize(640, 480);
this.setVisible(true);
timer = new Timer(100, this);
timer.start();
}

int rad = 10;

public void paint(Graphics g){
super.paint(g);
g.drawOval(400-rad, 400-rad, rad, rad);
}

@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
rad++;
if(rad >= 200){
timer.stop();
}
}




This question already has an answer here:




  • Repaint is not functioning properly as required

    1 answer








java swing user-interface timer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 20 '14 at 5:33







Crystal Meth

















asked Feb 20 '14 at 5:21









Crystal MethCrystal Meth

423415




423415




marked as duplicate by Hovercraft Full Of Eels, Joeri Hendrickx, crockeea, Clockwork-Muse, Pshemo Apr 23 '14 at 14:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Hovercraft Full Of Eels, Joeri Hendrickx, crockeea, Clockwork-Muse, Pshemo Apr 23 '14 at 14:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    You're still calling Thread.sleep(...) on the Swing event thread. I thought that we had this ironed out in your previous similar question. What gives?

    – Hovercraft Full Of Eels
    Feb 20 '14 at 5:23








  • 4





    "Using Timer To Pause Program Execution" Pause what exactly, the GUI rendering? The GUI doing some long running operation? Allowing user input? Note that while(rad < 200){ ... Thread.sleep(100); indicates a common, classic mistake with rendering animation in a GUI.

    – Andrew Thompson
    Feb 20 '14 at 5:24











  • I am unable to understand how to use the Timer. You had advised me to provide a simple, compileable code. So I posted it here.

    – Crystal Meth
    Feb 20 '14 at 5:24











  • @AndrewThompson: he's already been told all of this.

    – Hovercraft Full Of Eels
    Feb 20 '14 at 5:24











  • @AndrewThompson: Yes, I want to pause the GUI rendering. I know what you are saying. I just want an example as to how.

    – Crystal Meth
    Feb 20 '14 at 5:25
















  • 1





    You're still calling Thread.sleep(...) on the Swing event thread. I thought that we had this ironed out in your previous similar question. What gives?

    – Hovercraft Full Of Eels
    Feb 20 '14 at 5:23








  • 4





    "Using Timer To Pause Program Execution" Pause what exactly, the GUI rendering? The GUI doing some long running operation? Allowing user input? Note that while(rad < 200){ ... Thread.sleep(100); indicates a common, classic mistake with rendering animation in a GUI.

    – Andrew Thompson
    Feb 20 '14 at 5:24











  • I am unable to understand how to use the Timer. You had advised me to provide a simple, compileable code. So I posted it here.

    – Crystal Meth
    Feb 20 '14 at 5:24











  • @AndrewThompson: he's already been told all of this.

    – Hovercraft Full Of Eels
    Feb 20 '14 at 5:24











  • @AndrewThompson: Yes, I want to pause the GUI rendering. I know what you are saying. I just want an example as to how.

    – Crystal Meth
    Feb 20 '14 at 5:25










1




1





You're still calling Thread.sleep(...) on the Swing event thread. I thought that we had this ironed out in your previous similar question. What gives?

– Hovercraft Full Of Eels
Feb 20 '14 at 5:23







You're still calling Thread.sleep(...) on the Swing event thread. I thought that we had this ironed out in your previous similar question. What gives?

– Hovercraft Full Of Eels
Feb 20 '14 at 5:23






4




4





"Using Timer To Pause Program Execution" Pause what exactly, the GUI rendering? The GUI doing some long running operation? Allowing user input? Note that while(rad < 200){ ... Thread.sleep(100); indicates a common, classic mistake with rendering animation in a GUI.

– Andrew Thompson
Feb 20 '14 at 5:24





"Using Timer To Pause Program Execution" Pause what exactly, the GUI rendering? The GUI doing some long running operation? Allowing user input? Note that while(rad < 200){ ... Thread.sleep(100); indicates a common, classic mistake with rendering animation in a GUI.

– Andrew Thompson
Feb 20 '14 at 5:24













I am unable to understand how to use the Timer. You had advised me to provide a simple, compileable code. So I posted it here.

– Crystal Meth
Feb 20 '14 at 5:24





I am unable to understand how to use the Timer. You had advised me to provide a simple, compileable code. So I posted it here.

– Crystal Meth
Feb 20 '14 at 5:24













@AndrewThompson: he's already been told all of this.

– Hovercraft Full Of Eels
Feb 20 '14 at 5:24





@AndrewThompson: he's already been told all of this.

– Hovercraft Full Of Eels
Feb 20 '14 at 5:24













@AndrewThompson: Yes, I want to pause the GUI rendering. I know what you are saying. I just want an example as to how.

– Crystal Meth
Feb 20 '14 at 5:25







@AndrewThompson: Yes, I want to pause the GUI rendering. I know what you are saying. I just want an example as to how.

– Crystal Meth
Feb 20 '14 at 5:25














1 Answer
1






active

oldest

votes


















2














So instead of...



while(rad < 200){
repaint();
rad++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


You simply need to turn the logic around a little...



Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rad++;
if (rad < 200) {
repaint();
} else {
((Timer)evt.getSource()).stop();
}
}
});
timer.start();


Basically, the Timer will act as the Thread.sleep(), but in a nice way that doesn't break the UI, but will allow you to inject a delay between execution. Each time it executes, you need to increment your value, test for the "stop" condition and update otherwise...



Take a look at How to Use Swing Timers and the other 3, 800 questions on the subject on SO...






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    So instead of...



    while(rad < 200){
    repaint();
    rad++;
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }


    You simply need to turn the logic around a little...



    Timer timer = new Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    rad++;
    if (rad < 200) {
    repaint();
    } else {
    ((Timer)evt.getSource()).stop();
    }
    }
    });
    timer.start();


    Basically, the Timer will act as the Thread.sleep(), but in a nice way that doesn't break the UI, but will allow you to inject a delay between execution. Each time it executes, you need to increment your value, test for the "stop" condition and update otherwise...



    Take a look at How to Use Swing Timers and the other 3, 800 questions on the subject on SO...






    share|improve this answer




























      2














      So instead of...



      while(rad < 200){
      repaint();
      rad++;
      try {
      Thread.sleep(100);
      } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }


      You simply need to turn the logic around a little...



      Timer timer = new Timer(1000, new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      rad++;
      if (rad < 200) {
      repaint();
      } else {
      ((Timer)evt.getSource()).stop();
      }
      }
      });
      timer.start();


      Basically, the Timer will act as the Thread.sleep(), but in a nice way that doesn't break the UI, but will allow you to inject a delay between execution. Each time it executes, you need to increment your value, test for the "stop" condition and update otherwise...



      Take a look at How to Use Swing Timers and the other 3, 800 questions on the subject on SO...






      share|improve this answer


























        2












        2








        2







        So instead of...



        while(rad < 200){
        repaint();
        rad++;
        try {
        Thread.sleep(100);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        }


        You simply need to turn the logic around a little...



        Timer timer = new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
        rad++;
        if (rad < 200) {
        repaint();
        } else {
        ((Timer)evt.getSource()).stop();
        }
        }
        });
        timer.start();


        Basically, the Timer will act as the Thread.sleep(), but in a nice way that doesn't break the UI, but will allow you to inject a delay between execution. Each time it executes, you need to increment your value, test for the "stop" condition and update otherwise...



        Take a look at How to Use Swing Timers and the other 3, 800 questions on the subject on SO...






        share|improve this answer













        So instead of...



        while(rad < 200){
        repaint();
        rad++;
        try {
        Thread.sleep(100);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        }


        You simply need to turn the logic around a little...



        Timer timer = new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
        rad++;
        if (rad < 200) {
        repaint();
        } else {
        ((Timer)evt.getSource()).stop();
        }
        }
        });
        timer.start();


        Basically, the Timer will act as the Thread.sleep(), but in a nice way that doesn't break the UI, but will allow you to inject a delay between execution. Each time it executes, you need to increment your value, test for the "stop" condition and update otherwise...



        Take a look at How to Use Swing Timers and the other 3, 800 questions on the subject on SO...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 20 '14 at 5:38









        MadProgrammerMadProgrammer

        303k17156275




        303k17156275

















            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()