Java calls repaint to much (100% CPU)
Hello,
I'm creating an emulator, and the image to display is created with an ImageProducer. Now, I have this code:
publicclass Videoextends JPanel{
privateint a;
private Image screen;
private Lcd lcd;
private Dimension lcdDimension;
privateint scale;
privateint scaleFactor = 3;
/** Creates a new instance of Video */
public Video(){
lcd =new Lcd(64, 32);
lcdDimension = lcd.getDimension();
screen = createImage(lcd);
setScale(2);
}
publicvoid paint(Graphics g){
System.out.println(a++);
screen.flush();
g.drawImage(screen, 0, 0, lcdDimension.width*scale*scaleFactor, lcdDimension.height*scale*scaleFactor, Color.WHITE,this);
}
public Dimension getPreferredSize(){
returnnew Dimension(lcdDimension.width*scale*scaleFactor, lcdDimension.height*scale*scaleFactor);
}
public Lcd getLcd(){return lcd;}
publicvoid setScale(int s){ scale = s;}
publicint getScale(){return scale;}
}
The problem is, as soon as I open the menu and close it (I don't click on anything, no code should be executed), java starts repainting the Video frame like mad...
I can see the number count go up on the standard output with about 200 / second...

