0

So I have made a program where I made so a clock appears when running the program. so. When I press a button "Start" a clock should go random inside a bounds and go as a clock aswell (I mean like digital, 15:18:19, 15:18:20). but my problem is now whenever I press the program. the clock stays on the same spot and when changing the sleep time to etc. 500. then it starts to move but the time semms to go alot faster (since it should sleep 1000). however I don't see the problem in the code where it should go. but however maybe some of you could help me with that.

public void startClock() {
  Thread t2 = new Thread() {
   public void run() {
    if (clocking) {
     Random rand = new Random();

     while(clocking){
      Calendar cal = Calendar.getInstance();
      int hour = cal.get(Calendar.HOUR_OF_DAY);
      int minute = cal.get(Calendar.MINUTE);
      int second = cal.get(Calendar.SECOND);
      movingClock.setText(hour + ":" + minute + ":" + second);
      int x = rand.nextInt(100) + 1;
      int y = rand.nextInt(100) + 1;
      movingClock.setBounds(x, y, 150, 150);


      try {
       sleep(1000);
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     }
    } 
   }

  };

  t2.start();
 }

where clocking is = true. because whenever I press the button. I turn it the clocking to true and then run the startClock().

but as I said before. the digital clock only goes randomly if I change the sleep time below 500. and how can I make it work if it's sleep(1000)?

EDIT NEW ONE:

public void startMoving() {
    Thread t1 = new Thread() {
        public void run() {
            if (moving) {
                Random random = new Random();
                while (moving) {
                    int x = random.nextInt(100) + 1;
                    int y = random.nextInt(100) + 1;
                    movingDisplay.setBounds(x, y, 150, 150);

                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } 
        }

    };

    t1.start();
}




public void startClock() {
    Thread t2 = new Thread() {
        public void run() {
            if (clocking) {
                Random rand = new Random();

                while(clocking){

                    Calendar cal = Calendar.getInstance();
                    int hour = cal.get(Calendar.HOUR_OF_DAY);
                    int minute = cal.get(Calendar.MINUTE);
                    int second = cal.get(Calendar.SECOND);
                    movingClock.setText(hour + ":" + minute + ":" + second);
                    int x = rand.nextInt(100) + 1;
                    int y = rand.nextInt(100) + 1;
                    movingClock.setBounds(x, y, 150, 150);

                    java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            movingClock.setText(hour + ":" + minute + ":" + second);
                            movingClock.setBounds(x, y, 150, 150);
                            movingDisplay.setBounds(x, y, 150, 150);
                        }
                    } );


                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            } 
        }

    };

    t2.start();
}
WeInThis
  • 537
  • 2
  • 8
  • 22
  • I don't see anything wrong so, have you tried using 2 Thread.sleep(500); instead of just thread.sleep(1000); – Paul Nov 13 '15 at 12:56
  • Try to give seed to your random. new Random(System.currentTimeMillis()); – newbieee Nov 13 '15 at 12:57
  • 1
    Beware when making a clock and refreshing it every 1000ms. If you get the time say at 14:10:10.999 then you perform some calculations then display something, you will spend a few ms, then you sleep 1000ms then perform some operations, when you get the time the next time its likely to be 14:10:12.005, you will never display 14:10:11 on screen. – StephaneM Nov 13 '15 at 13:12
  • @Paul I havent tried it and will in a second @newbieee Hmm, its only `Random rand = new Random(); @StephaneM ohh well! It only should take the current time. but it doesnt' really matter if its by a second late or early :) – WeInThis Nov 13 '15 at 13:30
  • So I tried both with thread sleep 500 and the random function with without result – WeInThis Nov 13 '15 at 14:47

1 Answers1

0

I guess you are trying to move your clock on some Swing component like JPanel. You should do all the modifications to the components on an Event Dispatch Thread

Your code should look something like this:

java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        movingClock.setText(hour + ":" + minute + ":" + second);
        movingClock.setBounds(x, y, 150, 150);
    }
} );

Look at: Java Event-Dispatching Thread explanation

Community
  • 1
  • 1
Chobicus
  • 2,024
  • 2
  • 17
  • 26
  • Just wonder if its necessary ? I have never workeld with invokeLater before either :O – WeInThis Nov 13 '15 at 13:32
  • It is necessary. All code that modifies the gui should operate on this thread. You can just try it and see if it works :) – Chobicus Nov 13 '15 at 13:51
  • What about the rest of the code? Should I enter there aswell? – WeInThis Nov 13 '15 at 14:12
  • Leave the rest of the code as is. Everything that changes UI should run on EDT. From the code you provided I can only guess that it is setting the text and setting the bounds that changes UI. – Chobicus Nov 13 '15 at 14:14
  • Well that work. I edited the post, could you see it if you meant something like this? – WeInThis Nov 13 '15 at 14:19
  • because I have a another button that should say your name on the other box. If I add this run in the method where I have the clock. like the "left one (my name)" is going faster when both thread is on – WeInThis Nov 13 '15 at 14:33
  • You have a setText line twice in the code. As for another button you are mentioning I don't quite understand how you've set up things. If you are calling startClock() on a click of a button it will go twice as fast if you clicked 2 times (3 times faster if you clicked 3 times etc.) – Chobicus Nov 13 '15 at 14:37
  • But doesnt that mean that it runs on a own Thread and not my own that I created? Well. I updating again the code. see it again :) – WeInThis Nov 13 '15 at 14:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95047/discussion-between-weinthis-and-chobicus). – WeInThis Nov 13 '15 at 14:39
  • The first method is that there is a JLabel text that is moving, which means a name is moving randomly and the other method is the clock. so now if I use the new Runnable. it means it has the same thread if im right? – WeInThis Nov 13 '15 at 14:40