• Post Reply Bookmark Topic Watch Topic
  • New Topic

Double drawing pixels when manipulating DataBuffer of a BufferedImage  RSS feed

 
Ian Griffiths
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been working my way through a tutorial to build a simple 2D tile-based engine that also allows the code to manipulate the colour of pixels on the screen based on a monochrome map read from a file. I've got the code right line for line, but I seem to have a bug when it comes to draw to the screen! At regular intervals, despite what is held in the pixel array of ints (which I have confirmed to be correct when debugging), I get the same row of pixels being draw twice in a row. I figured there was some loop issue somewhere, but after attempting to debug this for some time, I haven't come up with the answer.

I was hoping someone with a bit more experience of Java 2D interfaces might be able to spot what I haven't been able to

Original tilesheet:As rendered:

Game.java:

Screen.java:

SpriteSheet.java:
 
Marcel Boelen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also had this strange behavior, and it took me a while to figure it out!
First of all, it has nothing to do with your code. Your code works just as supposed!
The problem lies with the canvas of the Frame where you are drawing on!

You set a dimension on the frame, but then there comes the problem. You pack the frame before you set resizable to false.
Pack has the following function on the jframe. http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#pack()

Pack()
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.
If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size.
The Window will be validated after the preferredSize is calculated.


When you don't set resizable to false before you pack, you allow the frame to resize to fit in the window.
When you set resizable false before packing the frame you avoid this behavior.


You can also check this by removing the decoration around the drawing canvas of the frame.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!