aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes timer event handler doesn't receive events and repaint() doesn't consistently call paintComponent() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "timer event handler doesn Watch "timer event handler doesn New topic
Author

timer event handler doesn't receive events and repaint() doesn't consistently call paintComponent()

Suzy Post
Greenhorn

Joined: Oct 17, 2012
Posts: 2
Firstly - also asked this question on stack overflow : http://stackoverflow.com/questions/13442513/timer-event-handler-doesnt-receive-events-and-repaint-doesnt-consistently-ca
Secondly - I have spent several hours on google reading posts with similar problems but nothing seems to work for me.

Thirdly - thank you for reading - I'm just about ready to throw my computer against a wall

My program is supposed to mimic a casino machine and refresh three pictures upon the pull on a lever for about 10 sec until they stop and then get compared to each other.
The architecture of my program is the following:
4 classes:
- 1st class which contains main and extends JFrame and instantiates the other three classes
- 2nd class for the sound - this one works a treat
- 3rd class extends JPanel for the background with the animation, scores etc
- 4th class extends JPanel for the lever (JButton / JLabel - tried both) implements ActionListener / MouseListener

To get the animation to work I am using an array of images which I upload in the constructor. I have used setLayout(null) to be able to place the various other elements and have a timer with 150ms delay and the event listener for the timer which calls repaint();
In my paintComponent function I have randomly select images from my array and use Imagearray[i].paintIcon(this, g, x, y) to paint them to screen. I have a startit method that initializes (the first time) the timer or restarts it if it is already running and a stopit method that just has mytimer.stop(); in it.

The class for the lever has the JButton with an imageicon of a lever and an actionlistener attached to that that is in turn calling a function startGame with the following functions:
- startit();
- startMusic();
- Thread.sleep(5000);
- sets a variable to change how many of the fields get updated
- Thread.sleep(5000);
- sets another variable
- Thread.sleep(5000);
- stopit();
- sets another variable;
- stopMusic();
all of this is in a try / catch block due to the Thread.sleep

When I call startGame() from my class containing main after instantiating the JFrame and JPanels everything works great - the pictures start and stop exactly how I would expect them to, however, when startGame is called from the eventhandler in the lever class, the timer will start / restart (I tested this by printing results of myTimer.isRunning() ) but I never get into the timer Eventhandler.

Trying to re-arrange somewhat I then stopped using the timer and replaced it with a for loop (not elegant, I know, just trying to trouble shoot to see what might be the problem) in which I call repaint() but I never make it into my paintComponent() method while the for loop is iterating (I tested with system.out.println comments). Again, if called from class containing main everything was fine.

I'm at my wit's end why the same method (i.e. startGame() ) would execute perfectly when called upon from my constructor in class containing main but not at all when called from the eventhandler for my button (by the way the sound works so I know that it in theory is stepping through the method...)

Any input would be hugely appreciated!!!

Thanks for reading!




Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
most posts in forums receive replies/answers.
when you post on multiple forums, it means the person replying/answering
on the second forum is totally wasting his/her time.

> Thanks for reading!

nah, didn't bother, bye.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Michael refers to BeForthrightWhenCrossPostingToOtherSites. In this case, http://stackoverflow.com/questions/13442513/timer-event-handler-doesnt-receive-events-and-repaint-doesnt-consistently-ca.

Suzy, you should read Concurrency in Swing. In short, those calls to Thread.sleep cause the EDT to block. Since the timer runs on the EDT it can't get called until the button's click code is executed. By this time I'm guessing you've stopped the timer, which means it doesn't get called at all.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Suzy Post
Greenhorn

Joined: Oct 17, 2012
Posts: 2
Rob Spoor wrote:

Suzy, you should read Concurrency in Swing. In short, those calls to Thread.sleep cause the EDT to block. Since the timer runs on the EDT it can't get called until the button's click code is executed. By this time I'm guessing you've stopped the timer, which means it doesn't get called at all.


Thanks for the explanation - I'm still new to this whole posting in forums thing...

Thank you for the link to the swing tutorial - I have to investigate that further. In the meantime I have replaced the Thread.sleep with just a for-loop (i.e.
for (int i = 0; i < 20; i++)
{

repaint();
for (int k = 0; k < 20000; k++);
System.out.println("in the forloop" + i);
}

and it still doesn't trigger the paintComponent method upon repaint while it steps through the for loop and prints out the statements. :/ I just can't figure out what it is... I was thinking that maybe it's a matter of focus, i.e. the panel with the lever having the focus and therefore the repaint for the other panel not working but I have also tried putting all components in my class with animation to no avail...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

The current method still doesn't end. Instead of using Thread.sleep you've implemented busy waiting. I'd hate to see your CPU usage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: timer event handler doesn't receive events and repaint() doesn't consistently call paintComponent()