repaint panel without clearing it
I'm currently making a 2D game with sprite animation. The problem is that I only want to redraw the character sprites and not redraw the background sprites on every repaint.
Due to the backgrounds being large and randomly generated from several sprites, it takes a lot of time to constantly redraw the background.
How can I accomplish this? I've tried overloading the update function but the background still gets cleared.
Thanks for any help.
Re: repaint panel without clearing it
The doc say that overriding update() should avoid clearing the background.
Did you override or overload? Do you use the @Override directive to have the compiler check it?
Can you make a small program that demos the problem?
Re: repaint panel without clearing it
Some alternative to not clearing the background would be 1) try having your sprites as animated GIF's (do not know how java handles gif animation, eg if it repaints the whole panel or what - easy to test) 2) do the sprite animation in separate JPanels which are positioned, perhaps via OverlayLayout or (gasp) null layout. This way you can dispatch the painting to only the sprites needed to be repainted 3) depending upon how things are being drawn, you could draw to a BufferedImage, then draw the image in the panel rather than performing the drawing in the EDT methods. You could for instance, create an object Sprite, that has a BufferedImage, painting method, and position values. Then in the paintComponent method just loop through all Sprites and draw their image objects, and the changes to the image objects are dealt with when needed and not redrawn for every repaint.
Re: repaint panel without clearing it
I Overrided it, I don't know why I put overloaded...
I have something along the lines of:
Code :
@Override
public void update(Graphics g) {
paint(g);
}
@Override
public void paint(Graphics g) {
//code to draw background
// code to draw characters etc.
}
And that's basically it. I read online that overriding the update function should do it, but it's not quite working.
Re: repaint panel without clearing it
Can you make and post a small program that demos the problem?
Re: repaint panel without clearing it
Keep a copy of the game panel inside a buffered image. Then, every-time you want to change the frame, paint only the changes to the buffered image. Then all you have to do is over-ride the update method to not clear, and change the paint method to only paint that buffered image.
Note: You can do this without using a buffered image, but you will be left with some portions of the screen being updated before others since Java can draw one image a lot faster than trying to process and draw multiple sprites. This method also prevents long paint methods which will cause your game to run slower (may or may not be that big of a deal).
Something like this:
Code Java:
public class GamePanel extends JApplet
{
BufferedImage image;
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
// draw the buffered image
g.drawImage(image, 0, 0, null);
}
// method that will update the animation sprites
public void updateImage()
{
// TODO: this is where you draw stuff to the buffered image
}
public void clearBuffer()
{
// clearing the buffered image is as simple as creating a new buffered image.
image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
}
}