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]

# 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
# 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);
}
}