Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Drawing to a Java BufferedImage's graphics producing inaccurate results.

  1. #1
    Junior Member
    Join Date
    Jul 2012
    My Mood
    Thanked 0 Times in 0 Posts

    Default Drawing to a Java BufferedImage's graphics producing inaccurate results.

    I'm trying to draw one image onto another using graphics.drawImage() but it's only working accurately for some images, others it messes up. My code's below, I've made sure texture is the right image when it enters the method so that's definitely not it. Any ideas?

        public void swapRegionWithTexture(Rectangle region, Image texture) {
            Graphics sheetGraphics = currentSheet.createGraphics();
            for (int ix = region.x; ix < region.x + region.width; ix++) {
                for (int iy = region.y; iy < region.y + region.height; iy++) {
                    currentSheet.setRGB(ix, iy, 0x000000);
            sheetGraphics.drawImage(texture, region.x, region.y, null);

    The general idea is:

    1. Grab the graphics to draw to.
    2. Clear out the section of the graphics that will be drawn on.
    3. Draw the image at the given location on the graphics.
    4. Dispose of the graphics.

    I've posted this question at Stackoverflow here, but the suggested fix I've gotten so far doesn't work.
    I've created an imgur gallery with the before and after here.
    Last edited by tmetcalfe; July 20th, 2012 at 07:49 PM.

Similar Threads

  1. Graphics Drawing Images/Rectangle Detecting/Deleting
    By Massaslayer in forum AWT / Java Swing
    Replies: 2
    Last Post: December 17th, 2011, 07:19 AM
  2. [SOLVED] Java Graphics: Drawing one object into another
    By zaxahmed in forum Object Oriented Programming
    Replies: 5
    Last Post: August 22nd, 2011, 08:06 AM
  3. Array code not producing, thoughts?
    By r19ecua in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 6th, 2011, 05:37 PM
  4. Help with a beginner's java assignment: Survey results
    By lavloki in forum Java Theory & Questions
    Replies: 17
    Last Post: October 14th, 2010, 09:08 AM
  5. keeping an drawing centered in Graphics
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 27th, 2010, 11:26 PM