private void animationLoop() {
		while (Thread.currentThread() == animationThread) {
			try {
				double frameStartTime = System.nanoTime();
				animateNextFrame();
				double currentFrameTime = System.nanoTime() - frameStartTime;
				int timeToNextFrame = (int) ((timePerFrame - currentFrameTime) / 1000000);
				if (timeToNextFrame > 0) {
					// Adjust frame rate to minimize sleep time
					if (fps < MAX_FRAMERATE && timeToNextFrame > 1) {
						setFrameRate(calculateOptimalFrameRate(currentFrameTime));
					}
					Thread.sleep(timeToNextFrame);
				} else if (fps > MIN_FRAMERATE) {
					// Inadequate time left, decrease the frame rate
					setFrameRate(calculateOptimalFrameRate(currentFrameTime));
				}
			} catch (InterruptedException e) {
			} catch (IllegalStateException e) {
			}
		}
	}