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...

[2647 byte] By [MichaelArnautsa] at [2007-11-26 23:33:04]
# 1

i dont know if this will work but try putting this code in ur class and put private Graphics dbg;

at the top

public void update(Graphics g)

{

if (dbImage == null)

{

dbImage = createImage (this.getSize().width, this.getSize().height);

dbg = dbImage.getGraphics ();

}

dbg.setColor (getBackground ());

dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

dbg.setColor (getForeground());

paint (dbg);

g.drawImage (dbImage, 0, 0, this);

}

stephensk8sa at 2007-7-11 14:52:05 > top of Java-index,Other Topics,Java Game Development...
# 2
Read through this tutorial on custom painting: http://java.sun.com/docs/books/tutorial/uiswing/painting/index.htmlYou should be overriding the paintComponent method, not the paint method.
hunter9000a at 2007-7-11 14:52:05 > top of Java-index,Other Topics,Java Game Development...