Rendering problem

Hello I am coding a small araknoid type of game but am facing a problem.I have written a code to move the ball in the screen but the problem is that it does not move uniformly.The ball speeds up sometimes whereas sometimes slows down (i.e it flickers).I am using BufferedImage but no luck…Please help me…

Here is the code :

import java.awt.;
import java.awt.image.
;
import java.awt.geom.*;
class interTest extends Frame implements Runnable
{int width=1368,height=760;
BufferedImage buffer=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d=buffer.createGraphics();
Imob j;
interTest()
{
super(“Game”);
setSize(width,height);
setVisible(true);
setEnabled(true);
repaint();
new Thread(this).start();
}
public void paint(Graphics g)
{

    g2d.setColor(Color.WHITE);
    g2d.fillRect(0,0,width,height);
    if(j!=null)j.update(g2d);
  g.drawImage(buffer,0,0,this);

}
public void start(){}
public void stop(){}
public void run(){
j=new Imob(2,2,10,10,this);
j.load(“balls.gif”);
while(true){
repaint();
try{Thread.sleep(5);}catch(Exception e){}
}

}

}

import java.awt.;
import java.net.
;
import javax.swing.;
import java.awt.geom.
;
class Imob{
Toolkit loader=Toolkit.getDefaultToolkit();
Image img;
int x,y,dx,dy;
Frame frame;
Imob(int dx,int dy,int x,int y,Frame f)
{
this.x=x;
this.y=y;
this.dx=dx;
this.dy=dy;
this.frame=f;
}
public void load(String s)
{img=loader.getImage(this.getClass().getResource(s));
}
public void draw(Graphics2D g2d)
{

  g2d.setTransform(new AffineTransform());
  g2d.drawImage(img,x,y,frame);  }
public Rectangle getBounds()
{
    int height=img.getHeight(frame);
    int width=img.getWidth(frame);
    return(new Rectangle(x,y,width,height));
}
public boolean hasColided(Imob x)
{
    return getBounds().intersects(x.getBounds());
}
public void update(Graphics2D g2d)
{x+=dx;
 y+=dy;   
   draw(g2d); 
}
}

There is 2 fundamental problems with your code at the moment:

In regard to speed, you have to remember that every computer runs at a different speed, and the speed may vary during execution of your program. e.g. A fast computer will run an update loop at a faster rate, thus moving the ball at a faster rate. This is most commonly addressed by computing the time between frames, called the delta. This delta is used to scale the amount of ball moves on the next frame. e.g. If your frames are taking 0.1s to pass, then you will want to move your ball 1/10th of the distance.

In regard to flickering, this is most commonly solved through a technique called double buffering. I believe Java has a BufferStrategy system for doing this.

I’ll leave these as an exercise for you to complete as there is plenty of resources online.

And just for future reference, when creating a post, you can put your code in code tags, which makes code keep it’s formatting.

I am using BufferedImage for buffering each frame before drawing.Is it not double buffering?? or double buffering is something else?

Please let me know…

I’m not really familiar with the Java2D API, but BufferedImage is almost definitely not referring to double buffering. A buffer is just an area for data. Double buffering just uses buffers. It is essentially a technique where you have two buffers. You draw all your graphics to a back buffer, and display the front one. Only once the back buffer is complete and it’s time for the monitor to redraw, do you set the back buffer to be the front, and the front to be the back, and repeat the process. This prevents the drawing to the monitor a partially complete render, what you would call tearing.

You are currently using a Frame from the AWT library, which I don’t believe uses double buffering by default. The Swing library, which you should probably use anyway, does however. That would be the easiest way to fix the problem. Fortunately, this is as simple as changing “extends Frame” to “extends JFrame”, and importing the the class.

Edit: I missed where you were using a thread to update your logic at a fixed rate, which @KevinWorkman observed. He is correct, which means my mention about delta time isn’t strictly necessary, though I’ll stick to saying you should use it instead.

Additionally, this link: http://www.codeproject.com/Articles/2136/Double-buffer-in-standard-Java-AWT?msg=3559183#xx3559183xx outlines the underlying issue, but you should go with the mentioned solutions here rather than what is on that page. That is not to say the solution is bad, but it’s an unnecessary effort.

I am now using javax.swing.jframe.But the problem is still persisting.When I run the code , at first the ball moves slowly(non uniformly)and then suddenly it gains speed(after gaining speed, it moves somewhat uniformly) and suddenly slows down(slow down effect is short-lived and again it restores to previous condition)…Please help…

That’s because you’re using a Thread to call repaint(), and you’re calling your update code (aka “business logic”) from the paint() method. You can’t guarantee that calling repaint() will result in calling paint(), since multiple calls to repaint() can be “collapsed” into a single paint() call.

Step 1: Switch to using a JPanel inside a JFrame. This will handle double buffering for you.

Step 2: Use a Swing Timer instead of a Thread. This is a no-no in “real” games, but it will make your life much easier while you learn the basics.

Step 3: Separate your “business logic” (updating the position of the ball) from your painting code.

Step 4: In your “business logic”, take into account the actual time that has elapsed, and adjust the ball’s speed accordingly.

Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/painting/

Shameless self-promotion: http://staticvoidgames.com/tutorials/swing/customPainting

Yes your advice has helped a lot…Flickering has reduced but is still there…please help!!!

You’ve got three more steps of advice left. :stuck_out_tongue:

After you’ve followed them, please post an updated MCVE and we’ll go from there.

By the way, swing’s Components are Double Buffered already…