I have a simple Java JFrame canvas. I am updating what is displayed on the screen every half second or so, and is flickering. I want to implement double buffering to eliminate flickering, but I'm pretty new to Java and don't know how to do this. I found some examples, but not sure how to implement their methods in mine.
Below is a basic setup of how I have things now. This is not my exact code - just an example of a basic setup.
Thanks for any push in the right direction!
public class myCanvas extends Canvas{
Color rectColor=Color.red;
public myCanvas()
{
}
public void paint(Graphics graphics)
{
graphics.setColor(rectColor);
graphics.fillRect(X,Y,W,H);
}
public static void main(String[] args)
{
myCanvas canvas = new myCanvas();
JFrame frame = new JFrame("GUI");
frame.setSize(frameWidth,frameHeight);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(canvas);
frame.setVisible(true);
while(true){
rectColor=Color.green;
canvas.validate();
canvas.repaint();
Thread.sleep(500);
}
}
}
source
share