0

I'm trying to use some code like is seen on another questions answer: https://stackoverflow.com/a/621849/1044984

Upon using this I get the following error:

Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (y + height) is outside of Raster
    at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
    at java.awt.image.BufferedImage.getSubimage(Unknown Source)
    at main.Grid.paintComponent(Grid.java:111)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Here is the code relating to this error:

try {

            tileSheetBig = ImageIO.read(new File("sprites/tiles.png"));
            charSheetBig = ImageIO.read(new File("sprites/player.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }

        final int tileWidth = 64;
        final int tileHeight = 64;
        final int tileRows = 1;
        final int tileCols = 11;
        tileSheet = new BufferedImage[tileRows * tileCols];

        for (int i = 0; i < tileRows; i++) {
            for (int j = 0; j < tileCols; j++) {
                tileSheet[(i * tileCols) + j] = tileSheetBig.getSubimage(i
                        * tileWidth, j * tileHeight, tileWidth, tileHeight);
            }
        }

        final int charWidth = 16;
        final int charHeight = 23;
        final int charRows = 2;
        final int charCols = 3;
        charSheet = new BufferedImage[charRows * charCols];

        for (int i = 0; i < charRows; i++) {
            for (int j = 0; j < charCols; j++) {
                charSheet[(i * charCols) + j] = charSheetBig.getSubimage(i
                        * charWidth, j * charHeight, charWidth, charHeight);
            }
        }

Since not much was changed from the code provided on the answer I can not see what the issue may be. I've tried to google the error but not many answers out there relate to my issue.

Community
  • 1
  • 1
ComputerLocus
  • 3,448
  • 10
  • 47
  • 96

1 Answers1

1

That RasterFormatException is thrown by getSubImage() when the area specified by [ x,y : x+width, y+height] is not contained within the BufferedImage area.

Check that your tiles.png image is at least 704x64 px (width*cols,height*rows) and similarly that player.png is at least 48x46 px.

EDIT: sorry i didn't notice it at first glance; player.png has to be 32x69 px and tiles.png 64x704 px

EDIT 2: this fixes your code for the player without editing the sprites; do the same for the tiles

final int charWidth = 64;
final int charHeight = 64;
final int charCols = 11;
final int charRows = 1;
for (int i = 0; i < charCols; i++) {
    for (int j = 0; j < charRows; j++) {
        charSheet[i * charRows + j] = charSheetBig
          .getSubimage(i * charWidth, j * charHeight, charWidth, charHeight);
    }
 }
guido
  • 18,864
  • 6
  • 70
  • 95
  • tiles.png is 704 by 64px. player.png is 48 by 46 px. – ComputerLocus May 24 '12 at 01:49
  • please see my edit; you use index *i* to span from 0 to a variable named *charRows* but actually you are using it for selecting x axis sprite (so for columns); and vice versa for *y* index. The same applies to tiles.png – guido May 24 '12 at 02:02
  • So your saying my images need to be vertical instead of horizontal and the player.png even has different dimensions then before? – ComputerLocus May 24 '12 at 02:05
  • Or change the code; just put a System.out.println(i * charWidth+","+ j * charHeight+","+ (i * charWidth+charWidth)+","+ (j * charHeight+charHeight)); inside the for loops to see it by yourself – guido May 24 '12 at 02:13
  • The System.out.println does not work as it still is doing the same thing, and giving the same error. – ComputerLocus May 24 '12 at 02:16
  • ok i did this for you; see edit. you had to comment out the method throwing the exception before adding the println, that was just a very basic way of debugging, meant to understand what did happen without going through a debugger run; not a fix itself... – guido May 24 '12 at 02:25
  • I do apologize for, you having to spoon feed me here, I'm new to java and even newer to graphics (images specifically), and therefore just am not as knowledgeable in this yet to understand it fully. I have tried switching to the code you edited in, but the same error is still present. – ComputerLocus May 24 '12 at 02:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11667/discussion-between-guido-and-fogest) – guido May 24 '12 at 02:40