Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 1091 times)
0 Members and 1 Guest are viewing this topic.
Offline Tokki

Junior Newbie





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

Hello!

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 http://stackoverflow.com/questions/4430356/java-how-to-do-double-buffering-in-swing?answertab=votes#tab-top

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:

Error:

Exception in thread "Thread-3" java.lang.IllegalStateException: Buffers have not been created
   at sun.awt.windows.WComponentPeer.getBackBuffer(Unknown 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()
   {

      ......
     
      setIgnoreRepaint(true);
      this.setVisible(true);
 

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

      runGameLoop();
   }


Code part 2:
   private void drawGame(float interpolation)
   {
      gamePanel.setInterpolation(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);
      g.dispose();
      bufferStrategy.show();
   }


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 )

1  
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.

I.e:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public void drawGame() {
     BufferStrategy bufferStrategy = getBufferStrategy();
     if (bufferStrategy == null) {
          createBufferStrategy(2);
          return;
     }

     ...
}

Offline Tokki

Junior Newbie





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

Thank you again Cheesy

This is for you

https://www.youtube.com/watch?v=Dq0llrCYtCQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (23 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (96 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (152 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!