Hey guys I have been having lots of problems getting my animation to not flicker. I have read that double buffering is a good way to stop the flickering but everything I have tried doesn't do anything to help. If you could please help me understand what I am doing wrong it would really appreciate it. Here is my code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Bouncing extends JFrame implements ActionListener{
  ImageIcon ball;
  Timer clock;
  int x, y;
  int dx, dy;
  Graphics dbg;
  Image dbImage;
  Image bdImage;
  
  public static void main(String args[]){
	  new Bouncing();
  } // end main
  
  public Bouncing(){
	  super("Bouncing Ball");
	  //load up the image
	  ball = new ImageIcon("ball.gif");
	  clock = new Timer(50, this);
	  dx = 5;
	  dy = 3;
	  
	  this.setSize(400, 400);
	  this.setVisible(true);
	  this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
	  
	  clock.start();
  } // end constructor
  
  public void paint(Graphics g) {
	  super.paint(g);
	  
	  g.drawImage(ball.getImage(), x, y, null);
	  
  } // end paint
  
  public void update(Graphics g){
	// initialize buffer
	  if (dbImage == null)
	  {
	  dbImage = createImage (this.getSize().width, this.getSize().height);
	  dbg = dbImage.getGraphics ();
	  }

	  // clear screen in background
	  dbg.setColor (getBackground ());
	  dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

	  // draw elements in background
	  dbg.setColor (getForeground());
	  paint (dbg);

	  // draw image on the screen
	  g.drawImage (dbImage, 0, 0, this);

	  
  }
  
  public void actionPerformed(ActionEvent e){
	  //happens once each 50 ms
	  x += dx;
	  y += dy;
	  
	  checkBounds();
	  
      repaint();
  } // end actionPerformed
  
  
  public void checkBounds(){
	  if (x > this.getWidth() - ball.getIconWidth()){
		  dx *= -1;
	  } // end if
	  if (x < 0){
		  dx *= -1;
	  } // end if
	  if (y > this.getHeight()- ball.getIconHeight()){
		  dy *= -1;
	  } // end if
	  if (y < 0){
		  dy *= -1;
	  } // end if
  } // end checkBounds
  
} // end class def
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.