java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt

谁都会走 提交于 2019-12-18 05:17:04

问题


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...

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)

回答1:


To solve your problem, you need to change the BufferedImage type of

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




回答2:


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.

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.

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.



来源:https://stackoverflow.com/questions/15839223/java-awt-image-databufferbyte-cannot-be-cast-to-java-awt-image-databufferint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!