I can see from Google searches that this question gets asked a lot, but none of the solutions I find are doing it for me. I'm making a game in Java with images, you know, since games generally have that stuff. But the entire form is constantly flickering and I can't get it to stop. Yes, I have double buffered it and overridden the update() method, and while that definitely helped, the flickering still persists. I don't know if I'm doing something wrong with the double buffering, or if I need something completely different.
At first I thought maybe it had something to do with the clearRect() line, but upon removing it, the game still flickered but of course wasn't cleared each time. So that didn't help at all. Upon slowing down the timer the flickering does go away almost entirely, but I need to slow it down to 100ms and even then I still get some flicker. Besides, that is simply too slow for the game. I tried having one timer doing everything on a 10ms timer, with a separate one at 100ms for painting, but it just looks jumpy then. I can slow the painting timer down to about 30ms and have it still be smooth, though the flickering is still an issue.
There must be a way of doing this every 10-30ms without flickering. Is there some other method similar to double buffering but better in this case, or something I can use? Thanks in advance for any help.
public class main extends JApplet implements ActionListener {
//This Declares The Variables
Graphics2D buffer;
Image offscreen;
Timer timGame = new Timer(10, this);
//other variables
public void init(){
//This Creates The Offscreen Buffer Zone
offscreen = createImage(getSize().width, getSize().height);
buffer = (Graphics2D)offscreen.getGraphics();
//other initialization stuff irrelevant to drawing
}
public void actionPerformed(ActionEvent evt){
if (evt.getSource() == timGame)
runGame();
}
private void runGame(){
//Do stuff, move objects
repaint();
}
public void paint(Graphics g){
super.paint(g);
buffer.clearRect(0, 0, getSize().width, getSize().height);
//draw stuff to buffer
g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics gr){
paint(gr);
}