Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Issue involving drawing  (Read 2578 times)
0 Members and 1 Guest are viewing this topic.
Offline Tokki

Junior Newbie

« Posted 2012-07-24 07:02:29 »


I have been writing my first large scale game from scratch in Java/JSwing. The game I am making is a 2d overhead tile based game. Each land tile is a uniform size of 20x20 pixels and is redrawn every update as of now. Everything works fine except on a rare occasion where several blocks (always touching each other) seem to fail to draw for one draw cycle. The grey outline of the part-row/column appears and then quickly disappears as it is redrawn on the next draw call.

I cannot easily reproduce this however I notice that it only appears when I spam keyboard and mouse input. Usually once per 10/20 seconds of hammering my left/right click and keyboard will seemingly cause it to come up but really I cannot be sure if this is the cause or just something that exadurates an underling problem. I am using a class which extends keyAdapter and my main class implements ActionListener/MouseListener.

Has anyone heard of something causing this or have any ideas at all? I can post whatever code may be helpful if requested, please just specify which parts you wish to see. Thanks for your time!
Offline jonjava
« Reply #1 - Posted 2012-07-24 07:22:28 »

Seems like you might be experience flickering. What are you drawing on? A Canvas? A Frame? A JFrame? A JPanel?

IIRC AWT descendants don't double buffer automatically which causes flickering if you don't do it yourself and swing thingies (JFrame, JPanel) do double buffer but they handle their painting a bit mysteriously from you - in that case, you might want to turn swings automatic painting off ( "setIgnoreRepaint(true);" ) and paint manually by calling it inside your game loop and also implement your own double buffering like with the AWT descendants to get rid of flickering.

Here's a good link I found with google-fu that explains a bit more in depth

Flickering is basically what happens if you try and show an image to the screen while the image hasn't been fully drawn yet. So what Double Buffering means is that we have 2 images. One image that we draw on, and the other that we show on the screen. This way, we're always showing an image that is fully drawn and never an image that is incomplete.

E.g we have img1 and img2. We draw on img1 and once it has been fully drawn we save it in img2 and then we show img2 on the screen. Then we draw on img1 again, img1 hasn't been fully drawn yet, so we keep showing img2 on the screen, once img1 has been fully drawn, we save it in img2 and show it on the screen etc.

Offline Tokki

Junior Newbie

« Reply #2 - Posted 2012-07-24 07:26:13 »

Very helpful thank you! I am using a JFrame with an attached JPanel which is being drawn on. I had heard they had their own double buffering so I overlooked that a bit. I will give your suggestion a shot and see if it corrects it when I get home from work. Thanks again Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Tokki

Junior Newbie

« Reply #3 - Posted 2012-08-02 19:50:31 »

After a few days of messing with it I finally got my double buffering working and you were indeed right, it was jframes double buffering. Thank you again for you help  Grin
Offline Tokki

Junior Newbie

« Reply #4 - Posted 2012-08-10 01:22:13 »

I now receive an error on compile every 10th or so try, the error and code in question is as follows:


Exception in thread "Thread-3" java.lang.IllegalStateException: Buffers have not been created
   at Source)
   at java.awt.Component$FlipBufferStrategy.getBackBuffer(Unknown Source)
   at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Unknown Source)
   at java.awt.Component$FlipBufferStrategy.revalidate(Unknown Source)
   at java.awt.Component$FlipBufferStrategy.revalidate(Unknown Source)
   at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Unknown Source)

Code part 1:

   public main()


      createBufferStrategy(2);  <--- These two lines are pointed to some times
      bufferStrategy = getBufferStrategy(); <---


Code part 2:
   private void drawGame(float interpolation)
      gamePanel.draw(); <--- Where the offscreen image is set up and drawn to onscreen
      Graphics g = bufferStrategy.getDrawGraphics(); //<-- being pointed to in this error
      g.drawImage(onScreenImage, 0, 0, null);

Offline StumpyStrust
« Reply #5 - Posted 2012-08-10 06:42:39 »

Use code block helps us understand stuff.

I need to look at more code but it looks like you are drawing before the buffer strategy is made. Make sure everything is created before you start the game/render/whatever.

Offline jonjava
« Reply #6 - Posted 2012-08-10 08:05:10 »

Code blocks look like this: ( press the [size=12pt]#[/size] icon when posting or insert code between [code][/code] tags )

public main() { /*blabla*/ }

Try and create your bufferstrategy in your drawGame loop but don't create it if it already exists. And remove it altogether from the main function.


public void drawGame() {
     BufferStrategy bufferStrategy = getBufferStrategy();
     if (bufferStrategy == null) {


Offline Tokki

Junior Newbie

« Reply #7 - Posted 2012-08-10 15:05:49 »

Thank you again Cheesy

This is for you
Pages: [1]
  ignore  |  Print  

EgonOlsen (42 views)
2018-06-10 19:43:48

EgonOlsen (22 views)
2018-06-10 19:43:44

EgonOlsen (43 views)
2018-06-10 19:43:20

DesertCoockie (197 views)
2018-05-13 18:23:11

nelsongames (124 views)
2018-04-24 18:15:36

nelsongames (123 views)
2018-04-24 18:14:32

ivj94 (863 views)
2018-03-24 14:47:39

ivj94 (124 views)
2018-03-24 14:46:31

ivj94 (768 views)
2018-03-24 14:43:53

Solater (140 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!