java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt -
i have errors atm while im coding java, have been trying fix along time, trying find oterh ppl have same problem , fixed nothing work...
well.. here 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 = 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 error is:
exception in thread "main" java.lang.classcastexception: java.awt.image.databufferbyte cannot cast java.awt.image.databufferint @ ca.vanzeben.game.game.<init>(game.java:30) @ ca.vanzeben.game.game.main(game.java:122)
to solve problem, need change bufferedimage type of
private bufferedimage image = new bufferedimage(width, height, bufferedimage.type_3byte_bgr);
and change to
private bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
the problem bufferedimage.type_3byte_bgr
uses byte[3] represent each pixel , bufferedimage.type_int_rgb
uses int
Comments
Post a Comment