java - 将子图像数据存储在数组中

标签 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。

所以我想知道的是:

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

谢谢。

最佳答案

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

也许您的图像尺寸用完了?看documentation .

关于java - 将子图像数据存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9213202/

相关文章:

events - 扩展面板中的 Scala Swing react

java - 在运行时更改字体

java - SWT WizardDialog 未显示在任务栏中

java - Java程序如何在虚拟环境中运行Python程序?

java - 如何从服务器读取文本文件并仅显示例如textview 中的最后 100 行

java - 如何根据显示器尺寸调整桌面应用程序的大小

java - 警告 : A HTTP GET method, public javax.ws.rs.core.Response... 抛出 org.codehaus.jettison.json.JSONException,不应消耗任何实体

javascript - 如何使用 pentaho 从 pdf 中提取文本?

java - 我的 Swing 应用程序中的边框布局出了什么问题?

java - 劫持 Java Swing 组件中的 `repaint()` 调用