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: Animation and double buffering

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Animation and double buffering

    I have a class for which I am not happy with the animation. I've simplified it to a simple square moving accross the screen. My problem is that the screen doesn't clear and so instead of a moving box, I get a thick line. I've tried a few attempts to fix this below. I've read about double-buffering and have tried to implement this, but something is missing. Can you help point out my issue?

    Original class (a moving box is a line):
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
     
    public class RightPane extends JApplet implements Runnable {
      int frame;
      Thread  animator;
     
      public void start() {
        frame = 0;
        animator = new Thread(this);
        animator.start();
      }
     
      public void run() {
        long tm = System.currentTimeMillis();
        while(Thread.currentThread() == animator) {
          repaint();
     
          try {
            tm += 50; // 50 ms iteration rate
            Thread.sleep(Math.max(0, tm - System.currentTimeMillis() ) );
          } 
          catch (InterruptedException e) {  break;  }
     
          frame++;
     
          if (frame == 100) // Freeze the animation here
            this.stop();
        }
      }
     
      public void stop()  {
        animator = null;
      }
     
      public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        Dimension  d  = this.getSize();
     
        g2.setPaint( Color.red );
        g2.fill ( new Rectangle2D.Double( d.width*frame/100, d.height*frame/100, 10, 10) );
      }
    }

    This was my first attempt to fix this. It does help, but causes significant flashing:
      public void paint(Graphics g) {
     
        super.paint(g); // Adding this line will cause flashing, 
                        //    but at least everything clears
     
        Graphics2D g2 = (Graphics2D) g;
        Dimension  d  = this.getSize();
     
        g2.setPaint( Color.red );
        g2.fill ( new Rectangle2D.Double( d.width*frame/100, d.height*frame/100, 10, 10) );
      }

    Implementing the update() function was my second attempt to fix this. This is how I read to implement double-buffering. Unfortunately, it has no effect and I'm not sure what's wrong:
      private Image dbImage;
      private Graphics dbg;
     
      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);
      }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Animation and double buffering

    Don't override the paint() method, ever. Read this article on Custom Painting.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    stewbond (October 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Animation and double buffering

    Ok so I read this article. The only difference that I see between what I did and what they did was that they used paintComponent() instead of paint(). I've tried to over-ride this instead, but now there is no painting performed.

    Another difference is that they have inherited from JPanel instead of JApplet. I've tried that too in an attempt to get paintComponent() to be used, but I'm having some conflicts with the Runnable implementation. The output looks like so:

    stew@Romulus ~/Course/Week4-DQ2 $ javac *.java
    MainFrame.java:24: error: cannot find symbol
    rightPanel.start();
    ^
    symbol: method start()
    location: variable rightPanel of type JPanel
    1 error

    I'll keep trying to figure out another way to do this but what is the reason why the AWT paint() isn't appropriate?

    --- Update ---

    Update:
    I've changed the type of object of the instantiation from JPanel to RightPane, now I'm getting runtime errors with messages like this:
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at RightPane.paintComponent(RightPane.java:58)
    at javax.swing.JComponent.paint(JComponent.java:1054)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Animation and double buffering

    That looks like a recursion problem: a method that is calling itself.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    stewbond (October 26th, 2013)

  7. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Animation and double buffering

    Are you calling repaint() in the paintComponent() method? If so, that will set up an infinite loop. I was hoping you'd get those details from the Custom Painting Lesson.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    stewbond (October 26th, 2013)

  9. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Animation and double buffering

    Got it,

    In paintComponent() I was still calling super.paint(). Calling super.paintComponent() fixed this.

    Thanks for your awesome help. The program is 100% now.

    --- Update ---

    Total changes:

    RightPane extends JPanel instead of JApplet.

    The parent class instantiates a RightPane object instead of JApplet right = new RightPane()

    Called paintComponent instead of paint()

    Called super.paintComponent() at the start of paintComponent().

Similar Threads

  1. Problems with Double Buffering -- BufferStrategy
    By hobbles in forum AWT / Java Swing
    Replies: 9
    Last Post: April 26th, 2013, 11:39 AM
  2. [SOLVED] Double Buffering Problem
    By beansnbacon in forum What's Wrong With My Code?
    Replies: 16
    Last Post: March 30th, 2013, 09:24 PM
  3. Double Buffering in game programming
    By WaffleZombie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2012, 03:26 PM
  4. Help With Double Buffering/Animations
    By bgroenks96 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 18th, 2011, 06:58 PM
  5. Double Buffering
    By Ganezan in forum Java Theory & Questions
    Replies: 2
    Last Post: November 20th, 2009, 03:51 AM

Tags for this Thread