java.awt.image.DataBufferByte 无法转换为 java.awt.image.DataBufferInt

8

我在编写JAVA代码时遇到了一些错误,我已经尝试很长时间来解决这个问题,也试图找到其他有同样问题并解决的人,但是都没有成功...

好吧.. 这是代码

    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();
    }   
}

错误信息如下:

 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)

1
我们没有行号,你能标记一下是哪一行吗? - Sam I am says Reinstate Monica
3
我猜它不喜欢你这样做:(DataBufferInt)image.getRaster().getDataBuffer() - Sam I am says Reinstate Monica
是的,这与以下有关:private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); - filleszq
正如错误所指示的那样,您正在尝试将DataBufferByte转换为DataBufferInt。您还对pixels[]做了其他什么操作? - asgs
第30行:private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 第120-123行:public static void main(String[] args) { new Game().start(); } - filleszq
3个回答

12

为了解决您的问题,您需要更改 BufferedImage 的类型。

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT,  
BufferedImage.TYPE_3BYTE_BGR);

并将其更改为

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

问题在于BufferedImage.TYPE_3BYTE_BGR使用byte[3]来表示每个像素, 而BufferedImage.TYPE_INT_RGB只使用一个int。


6
问题在于image.getRaster().getDataBuffer()返回的是一个DataBufferByte对象,而你试图将其转换为DataBufferInt。这两个是不同的类,都是DataBuffer的子类,但其中一个不是另一个的子类,因此它们之间的转换是不可能的。
对于栅格(Raster)的规范没有清楚地描述是什么决定了getDataBuffer返回DataBufferByte还是DataBufferInt(或者是其他类型的DataBuffer)。但可能这取决于所分解的图像类型。您可能正在分析每像素一个字节的图像,而代码则期望32位每像素。
因此,您可能需要从<init>部分中删除一些逻辑,并将其添加到显式构造函数中,以便可以测试返回的DataBuffer类型并相应地处理它,而不是无条件地将其强制转换为DataBufferInt。

1

image.getRaster().getDataBuffer().getDataType()提供了来自DataBuffer类的类型常量,例如TYPE_BYTE、TYPE_SHORT、TYPE_INT,每个都绑定了DataBuffer[Type]子类,因此您可以检测是否会成功进行类型缓冲区转换。

int和byte[4]之间的转换基本上取决于一个字节顺序规则,在Java内部类型级别上未定义,因此这样的数组的重新解释转换将无法定义。

如果您不是像接受的答案假设的那样是图像的创建者,则需要定义并应用类型转换。

编辑
DataInputStream.readInt实际上使用了一个已定义的转换:大端存储次序

byte[]int[]的转换可以通过使用以字节数组初始化的ByteArrayInputStream作为源,从中读取整数的DataInputStream来执行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接