2
votes

I have a BufferedImage that is being drawn onto a JPanel. What I'm trying to do is set the pixels of the BufferedImage by:

for (int yy = 0; yy < 18; yy++) {
    for (int xx = 0; xx < 24; xx++) {
        image.setRGB(xx * 32, yy * 32, 32, 32, pixels, 0, 32);
    }
}

pixels is defined and instantiated, 18 is the height of the image/32 and 24 is the width of the image/32. This method seems to work, but only half of the time. What happens is that it will sometimes draw all of the image, sometimes it will draw maybe the first third of it (then stop), and sometimes it will draw the first 1/5th and then skip to another part of the image and draw some there then stop. No errors thrown, the application isn't pausing or loading. Sometimes it works, sometimes it doesn't. Any reason why this could be occurring?

1

1 Answers

3
votes

My guess is that yours is a threading issue since the code you show could take quite a long time (relatively speaking) to work, and to get it to work best with Swing, you must make sure to run it in a background thread, and then notify the GUI when it is done, and have the GUI then update the image. A SwingWorker would work well for this.