Game is not running smoot?

Am creating this game and somehow the game is not running smooth. Can anyone help me with this plz…


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

public class Fighter extends JApplet implements Runnable,KeyListener
{
      Thread runner;
      Image buffer;
      Graphics gf;

      int x=400;
      int y=400;

      int skudXStart1=0;
      int skudYStart1=0;
      int skudXSlut1=0;
      int skudYSlut1=0;

      int skudXStart2=0;
      int skudYStart2=0;
      int skudXSlut2=0;
      int skudYSlut2=0;

      boolean right,left,space;


      public void init()
      {
          addKeyListener(this);
          setSize(800,600);
      }
      public void start()
      {

            if (runner == null)
            {
               runner = new Thread(this);
               runner.start();
            }
      }

      public void stop()
      {

            if(runner != null)
            {
               runner = null;
            }
      }

      public void run()
      {

            while(gf==null)
            {
              try {
              Thread.currentThread().sleep(50);
              } catch(InterruptedException e){}

            }

            while(Thread.currentThread()==runner)
            {

          repaint();

          gf.setColor(Color.white);
          gf.fillRect(0, 0, 800, 600);


              gf.drawImage((new ImageIcon("raptorSmall.gif")).getImage(),x,y,this);
              gf.setColor(Color.red);
              gf.drawLine(skudXStart1,skudYStart1,skudXSlut1,skudYSlut1);
              gf.drawLine(skudXStart2,skudYStart2,skudXSlut2,skudYSlut2);

              if (space)
              {
                    skudYStart1-=50;
                    skudYSlut1-=50;
                    skudYStart2-=50;
                    skudYSlut2-=50;

                    if (skudYStart1 <= 0)
                    {
                          space = false;
                    }
              }
              if (left)
              {
                    x -= 10;
              }
              if (right)
              {
                    x += 10;
            }

              try {
              Thread.currentThread().sleep(50);
              } catch(InterruptedException e) {}

            }
      }
      public void paint(Graphics g)
      {
          update(g);
      }

      public void update(Graphics g)
      {
            if (gf == null)
            {
              buffer = createImage(800, 600);
              gf = buffer.getGraphics();
            }
          g.drawImage(buffer, 0, 0, this);

      }
      public void keyPressed(KeyEvent e)
      {
             if(e.getKeyCode() == KeyEvent.VK_ESCAPE )
             {
                  System.exit(0);
             }
             if(e.getKeyCode() == KeyEvent.VK_LEFT)
             {
                  left=true;
             }
             if(e.getKeyCode() == KeyEvent.VK_RIGHT)
             {
                  right=true;
             }
             if(e.getKeyCode() == KeyEvent.VK_SPACE)
             {
                  if (skudYStart1 <= 0)
                  {
                        skudXStart1=x+20;
                        skudYStart1=y+30;
                        skudXSlut1=x+20;
                        skudYSlut1=450;
                        skudXStart2=x+78;
                        skudYStart2=y+30;
                        skudXSlut2=x+78;
                        skudYSlut2=450;
                  }

                  space=true;

           }
      }
      public void keyReleased(KeyEvent e)
      {
            if(e.getKeyCode() == KeyEvent.VK_LEFT)
            {
               left=false;
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            {
               right=false;
            }
            if(e.getKeyCode() == KeyEvent.VK_SPACE)
            {
            }
      }
      public void keyTyped(KeyEvent e){}


}


uhm.
gf.drawImage((new ImageIcon(“raptorSmall.gif”)).getImage(),x,y,this);

This will load the image from disk each frame. Bad! Load it into an image object once and draw that one instead. May help some I guess.

Didn’t help much… still not smooth ???

Just for fun get rid of the 50 ms sleep in your rendering loop.

ehhh… which one? I have 2…

The second one, the first one doesn’t control the rendering.

Its flickering like hell now??

I’m kind of a newb myself, but it looks like you are actually not double buffering here.

The way I’m using double buffering in my game (it’s my first attempt though) is to do this:

  • the update(Graphics g) method builds the buffer graphics if it’s not created. Then using the buffer graphics to clear everything and redraw by calling paint(bufferGraphics)
  • the paint(Graphics g) method does all the drawing, this looks like it’s happening in your game thread’s loop
  • the thread will just update the environment (move objects, etc) and then call repaint().

I think this is referred to as “passive” rendering, since it’s not just using this.getGraphics() in the game loop (which I think is called “active” rendering). Remember, I’m a newb to game dev, so this could all be wrong.
;D

For reference, this is where I learned double buffering techniques:
http://javaboutique.internet.com/tutorials/Java_Game_Programming/BildschirmflackernEng.html

nevermind, it looks like you are properly double buffering, things are just in weird spots. I dunno why it’s flickering then… sorry.

im sorry if you take offence, but your code is abit…
hmm…
unconventional :o

I suggest you take a look at some sample game code to get the basic structure fixed. This will sort out your flickering/stuttery animation.

If you’re creating (and disposing) a new image on each frame, you’re likely to run into garbage collection thrashing.

Also, for the type of the application you’re writing, using repaint() might not be a best choice, as the rendering happens asynchronously on another thread (so you can’t really guarantee the same intervals between the frames)

I’d suggest to use active rendering.

The idea is to take the rendering into your own hands.
Say, you’d have a method called
render(Graphics g);
which would render the world to the passed graphics object.

Then in your run() method, inside the loop, you’d have something like:
changeTheWorld();
render(backBufferGraphics);
copyBackBufferToTheScreen();
SleepUntilTheNextFrame();

the copyBackBufferToTheScreen() just copies the
backbuffer to the screen, something like this:
Graphics g = renderingPanel.getGrahics();
g.drawImage(backBuffer, 0, 0, null);

Ideally, you’d use BufferStrategy/FullScreen apis to do this.
Check out the tutorials on the Java2D and AWT home pages for more info on active rendering:
http://java.sun.com/products/java-media/2D/
http://java.sun.com/docs/books/tutorial/extra/fullscreen/

I’m sure you can find lots of exampleson this board as well.