2
所以我有以下代碼:貯藏子圖像數據
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。
所以我想知道的是:
我的代碼是否正確?我試着去讓存儲我的主圖像文件的子圖像到所述陣列的二維數組,所以我可以隨意根據自己的陣列位置後調用。
謝謝。
參見['ImageLabelPanel'](http://stackoverflow.com/a/3078354/230513)。 – trashgod 2012-02-09 15:34:19
謝謝,我想清楚發生了什麼問題。我有我的變量倒退,所以它正在尋找一個320x1024的圖片,而不是我的圖像,這是1024x320。 – sekimberly52 2012-02-09 16:37:40