My first double buffer / game loop. In Java
OK i''m just trying to create a gameloop with double buffering, which will load a 640x480 image as background. which i have done. however, it flashes periodically and when you move the mouse about. i think paint is drawing to the screen without copying from the buffer but i dont know how to fix it. any help anyone? ps when i have sorted this out i will switch it to use proper buffers and that
Game.java
________________________________________________________
package game;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
public class Game implements Runnable {
private Image backBuffer, background;
private Graphics offscr;
private int width, height = 0;
private long time, lastFrameTime = 0;
private Thread runThread;
private Frame drawFrame;
/**Game state */
boolean gameRunning = true;
public Game() {
// this is where it looks for the 640x480 gif file...
background = Toolkit.getDefaultToolkit().getImage("c:/game/back.gif");
if (background == null) {System.out.println("isNull");}
drawFrame = new Frame();
drawFrame.setSize(640,480);
width = drawFrame.getSize().width;
height = drawFrame.getSize().height;
drawFrame.setVisible(true);
/**Set backbuffer*/
backBuffer = drawFrame.createImage(width, height);
offscr = backBuffer.getGraphics();
runThread = new Thread(this);
runThread.start();
}
public void paint(Graphics g) {
g.drawImage(backBuffer, 0, 0, this.drawFrame);
}
public void run() {
System.out.println("entering main loop...");
while (runThread != null) {
/**Check time and update*/
lastFrameTime = System.currentTimeMillis() - time;
time = System.currentTimeMillis();
//get Input
//update game world
//draw to buffer
drawObjects();
drawFrame.getGraphics().drawImage(backBuffer, 0, 0, this.drawFrame);
//draw to screen
drawFrame.repaint();
//limit fps??
try {
runThread.sleep(5);
}
catch (InterruptedException e) { ;
}
}
}
public void drawObjects(){
//load background
offscr.drawRect(10,10,10,10);
offscr.drawImage(background, 0,0,Color.black,this.drawFrame);
offscr.drawRect(30,10,10,10);
}
public void update(Graphics g) {
}
public static void main(String[] args){
Game game = new Game();
}
}
I would guess you need to overload update(); then that calls paint(); you don''t need to call repaint(); If I recall calling repaint does lead to some ugly tearing and junk.
1) Your drawing logic is a bit of a mess, you shouldn''t be calling repaint for a start and you''re not double buffered efficiently either. Grab this file for some sample code. It''s a test program i did for double buffering/active rendering amongst other things and should give you a few pointers.
2) Check out javagaming.org forums as well, theres tons of info there...
2) Check out javagaming.org forums as well, theres tons of info there...
update() is called by the system when the frame needs to be repainted, so if you "intercept" that it stops all the weird effects. Its kinda wierd but its like update calls repaint calls paint, Kapeesh?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement