| | |
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:
Solved Threads: 501
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)
// 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 (); } }
Java Syntax (Toggle Plain Text)
// 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); } } } }
Java Syntax (Toggle Plain Text)
// 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); } }
Java Syntax (Toggle Plain Text)
// 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 (); } } }
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
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.
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...
the only things i can think of, are an InterruptException (but you should get a log of this), or repaint not calling paintComponent properly...
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
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.
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
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.
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.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
This will work the way you want if you put your loop in a different thread from the Swing event thread. Replace your call
with
mp.RandomAnimation (); in actionPerformedwith
Java Syntax (Toggle Plain Text)
new Thread( new Runnable() { public void run() { mp.RandomAnimation (); } } ).start();
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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)
new Thread( new Runnable() { public void run() { mp.RandomAnimation (); } } ).start();
![]() |
Similar Threads
- Dealing with animation and components? (isolating the two while drawing) (Java)
- Probably very easy for you to answer (Java)
- Button Question/Issue (Java)
- Image Array Issue..... Any ideas??? (Java)
- more game trouble- need high score system (Java)
Other Threads in the Java Forum
- Previous Thread: Project help
- Next Thread: Help needed to complete the Java method test() in below program
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






