Animation Thread - Paint is only called once
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?
// MainFrame.java
import java.awt.Container;
import javax.swing.*;
public class MainFrame extends JFrame
{
MainPanel mp;
public MainFrame ()
{
this.setSize (800, 800);
mp = new MainPanel ();
Container container = this.getContentPane ();
container.add (mp);
this.setVisible (true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String args[])
{
MainFrame mf = new MainFrame ();
}
}
// MainPanel.java
import java.awt.*;
import java.util.logging.*;
import javax.swing.*;
public class MainPanel extends JPanel
{
Panel1 panel1;
Panel2 panel2;
public MainPanel ()
{
panel1 = new Panel1 (this);
panel2 = new Panel2 (this);
this.setLayout (new GridLayout (2, 1));
this.add (panel1);
this.add (panel2);
}
public void RandomAnimation ()
{
for (int i = 0; i < 25; i++)
{
try
{
Thread.sleep(50);
panel1.repaint();
}
catch (InterruptedException ex)
{
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
// Panel1.java
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class Panel1 extends JPanel
{
Random random;
MainPanel mp;
public Panel1 (MainPanel mainpan)
{
random = new Random ();
mp = mainpan;
}
@Override
public void paintComponent (Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = random.nextInt (200) + 100;
int y = random.nextInt (200) + 100;
System.out.println ("Inside Panel1 paintComponent function.");
g2.setColor (Color.RED);
g2.fillOval (x, y, 50, 50);
}
}
// Panel2.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Panel2 extends JPanel implements ActionListener
{
JButton animateButton;
MainPanel mp;
public Panel2 (MainPanel mainpan)
{
mp = mainpan;
animateButton = new JButton ("Animate");
animateButton.addActionListener (this);
this.add (animateButton);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource () == animateButton)
{
mp.RandomAnimation ();
}
}
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
out of curiosity, what is generally your "fix" to this issue? (it may provide some clues)
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
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.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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...
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
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.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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 :(
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
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?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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.
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
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
new Thread(
new Runnable() {
public void run() {
mp.RandomAnimation ();
}
}
).start();
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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
new Thread(
new Runnable() {
public void run() {
mp.RandomAnimation ();
}
}
).start();
Awesome! Worked like a charm. Sillyboy, thanks for the link.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Just to provide a little more explanation why that works and why you had the previous difficulties, you were executing your animation loop code that slept and called repaint() on the event dispatch thread - which is the same thread that is responsible for processing those repaint() calls you were sending. By moving the animation code to it's own thread, you free up the event dispatch thread to respond to the repaints() that you are requesting.
You can read more about concurrency concerns in Swing here if you like: http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Just to provide a little more explanation why that works and why you had the previous difficulties, you were executing your animation loop code that slept and called repaint() on the event dispatch thread - which is the same thread that is responsible for processing those repaint() calls you were sending. By moving the animation code to it's own thread, you free up the event dispatch thread to respond to the repaints() that you are requesting.
You can read more about concurrency concerns in Swing here if you like: http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
Thanks. That helps.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711