将subImage数据存储在数组中

时间:2012-02-09 15:00:01

标签: java swing

所以我有以下代码:

package animation;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AnimTest
{
public static void main(String[] args)
{
    AnimTest test = new AnimTest();
    test.go();
}

public void go()
{
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyDrawP drawP = new MyDrawP();
    frame.getContentPane().add(drawP);
    frame.setSize(500,500);
    frame.setVisible(true);
}
}

class MyDrawP extends JPanel
{

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    try {

        BufferedImage bigImg = ImageIO.read(new File("C:/Users/scott/Desktop/Personal Work/Pixel Art/terrain.png"));

        final int width = 64;
        final int height = 64;
        final int rows = 5;
        final int cols = 16;
        BufferedImage[][] sprites = new BufferedImage[rows][cols];

        int x = 0;
        int y = 0;

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                sprites[i][j] = bigImg.getSubimage(i * width, j * height, width, height);
                //g.drawImage(sprites[i][j], 5, 5, this);
            }
        }
        //Image subImage = bigImg.getSubimage(x, y, width, height);
        g.drawImage(sprites[0][0], 5, 5, this);

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

}
}

生成以下错误:

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 animation.MyDrawP.paintComponent(AnimTest.java:55)
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)

现在,它在第55行说错了,但我不明白为什么我的代码会产生那种错误; RasterFormatException。

所以我想知道的是:

我的代码是否正确?我试图制作一个二维数组,将我的主图像文件的子图像存储到所述数组中,以便我可以根据它们的数组位置随意回忆它们。

感谢。

1 个答案:

答案 0 :(得分:2)

java.awt.image.RasterFormatException: (y +       height) is outside of Raster

也许你的图像尺寸用完了?请看documentation