java.awt.image.DataBufferByte中不能被强制转换为java.awt.image.DataBufferInt [英] java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
问题描述
我有一些错误,而ATM用JAVA IM编码,我一直在试图解决这一问题沿的时候,也试图找到oterh谁都有同样的问题脂肪酶和固定,但没有工作......
嗯..这里是code
包ca.vanzeben.game;进口java.awt.BorderLayout中;
进口java.awt.Canvas中;
进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.Graphics;
进口java.awt.image.BufferStrategy;
进口java.awt.image.BufferedImage中;
进口java.awt.image.DataBufferInt;进口javax.swing.JFrame中;公共类游戏扩展画布实现Runnable { 私有静态最后长serialVerisionUID = 1L; 公共静态最终诠释WIDTH = 160;
公共静态最终诠释HEIGHT = WIDTH / 12 * 9;
公共静态最终诠释SCALE = 3;
公共静态最后弦乐NAME =游戏; 公共布尔运行= FALSE;
公众诠释计时单位计数= 0; 私人的JFrame框架; 私人形象的BufferedImage =新的BufferedImage(宽度,高度,BufferedImage.TYPE_3BYTE_BGR);
私人诠释[] =像素((DataBufferInt)image.getRaster()getDataBuffer()。)的getData()。 公共游戏(){
了setMinimumSize(新尺寸(宽*尺,高度* SCALE));
setMaximumSize(新尺寸(宽*尺,高度* SCALE));
集preferredSize(新尺寸(宽*尺,高度* SCALE)); 帧=新的JFrame(名); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout()); frame.add(这一点,BorderLayout.CENTER);
frame.pack(); frame.setResizable(假);
frame.setLocationRelativeTo(NULL);
frame.setVisible(真);
} 公共同步启动无效(){
运行= TRUE;
新的Thread(本)。开始();
} 公共同步无效停止(){
运行= FALSE; } 公共无效的run(){
长lastTime = System.nanoTime();
双nsPerTick = 1000000000D / 60D; INT蜱= 0;
INT帧= 0; 长lastTimer = System.currentTimeMillis的();
双Δ= 0; 而(运行){
长今= System.nanoTime();
三角洲+ =(现 - lastTime)/ nsPerTick;
lastTime =现在;
布尔shouldRender = TRUE; 而(增量> = 1){
蜱++;
蜱();
三角洲 - = 1;
shouldRender = TRUE;
}
尝试{
视频下载(2);
}赶上(InterruptedException的E){
e.printStackTrace();
}
如果(shouldRender){
帧++;
渲染();
} 如果(System.currentTimeMillis的() - lastTimer> = 1000){
lastTimer + = 1000;
的System.out.println(蜱+滴答+帧+框架);
帧= 0;
蜱= 0;
}
}
} 公共无效打勾(){
计时单位计数++;
} 公共无效渲染(){
BufferStrategy中BS = getBufferStrategy();
如果(BS == NULL){
createBufferStrategy(3);
返回;
} 图形G = bs.getDrawGraphics(); g.setColor(Color.black);
g.fillRect(0,0,的getWidth(),的getHeight()); g.dispose();
bs.show();
} 公共静态无效的主要(字串[] args){
新的游戏()开始();
}
}
和错误是:
异常线程mainjava.lang.ClassCastException:java.awt.image.DataBufferByte中不能被强制转换为java.awt.image.DataBufferInt
在ca.vanzeben.game.Game<&初始化GT;(Game.java:30)
在ca.vanzeben.game.Game.main(Game.java:122)
要解决你的问题,你需要改变的BufferedImage的类型
私人BufferedImage的图像=新的BufferedImage(宽度,高度,
BufferedImage.TYPE_3BYTE_BGR);
并将其更改为
私人BufferedImage的图像=新的BufferedImage(宽度,高度,BufferedImage.TYPE_INT_RGB);
问题是, BufferedImage.TYPE_3BYTE_BGR
使用字节[3]重新present每个像素 BufferedImage.TYPE_INT_RGB
只使用一个int
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)
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
这篇关于java.awt.image.DataBufferByte中不能被强制转换为java.awt.image.DataBufferInt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!