Hey guys,
I am making a 2D game using SWING/paintComponent and have run in to an FPS problem. My FPS is fine at runs at 60 when I run it through Netbeans, though if I run a jar file then it will drop to 30. Would I be better off using AWT/Canvas?
I have cross-posted this problem. The original thread is http://www.java-forums.org/new-java/81086-fps-dropping-exactly-half-when-running-game-through-jar-file.html , but I have been advised to seek help from java-gaming instead.
Thanks for any help
- It only happens in fullscreen exclusive mode, and does not happen when windowed.
- Double buffering has no affect on it.
- Turning VYsnc off in my NVIDIA control panel will fix the problem and it will run at over 320fps.
If I run this exact code through Netbeans, it does exactly what I need…
- It takes 15ms from the time repaint() gets called, till paintComponent has finished.
- paintComponent takes 8ms of that 15ms time.
- Paintcomponent will be called 60 times per second.
If I run this exact code from a jar file, the FPS drops by half
- It takes 30ms from the time repaint() gets called, till paintComponent has finished.
- paintComponent takes 15ms of that 30ms time.
- Paintcomponent will be called 30times per second.
public void gameLoop(){
double startMS;
double currentMS;
while (true){
startMS = System.currentTimeMillis();
currentMS = System.currentTimeMillis() - startMS ;
while (currentMS < 1000){ // Display FPS after 1 second has passed
if (gameScreen.getIsPainting() == false){
render();
}
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
Logger.getLogger(GameEngine.class.getName()).log(Level.SEVERE, null, ex);
}
currentMS = System.currentTimeMillis() - startMS ;
}
if (SHOW_FPS == true){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
gameScreen.getTfFPS().setText(String.valueOf(GameScreen.paintingAmount));
gameScreen.revalidate();
GameScreen.paintingAmount = 0; // Reset painting counter to get amount of times painting every second
}
});
}
}
}
public void render(){
gameScreen.setIsPainting(true);
gameScreen.repaint();
}
private void init(){
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
gameScreen = new GameScreen(this);
gameEngine.init();
input = new Input();
input.initInput();
frame.add(input);
frame.add(gameScreen);
frame.setVisible(true);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
DisplayMode dm = new DisplayMode(640,480,32,60);
gd.setFullScreenWindow(frame);
gd.setDisplayMode(dm);
}