Reputation: 57
I have been practicing with java's swing features recently, and in one of my classes that extends the class JPanel, I have overriden the method paintComponent()
so that it will paint my BufferedImage onto the JPanel. I also have a method on it to move around. Before this issue, I have had a problem that shows the process of moving as it repaints too quickly. So, I created a boolean variable called available
which is set to false when the image is still in the moving process. But, now I see that the screen is taking away the entire image and putting it back, causing it to blink. Here is my basic pseudocode:
class A extends JPanel{
BufferedImage canvas;
public A(){
//create image here
}
public move(){
available = false;
//move things around in here
available = true;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(available){
g.drawImage(this.canvas, 0, 0, null);
}
g.dispose();
}
}
class B{
public static void main(String[] args){
//construct the class A JPanel
while(some_variable){
class_A_JPanel.repaint();
}
}
}
Upvotes: 2
Views: 110
Reputation: 719
This is very old topic which is fixed in modern Java. But you prefer old way then use old techniques. For example Double Buffering
Upvotes: 1