java.awt.image.DataBufferByte中不能被强制转换为java.awt.ima

2019-08-19 21:29发布

我有一些错误,ATM,同时用JAVA IM编码,我一直在试图解决这一问题沿的时候,也试图找到oterh谁具有同样的问题,固定它,但没有工作脂肪酶...

嗯..这里是代码

    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)

Answer 1:

为了解决您的问题,您需要更改的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使用字节[3]来表示每个像素和BufferedImage.TYPE_INT_RGB只使用一个int



Answer 2:

问题是, image.getRaster().getDataBuffer()返回一个DataBufferByte,而你试图转换为DataBufferInt。 这是两个不同的类,DataBuffer的两个子类,但一个是不是其他的子类,所以它们之间的转换是不可能的。

对于栅格的规范没有明确说明是什么决定getDataBuffer是否返回DataBufferByte或DataBufferInt(或者DataBuffer的一些其他的味道)。 但想必这取决于图像的类型解剖。 你可能解剖字节每像素图像和代码,因为它的立场,预计32位每像素。

正因为如此,你可能需要删除一些从这一逻辑的<init>部分,并将其添加到显式构造,所以可以测试类型的DataBuffer返回,并相应地处理它,而不是无条件铸造DataBufferInt。



文章来源: java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt