Animation Thread - Paint is only called once

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Animation Thread - Paint is only called once

 
0
  #1
Feb 24th, 2009
I have a program that is supposed to draw circles every twentieth of a second every time a button is clicked. When the button is pressed, a function is called and a for-loop is executed 25 times. Within that for-loop, repaint () should be called. Within the paintComponent function, random coordinates are produced and a red circle is drawn. However, the paintComponent function appears to NOT be called 25 times, but only once. I've run into this problem many times in the past and I've always eventually "fixed" it, but I've never really understood what's going on. Any ideas?

  1. // MainFrame.java
  2. import java.awt.Container;
  3. import javax.swing.*;
  4.  
  5.  
  6. public class MainFrame extends JFrame
  7. {
  8. MainPanel mp;
  9.  
  10.  
  11. public MainFrame ()
  12. {
  13. this.setSize (800, 800);
  14. mp = new MainPanel ();
  15. Container container = this.getContentPane ();
  16. container.add (mp);
  17. this.setVisible (true);
  18. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. }
  20.  
  21.  
  22. public static void main (String args[])
  23. {
  24. MainFrame mf = new MainFrame ();
  25. }
  26. }

  1. // MainPanel.java
  2. import java.awt.*;
  3. import java.util.logging.*;
  4. import javax.swing.*;
  5.  
  6.  
  7. public class MainPanel extends JPanel
  8. {
  9. Panel1 panel1;
  10. Panel2 panel2;
  11.  
  12. public MainPanel ()
  13. {
  14. panel1 = new Panel1 (this);
  15. panel2 = new Panel2 (this);
  16.  
  17. this.setLayout (new GridLayout (2, 1));
  18. this.add (panel1);
  19. this.add (panel2);
  20. }
  21.  
  22.  
  23. public void RandomAnimation ()
  24. {
  25. for (int i = 0; i < 25; i++)
  26. {
  27. try
  28. {
  29. Thread.sleep(50);
  30. panel1.repaint();
  31. }
  32. catch (InterruptedException ex)
  33. {
  34. Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
  35. }
  36. }
  37. }
  38. }

  1. // Panel1.java
  2. import java.awt.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5.  
  6.  
  7. public class Panel1 extends JPanel
  8. {
  9. Random random;
  10. MainPanel mp;
  11.  
  12.  
  13. public Panel1 (MainPanel mainpan)
  14. {
  15. random = new Random ();
  16. mp = mainpan;
  17. }
  18.  
  19.  
  20. @Override
  21. public void paintComponent (Graphics g)
  22. {
  23. super.paintComponent(g);
  24. Graphics2D g2 = (Graphics2D) g;
  25. g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  26.  
  27. int x = random.nextInt (200) + 100;
  28. int y = random.nextInt (200) + 100;
  29.  
  30. System.out.println ("Inside Panel1 paintComponent function.");
  31.  
  32. g2.setColor (Color.RED);
  33. g2.fillOval (x, y, 50, 50);
  34. }
  35. }

  1. // Panel2.java
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.*;
  5.  
  6.  
  7. public class Panel2 extends JPanel implements ActionListener
  8. {
  9. JButton animateButton;
  10. MainPanel mp;
  11.  
  12.  
  13. public Panel2 (MainPanel mainpan)
  14. {
  15. mp = mainpan;
  16. animateButton = new JButton ("Animate");
  17. animateButton.addActionListener (this);
  18. this.add (animateButton);
  19. }
  20.  
  21.  
  22. public void actionPerformed(ActionEvent e)
  23. {
  24. if (e.getSource () == animateButton)
  25. {
  26. mp.RandomAnimation ();
  27. }
  28. }
  29. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Animation Thread - Paint is only called once

 
0
  #2
Feb 24th, 2009
out of curiosity, what is generally your "fix" to this issue? (it may provide some clues)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Animation Thread - Paint is only called once

 
0
  #3
Feb 25th, 2009
Originally Posted by sillyboy View Post
out of curiosity, what is generally your "fix" to this issue? (it may provide some clues)
I often end up using global variables for my loop counter, then setting up a Swing Timer which, when it fires, calls a function that does whatever was inside the loop, then calls repaint. That seems to work, but it seems overkill and I'd like to use a loop inside of a local function if I can, i think.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Animation Thread - Paint is only called once

 
0
  #4
Feb 25th, 2009
yeah, it seems strange. have you tried putting a System out directly within the for loop?
the only things i can think of, are an InterruptException (but you should get a log of this), or repaint not calling paintComponent properly...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Animation Thread - Paint is only called once

 
0
  #5
Feb 25th, 2009
Originally Posted by sillyboy View Post
yeah, it seems strange. have you tried putting a System out directly within the for loop?
the only things i can think of, are an InterruptException (but you should get a log of this), or repaint not calling paintComponent properly...
Debugging statements seem to show that it goes through the loop itself 25 times, but only calls paintComponent once at the end. I don't see it throwing any exceptions.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Animation Thread - Paint is only called once

 
0
  #6
Feb 25th, 2009
so then I guess repaint doesn't seem to be triggering paintComponent properly. perhaps you could start googling since you have something to base a search off... i personally don't have anything else to offer on this
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Animation Thread - Paint is only called once

 
0
  #7
Feb 25th, 2009
Originally Posted by sillyboy View Post
so then I guess repaint doesn't seem to be triggering paintComponent properly. perhaps you could start googling since you have something to base a search off... i personally don't have anything else to offer on this
Thanks. Anyone else?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Animation Thread - Paint is only called once

 
1
  #8
Feb 25th, 2009
this link may help you: http://forums.java.net/jive/thread.jspa?threadID=42506

there is also a link at the bottom of the page to a sun reference. one of my guesses would be the fact that repaint() doesn't guarantee an immediate execution, and so it may only call repaint() once at the end...

anyway, good luck.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Animation Thread - Paint is only called once

 
2
  #9
Feb 25th, 2009
This will work the way you want if you put your loop in a different thread from the Swing event thread. Replace your call
mp.RandomAnimation (); in actionPerformed
with
  1. new Thread(
  2. new Runnable() {
  3. public void run() {
  4. mp.RandomAnimation ();
  5. }
  6. }
  7. ).start();
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Animation Thread - Paint is only called once

 
0
  #10
Feb 25th, 2009
Originally Posted by JamesCherrill View Post
This will work the way you want if you put your loop in a different thread from the Swing event thread. Replace your call
mp.RandomAnimation (); in actionPerformed
with
  1. new Thread(
  2. new Runnable() {
  3. public void run() {
  4. mp.RandomAnimation ();
  5. }
  6. }
  7. ).start();
Awesome! Worked like a charm. Sillyboy, thanks for the link.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC