943,691 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2359
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 24th, 2009
0

Animation Thread - Paint is only called once

Expand Post »
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?

Java Syntax (Toggle Plain Text)
  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. }

Java Syntax (Toggle Plain Text)
  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. }

Java Syntax (Toggle Plain Text)
  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. }

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Feb 24th, 2009
0

Re: Animation Thread - Paint is only called once

out of curiosity, what is generally your "fix" to this issue? (it may provide some clues)
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 25th, 2009
0

Re: Animation Thread - Paint is only called once

Click to Expand / Collapse  Quote originally posted by sillyboy ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Feb 25th, 2009
0

Re: Animation Thread - Paint is only called once

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...
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 25th, 2009
0

Re: Animation Thread - Paint is only called once

Click to Expand / Collapse  Quote originally posted by sillyboy ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Feb 25th, 2009
0

Re: Animation Thread - Paint is only called once

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
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 25th, 2009
0

Re: Animation Thread - Paint is only called once

Click to Expand / Collapse  Quote originally posted by sillyboy ...
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?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Feb 25th, 2009
0

Re: Animation Thread - Paint is only called once

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.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 25th, 2009
2

Re: Animation Thread - Paint is only called once

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
Java Syntax (Toggle Plain Text)
  1. new Thread(
  2. new Runnable() {
  3. public void run() {
  4. mp.RandomAnimation ();
  5. }
  6. }
  7. ).start();
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,767 posts
since Apr 2008
Feb 25th, 2009
0

Re: Animation Thread - Paint is only called once

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
Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Project help
Next Thread in Java Forum Timeline: Help needed to complete the Java method test() in below program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC