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

标签 java image swing awt java-canvas

我在使用 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)

最佳答案

要解决您的问题,您需要更改 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

关于java.awt.image.DataBufferByte 无法转换为 java.awt.image.DataBufferInt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15839223/

相关文章:

html - 如何使用 CSS 替换 PNG 图像的颜色?

java - HikariCP 的 NullPointerException

java - 如何在 Sun Solaris 11 上将 java 版本从 1.6.0 降级到 java 1.5.0

javascript - 从维基百科 API 获取 SVG 文件

c# - Azure 数据库和照片

java - 树单元渲染器 : applying different style of the node's text which is depending on the user object's type

java - 为什么 Java GUI 中的大循环滞后/卡住?

java - 套接字中的 Swing 代码

java - java中的类实现是什么?

java - itext5 的 sandbox.jar 文件?