Why does the Java Swing timer get slower and slower?

Creid Maestro

New member
I'm writing a game in Java using Eclipse on a Mac OS X.

I have programmed a Java Swing timer so that every time it ticks (at a rate of 50 ms), it calls an event that does some calculations, and then adds a component to a JFrame:

myframe.getContentPane().add(new DrawPanel());

I have a seperate DrawPanel class that extends JPanel, and has the following method:

public void paintComponent(Graphics g)
{ g.fillRect(x, y, width, height); }

This all works as intended, however as the program continues to run, the speed at which the Java Swing Timer ticks gets slower and slower. The only thing that I can think of is changing over time is the fact that I'm adding an insane number of panels to the JFrame which is slowing it down. I have tried throwing a myframe.removeAll(); in there every 100 ticks or so, but it doesn't work - it doesn't remove all of the components.

Can anyone offer any kind of help? This is my graduation project, and it's due Thursday....
 
Back
Top