strange fps

hi i’m a newbie in this java 2D things.

i have a problem like this.
i create a Java 2D simple simulation which will animate objects around the arena.
when i run the application, the frame rate seems to be capped at around 40-50 fps.
and the weirdest thing is that sometimes the fps can grow rapidly on some frames (e.g. 2000, 6000, or even greater fps!!!)
can anyone please help me to solve this problem?
is it something to do with my application or is it because of the Java implementation?

thx for the help

FPS does fluctuate, but not in the values that you describe. So, I must assume it is your code. Could you post it, perhaps?

i’ve tried using GAGE as many people have pointed it out.
the fps seems not to “explode” again.
however there are still problems.
the fps will never get above 50 fps even though i cap the redraw 100 fps.
another thing is that as time goes by, the frame rate seems to decrease until 20-30 fps and never increases again.
this is very frustating.

my code is just a simple one.

while(run)
{
redraw();
simulatorCalculation();
}

i implement the double buffering myself. is it possible that this is the source of the problem? also i use 1020x750 window size (i’m not using fullscreen), plus i also render the background plus the object (so far only 1 object) every time i redraw.
what is the possible cause of the problem?

my code is quite big since i have many classes around.
if you want to inspect it, maybe i could send to u the zip files.

thanks for your help

the main:

public void runSimulator()
{
if(VISUAL_ON && FPS_ON)
{
at = new AdvancedTimer();
tick = 0;
at.start();
ticksPerFrame = AdvancedTimer.getTicksPerSecond() / DESIRED_FPS;
lastTime = System.currentTimeMillis();
frameNo = 0;
//startTime = System.currentTimeMillis();
}

while(run)
{

if(VISUAL_ON && FPS_ON)
{
//elapsedTime = System.currentTimeMillis();
if(System.currentTimeMillis() - lastTime > 1000)
{
lastTime += 1000;
displayFrameRate(frameNo);
frameNo = 0;
}
frameNo++;
}
if(VISUAL_ON)
{
repaint();
//pause(1);
}
// Update the objects position for the next 1 second (the simulation time cycle).
updateObjectsPosition(1);
fieldCollisions();

if(VISUAL_ON && FPS_ON)
{
at.sleepUntil(tick + ticksPerFrame);
tick += ticksPerFrame;
}
}
}