0

I've been working on a java game recently, and I have a lot of it figured out. One thing still plagues me, however. The way it's set up, a player moves across a background (the game board). Currently, every time the player moves, it repaints the whole frame, including the background. This causes a brief, yet annoying screen flicker whenever the player moves.

I've separated out my code to draw the background separately from the things that need to be repainted:

public void drawMap(Graphics pane) {...}

public void drawPlayer(Graphics pane) {...}

The problem is that I can't find a way to make the board stay on the screen when I use repaint(); , a necessity for the player to move. Any suggestions?

Docithe
  • 3
  • 4

2 Answers2

1

You should look into double buffering, basically you paint an image to the buffer, then paint the buffer. It should remove the flickering effect you are talking about. Below are a few helpful links:

http://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html
http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering
http://www.ecst.csuchico.edu/~amk/classes/csciOOP/double-buffering.html

Just comment if your having trouble understanding it.

UPDATE: I would also suggest you look in 'Killer game programming in java'. You can get a free ebook of one of the older versions. Some of it is a bit out dated, but the first few chapters about setting up a game loop and drawing to the screen etc are still very much relevant.

UPDATE 2: From the second link, try something like this:

private void drawStuff() { 
    BufferStrategy bf = this.getBufferStrategy();
    Graphics g = null;

  try {
    g = bf.getDrawGraphics();

       drawMap(g);
       drawPlayer(g);

  } finally {
    // It is best to dispose() a Graphics object when done with it.
    g.dispose();
  }

  // Shows the contents of the backbuffer on the screen.
  bf.show();

      //Tell the System to do the Drawing now, otherwise it can take a few extra ms until 
      //Drawing is done which looks very jerky
      Toolkit.getDefaultToolkit().sync();   
}

UPDATE 3: This post here gives a nice code sample that you can play with and adapt, that should give you the best idea on how to do double buffering

Community
  • 1
  • 1
dann.dev
  • 2,406
  • 3
  • 20
  • 32
  • I read up on those websites. I understand the idea behind it, but I'm not sure how to implement it using a Frame and a paint method. (The paint method calls the two methods listed above.) – Docithe Mar 27 '12 at 20:49
  • I saw that one, but it doesn't seem to use any paint methods. That's why I'm confused. – Docithe Mar 27 '12 at 22:18
  • I think `bf.show()` is what paints it to the screen, and `drawMap(g)` is what paints your stuff to the graphics object before that. Sorry have a bit of real world work to finish then I can take a better look at it for you. I think there are a few similar questions on Stackoverflow hidden around the place so maybe try find a few of those in the mean time – dann.dev Mar 27 '12 at 22:26
  • @Docithe This is the post i was looking for http://stackoverflow.com/questions/4430356/java-how-to-do-double-buffering-in-swing, i've put it in my main answer, but that should have everything you need for double buffering – dann.dev Mar 28 '12 at 01:44
0

I suggest to avoid redrawing everything with every change. Instead draw the whole frame at a fixed interval, e.g. every 50ms. Just keep the status of every element in a class and if something changes just change the data value. Due to the fixed redrawing interval the display will pick up any changes at the next redraw.

Jan Henke
  • 875
  • 1
  • 15
  • 28