-5

I have a problem in java: I have a method in a class that paints an Image. EDIT: Changed || to &&'s, fixed the else, but now the image is red. BTW: Assume the grid has been defined.

public BufferedImage scene() {
    BufferedImage image = new BufferedImage(250, 250, BufferedImage.TYPE_INT_RGB);
    if (ready) {
        int tv=0;
        int tc=0;
        for (int x = 0; x < 250; x++) {
            for (int y = 0; y < 250; y++) {
                int px = (int) ((x + r) / 16);
                int py = (int) (y / 16);
                if (px >= 0 && py >= 0 && px < width && py < height) {
                    image.setRGB(x, y, grid[px][py].image.getRGB(
                            (x + r) % 16, (y) % 16));
                } else {
                    image.setRGB(x, y, 0xFF0000);
                }
            }
        }
    }
    return image;
}

But when I run the code I get:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 19
at general.Level.scene(Level.java:47)
at general.Game.paintComponent(Game.java:34)
at javax.swing.JComponent.paint(JComponent.java:1037)
at javax.swing.JComponent._paintImmediately(JComponent.java:5106)
at javax.swing.JComponent.paintImmediately(JComponent.java:4890)
at javax.swing.RepaintManager$3.run(RepaintManager.java:814)
at javax.swing.RepaintManager$3.run(RepaintManager.java:802)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:802)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:745)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:725)
at javax.swing.RepaintManager.access$1000(RepaintManager.java:46)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1684)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:708)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:669)
at java.awt.EventQueue$2.run(EventQueue.java:667)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:678)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
 Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Barakados
  • 371
  • 7
  • 19
  • Can you tell us the line that out of bounds exception is happening at? – But I'm Not A Wrapper Class Jan 12 '14 at 02:02
  • I'm fairly certain that the bug is produced by *your* code, by the file `Level.java` at line 47. – Daniel Kamil Kozar Jan 12 '14 at 02:02
  • Did changing the `||` to `&&` fix the out-of-bounds exception? If so, it's really confusing to have the fixed code in your question now. If you have a separate problem then you need to create a new question. – Peter Bloomfield Jan 12 '14 at 02:23
  • *"Assume the grid has been defined."* No, I won't. Prove it with an [MCVE](http://stackoverflow.com/help/mcve). One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Jan 12 '14 at 04:24

1 Answers1

1

NOTE: The question has been edited since I wrote this answer.

This check seems to be wrong:

if (px >= 0 || py >= 0 || px < width || py < height)

You'll want logical AND (&&) instead of OR (||), otherwise it could be letting through invalid values of px or py. That's possibly what's causing your array index error (although you haven't shared enough code to be certain of that).

Peter Bloomfield
  • 5,578
  • 26
  • 37