我試圖訪問其中使用ImageIO.read(filePath)
文件加載的BufferedImage
像素,但我得到這個錯誤:如何更改從文件加載的BufferedImage的圖像類型?
Exception in thread "Game" java.lang.ClassCastException: java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
at com.package.graphics.Texture.<init>(Texture.java:29)
at com.package.graphics.Texture.loadTexture(Texture.java:40)
at com.package.Game.run(Game.java:71)
at java.lang.Thread.run(Unknown Source)
在代碼中,其誤差就行了,是在構造函數中,看起來像這樣:
// Get the pixel array from the BufferedImage
this.pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
據我瞭解這一點,BufferedImage的是類型BufferedImage.TYPE_INT_RGB
或BufferedImage.TYPE_INT_ARGB
的不是。因爲我在遊戲的其餘部分中使用了這些類型,所以我想知道是否有方法將加載的圖像從加載的類型轉換爲另一個類型。
在我的情況下,我想將圖像類型轉換爲BufferedImage.TYPE_INT_ARGB
。
好,謝謝!但是你也必須在新的圖像上繪製舊圖像。 – 2014-12-13 09:56:35
我會將其添加到您的答案中。 – 2014-12-13 09:57:01
修復了一次只調用'createGraphics()'的代碼('getGraphics()'調用相同的代碼)。 – haraldK 2014-12-13 11:38:09