java.awt.image.DataBufferByte 不能转换为 java.awt.image.DataBufferInt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15839223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
提问by filleszq
I have some errors atm while im coding with JAVA, I have been trying to fix this for along time, also trying to find oterh ppl who have same problem and fixed it but nothing work...
我在用 JAVA 编码时出现了一些错误 atm,我一直在尝试解决这个问题,也试图找到其他有同样问题的人并修复它,但没有任何效果......
Well.. here is the code
嗯..这是代码
package ca.vanzeben.game;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVerisionUID = 1L;
public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH / 12*9;
public static final int SCALE = 3;
public static final String NAME = "Game";
public boolean running = false;
public int tickCount = 0;
private JFrame frame;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
public Game(){
setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT * SCALE));
setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT * SCALE));
setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT * SCALE));
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public synchronized void start() {
running = true;
new Thread(this).start();
}
public synchronized void stop() {
running = false;
}
public void run(){
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D/60D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while(running){
long now = System.nanoTime();
delta +=(now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while(delta >= 1){
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (shouldRender){
frames++;
render();
}
if(System.currentTimeMillis() - lastTimer >= 1000){
lastTimer += 1000;
System.out.println(ticks + " ticks, " + frames + " frames");
frames = 0;
ticks = 0;
}
}
}
public void tick() {
tickCount++;
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}
public static void main(String[] args) {
new Game().start();
}
}
And the error is:
错误是:
Exception in thread "main" java.lang.ClassCastException: java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
at ca.vanzeben.game.Game.<init>(Game.java:30)
at ca.vanzeben.game.Game.main(Game.java:122)
回答by the13ghost
To solve your problem, you need to change the BufferedImage type of
要解决您的问题,您需要更改 BufferedImage 类型
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_3BYTE_BGR);
and change it to
并将其更改为
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
the problem is that BufferedImage.TYPE_3BYTE_BGR
uses byte[3] to represent each pixel and
BufferedImage.TYPE_INT_RGB
just uses an int
问题是BufferedImage.TYPE_3BYTE_BGR
使用 byte[3] 来表示每个像素并且
BufferedImage.TYPE_INT_RGB
只使用一个 int
回答by Hot Licks
The problem is that image.getRaster().getDataBuffer()
is returning a DataBufferByte, and you're attempting to cast to a DataBufferInt. Those are two distinct classes, both subclasses of DataBuffer, but one is not a subclass of the other, so casting between them is not possible.
问题是image.getRaster().getDataBuffer()
正在返回 DataBufferByte,而您正试图转换为 DataBufferInt。这是两个不同的类,都是 DataBuffer 的子类,但一个不是另一个的子类,因此无法在它们之间进行转换。
The spec for Raster doesn't clearly describe what determines whether getDataBuffer returns a DataBufferByte or a DataBufferInt (or perhaps some other flavor of DataBuffer). But presumably this varies depending on the type of image being dissected. You're probably dissecting a byte-per-pixel image and the code, as it stands, expects 32-bits-per-pixel.
Raster 的规范没有清楚地描述是什么决定了 getDataBuffer 是返回 DataBufferByte 还是 DataBufferInt(或者可能是其他一些 DataBuffer 风格)。但据推测,这取决于被解剖的图像类型。您可能正在剖析每像素字节的图像,并且代码,就目前而言,期望每像素 32 位。
As it is, you probably need to remove some of that logic from the <init>
section and add it to the explicit constructor, so you can test the type of DataBuffer returned and handle it accordingly, rather than unconditionally casting it to DataBufferInt.
实际上,您可能需要从该<init>
部分中删除一些逻辑并将其添加到显式构造函数中,以便您可以测试返回的 DataBuffer 的类型并相应地处理它,而不是无条件地将其强制转换为 DataBufferInt。