Repaint causing 100% CPU usage

I'ts been suggested that I link to my topic in new to java about a problem I'm having with 100% cpu usage, that I think is due to calling repaint.

Can someone help me out here - this ahs been driving me crazy for hours!

http://forum.java.sun.com/thread.jspa?threadID=5144804&tstart=0

[308 byte] By [nealbo101a] at [2007-11-26 20:40:40]
# 1
Can you repost all the relevant code?
CaptainMorgan08a at 2007-7-10 1:59:04 > top of Java-index,Other Topics,Java Game Development...
# 2

public void paint(Graphics g)

{

offscreen = createImage(IMAGE_WIDTH, IMAGE_HEIGHT);

bufferGraphics = offscreen.getGraphics();

Graphics2D g2 = (Graphics2D) g;

bufferGraphics2 = (Graphics2D) bufferGraphics;

g2.setColor(Color.WHITE);

bufferGraphics2.setColor(Color.WHITE);

bufferGraphics2.drawRect(0, 0, 500, 500);

if(Plane.planeList.size()!=0)

{

//loop through all planes

for(Plane s: Plane.planeList)

{

imageCenterX = (int)(s.getX() + (s.getWidth() / 2));

imageCenterY = (int)(s.getY() + (s.getHeight() / 2));

lineOneEndX = imageCenterX + IMAGE_RADIUS;

lineOneEndY = imageCenterY - IMAGE_RADIUS;

lineTwoStartX = lineOneEndX;

lineTwoStartY = lineOneEndY;

lineTwoEndX = lineOneEndX + LINE_TWO_LENGTH;

lineTwoEndY = lineOneEndY;

stringX = lineTwoStartX + STRING_LEFT_PADDING;

stringY = lineTwoStartY + STRING_BOTTOM_PADDING;

bufferGraphics2.drawLine(imageCenterX, imageCenterY, lineOneEndX, lineOneEndY);

bufferGraphics2.drawLine(lineTwoStartX, lineTwoStartY, lineTwoEndX, lineTwoEndY);

bufferGraphics2.drawString(s.getName(), stringX, stringY);

bufferGraphics2.drawImage(s.getImage(), (int)s.getX(), (int)s.getY(), null);

}

bufferGraphics2.drawImage(overlay, 0, 0, null);

g2.drawImage(offscreen, 0, 0, null);

}

}

The above is in my MyCanvas class

public PlaneMovementUpdater(MyCanvas c)

{

Timer t = new javax.swing.Timer(100, this);

t.start();

canvas = c;

}

public void actionPerformed(ActionEvent e)

{

if(Plane.planeList.size() != 0)

{

for(Plane p: Plane.planeList)

{

if(p.getAction().equals("fly"))

{

p.movePlane();

//canvas.repaint();

}

}

canvas.repaint();

}

}

The above is in my PlaneUpdater class

Also, I ran a java profiler and 98.4% of the CPU is being used by MyCanvas.paint

Other info on this, which I don't understand is 20,652 ms - 195 inv

Also it says MyCanvas,update uses 98.4% and java.awt.EventDispatchThread.run is using 100%

Any help on whats going on would be great

Message was edited by:

nealbo101

nealbo101a at 2007-7-10 1:59:04 > top of Java-index,Other Topics,Java Game Development...
# 3

Whoa!

offscreen = createImage(IMAGE_WIDTH, IMAGE_HEIGHT);

Don't do this every time paint() is called! Create your offscreen buffer in init() and just refer to it in paint()...

Ideally all paint() should do is draw stuff - no game logic, no loads, creates &c. just what must be there...

Simon__1sta at 2007-7-10 1:59:04 > top of Java-index,Other Topics,Java Game Development...
# 4

> Whoa!

>

> offscreen = createImage(IMAGE_WIDTH,

> IMAGE_HEIGHT);

> Don't do this every time paint() is called! Create

> your offscreen buffer in init() and just refer to it

> in paint()...

> Ideally all paint() should do is draw stuff - no game

> logic, no loads, creates &c. just what must be

> there...

good catch!

Clem1986a at 2007-7-10 1:59:04 > top of Java-index,Other Topics,Java Game Development...
# 5
> > Whoa!...> > good catch!Indeed. I completely looked over that.
CaptainMorgan08a at 2007-7-10 1:59:04 > top of Java-index,Other Topics,Java Game Development...
# 6
Thanks for the suggestion but moving that line into my constructor or public void init() causes a null pointer exception, in fact it causes a never ending loop of null pointer exceptions - any ideas as to why that is?
nealbo101a at 2007-7-10 1:59:04 > top of Java-index,Other Topics,Java Game Development...
# 7
> any ideas as to why that is?Post your new, updated code.
CaptainMorgan08a at 2007-7-10 1:59:04 > top of Java-index,Other Topics,Java Game Development...
# 8

Try this: Declare the buffer as global to the class - ie just above the constructor;

private Image offscreen=null;

then in paint();

public void paint(Graphics g)

{

if (offscreen==null)

{

offscreen = createImage(IMAGE_WIDTH, IMAGE_HEIGHT);

}

bufferGraphics = offscreen.getGraphics();

&c...

Simon__1sta at 2007-7-10 1:59:04 > top of Java-index,Other Topics,Java Game Development...
# 9

Thanks, that solved my 100% problem!

Unfortunately now when the canvas is redrawn the previous things drawn on it remain. i.e. I have an image drawn, its moved and repainted, but the previous image painted remains causing it to look like the image is being smeared across the screen - why is that happening?

public class MyCanvas extends Canvas

{

private int imageCenterX;

private int imageCenterY;

private int lineOneEndX;

private int lineOneEndY;

private int lineTwoStartX;

private int lineTwoStartY;

private int lineTwoEndX;

private int lineTwoEndY;

private int stringX;

private int stringY;

static final int IMAGE_HEIGHT = 500;

static final int IMAGE_WIDTH = 500;

static final int LINE_TWO_LENGTH = 50;

static final int IMAGE_RADIUS = 10;

static final int STRING_LEFT_PADDING = 5;

static final int STRING_BOTTOM_PADDING = -2;

static final int FONT_SIZE = 10;

//Used to create Timer that updates

PlaneMovementUpdater listener = new PlaneMovementUpdater(this);

Graphics bufferGraphics;

Graphics2D bufferGraphics2;

Image overlay;

Toolkit toolkit;

private Image offscreen = null;

public MyCanvas(int width, int height)

{

setSize(width, height);

setFont(new Font("sansserif", Font.ITALIC, FONT_SIZE));

toolkit = Toolkit.getDefaultToolkit();

overlay = toolkit.getImage("c:/overlay.gif");

//toolkit.prepareImage(overlay, -1, -1, null); //preloads

setBackground(Color.BLACK);

//offscreen = createImage(IMAGE_WIDTH, IMAGE_HEIGHT);

//bufferGraphics = offscreen.getGraphics();

}

public void paint(Graphics g)

{

if (offscreen==null)

{

offscreen = createImage(IMAGE_WIDTH, IMAGE_HEIGHT);

bufferGraphics = offscreen.getGraphics();

}

//offscreen = createImage(IMAGE_WIDTH, IMAGE_HEIGHT);

//bufferGraphics = offscreen.getGraphics();

Graphics2D g2 = (Graphics2D) g;

bufferGraphics2 = (Graphics2D) bufferGraphics;

g2.setColor(Color.WHITE);

bufferGraphics2.setColor(Color.WHITE);

bufferGraphics2.drawRect(0, 0, 500, 500);

if(Plane.planeList.size()!=0)

{

//loop through all planes

for(Plane s: Plane.planeList)

{

imageCenterX = (int)(s.getX() + (s.getWidth() / 2));

imageCenterY = (int)(s.getY() + (s.getHeight() / 2));

lineOneEndX = imageCenterX + IMAGE_RADIUS;

lineOneEndY = imageCenterY - IMAGE_RADIUS;

lineTwoStartX = lineOneEndX;

lineTwoStartY = lineOneEndY;

lineTwoEndX = lineOneEndX + LINE_TWO_LENGTH;

lineTwoEndY = lineOneEndY;

stringX = lineTwoStartX + STRING_LEFT_PADDING;

stringY = lineTwoStartY + STRING_BOTTOM_PADDING;

bufferGraphics2.drawLine(imageCenterX, imageCenterY, lineOneEndX, lineOneEndY);

bufferGraphics2.drawLine(lineTwoStartX, lineTwoStartY, lineTwoEndX, lineTwoEndY);

bufferGraphics2.drawString(s.getName(), stringX, stringY);

bufferGraphics2.drawImage(s.getImage(), (int)s.getX(), (int)s.getY(), null);

}

bufferGraphics2.drawImage(overlay, 0, 0, null);

g2.drawImage(offscreen, 0, 0, null);

}

}

public void update(Graphics g)

{

paint(g);

}

}

nealbo101a at 2007-7-10 1:59:04 > top of Java-index,Other Topics,Java Game Development...
# 10
Got it sorted - changed drawRect to clearRect :)
nealbo101a at 2007-7-10 1:59:05 > top of Java-index,Other Topics,Java Game Development...