0

I have searched the problem, but everyone seems to have problems with graphics updating too slow.

To present my situation:

I have a JFrame which I set to full screen, by using a compatible display mode. Within the JFrame I have a couple of JPanels and JButtons... On one of JPanels I am drawing moving objects that need to be updated.

I am updating the graphics like this : validate and repaint the JFrame, then revalidate and repaint the corresponding JPanel. The graphics are updating too fast. (I need to mention that on the JPanel I am overriding the paintComponent method). I have tried to use BufferStrategy on the JFrame, however this will prevent me from showing the JPanels and JButtons (have no idea why).

I also take this oppurtunity to ask some of you guys if you can give a clear distinction between paint, validate, repaint, revalidate, invalidate, etc... all the tutorials barely scratch the surface.

  • You only need to revalidate when a component as been added/removed from its parent. It has nothing to do with painting. Please explain what you're trying to do, and show an attempt or at least what you have so far – Vince Apr 27 '14 at 03:30
  • 1
    Can you explain why you think it is updating too fast? – FelixM Apr 27 '14 at 03:31
  • The reason I think it is updating too fast is because if I use a normally defined JFrame (new JFrame, and not a full screen one with a display mode) it works normally (I have some balls moving in a labirint). The code looks like this : void rollBalls(){ f.validate(); f.repaint(); updateBallsSpecs(); p.revalidate(); p.repaint();} f is the JFrame and p is the JPanel within the JFrame. The update specs method simply updates the specification of the balls (coordinates in an array), coordinates which I use in the paintComponent method within the JPanel class. – user3577465 Apr 27 '14 at 03:40
  • What kind of timing mechanism are you using for your animation? Please show some code – Paul Samsotha Apr 27 '14 at 03:55
  • The timing mechanism is simply a loop that runs for 5 minutes using System.timeCurrentMillis to count that. – user3577465 Apr 27 '14 at 04:01

1 Answers1

0

"The timing mechanism is simply a loop that runs for 5 minutes using System.timeCurrentMillis to count that"

That's not going to work. The loop is ultimately blocking the painting to occur until the loop is complete, if there is no delay.

I suggest you look into using a javax.swing.Timer for the animation. You can see more at How to Use Swing Timers

The basic construct of Timer is as follows

Timer ( int delayInMillis, ActionListener listener )

where delayInMillis is the time in milliseconds to delay between "ticks" and the listener provides the actionPerformed which is called every delayInMillis milliseconds. So ultimately you do something like

Timer timer = new Timer (40, new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        for (Ball ball: balls) {
            ball.move();
            repaint();
        }
    }
});

You can see a complete example here

enter image description here

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720