Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: repaint panel without clearing it

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.
    Last edited by copeg; June 27th, 2010 at 02:44 PM.

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint panel without clearing it

    I Overrided it, I don't know why I put overloaded...

    I have something along the lines of:

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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: repaint panel without clearing it

    Can you make and post a small program that demos the problem?

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

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

Similar Threads

  1. data mapping betweeb different collapsible panel
    By varuntcs in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: May 23rd, 2010, 02:33 PM
  2. Repaint,
    By Time in forum AWT / Java Swing
    Replies: 3
    Last Post: May 21st, 2010, 11:23 PM
  3. suggestionbox inside modal panel
    By smackdown90 in forum Web Frameworks
    Replies: 1
    Last Post: April 8th, 2010, 01:16 PM
  4. Repaint doesn't repaint?
    By PotataChipz in forum AWT / Java Swing
    Replies: 6
    Last Post: January 18th, 2010, 09:56 PM
  5. Replies: 1
    Last Post: October 23rd, 2009, 03:17 PM

Tags for this Thread