I am trying to increase performance with buffered images. I have made a game loop that has increased my fps but i also want buffered images, however these aren't working.
its supposed to show a black background and print the amount of ticks and fps in the console
package game3;
import java.awt.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.image.*;
import java.util.logging.*;
import java.util.logging.*;
import javax.swing.*;
/**,
*
* @author djb
*/
public class Game extends Canvas implements Runnable{
private boolean running = false;
private Thread thread;
private BufferedImage image = new BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);
private synchronized void start()
{
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop()
{
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(1);
}
public void run()
{ //game loop
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(running)
{
//game loop
long now = System.nanoTime();
delta+=(now - lastTime)/ns;
lastTime = now;
if(delta >= 1)
{
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
System.out.println(updates + "ticks, Fps " + frames);
updates = 0;
frames = 0;
}
}
stop();
}
private void tick()
{
}
private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//put objects between here and dispose to draw stuff
g.drawImage(image, 0, 0, 630, 480, this);
g.dispose();
bs.show();
}
public static void main(String[]args)
{
Game game = new Game();
JFrame frame = new JFrame();
frame.pack();
frame.setSize(640,480);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.start();
}
}
could really use some help, thanks