Tuesday, 15 May 2012

bufferedimage - Java - Displaying a Background by using Buffered Image -



bufferedimage - Java - Displaying a Background by using Buffered Image -

writing little game here , have ran issues. can not life of me manage display buffered image.

i think have in place wrong if background still black.

game class:

public class game extends canvas implements runnable { private boolean running = false; private thread thread; public static int width, height; public bufferedimage background = null; // object handler handler handler; random rand = new random(); private void init(){ width = getwidth(); height = getheight(); bufferedimage background = new bufferedimage(0, 0, 0); try{ background = imageio.read(new file("/background.png")); }catch(ioexception e){ } handler = new handler(); public synchronized void start(){ if(running) return; running = true; thread = new thread(this); thread.start(); } public void run(){ // heat of game. game loop. init(); this.requestfocus(); long lasttime = system.nanotime(); double amountofticks = 60.0; double ns = 1000000000 / amountofticks; double delta = 0; long timer = system.currenttimemillis(); int updates = 0; int frames = 0; while(running){ long = system.nanotime(); delta += (now - lasttime) / ns; lasttime = now; while(delta >= 1){ tick(); updates++; delta--; } render(); frames++; if(system.currenttimemillis() - timer > 1000){ timer += 1000; system.out.println("fps: " + frames + " ticks " + updates); frames = 0; updates = 0; } } } private void tick(){ handler.tick(); } private void render(){ bufferstrategy bs = this.getbufferstrategy(); if(bs == null){ this.createbufferstrategy(3); return; } graphics g = bs.getdrawgraphics(); g.setcolor(color.black); g.fillrect(0, 0, getwidth(), getheight()); g.drawimage(background, 0, 0, null); g.dispose(); bs.show(); } private void loadimagebackground(bufferedimage image){ int h = image.getheight(); int w = image.getwidth(); } public static void main(string[] args) { new window(762,720, "busybee", new game()); } }

window class:

public class window extends game { public window(int w, int h, string title, game game){ game.setpreferredsize(new dimension(w,h)); game.setmaximumsize(new dimension(w,h)); game.setminimumsize(new dimension(w,h)); jframe frame = new jframe(title); frame.add(game); frame.pack(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable(false); frame.setlocationrelativeto(null); frame.setvisible(true); frame.seticonimage(background); // starts synchronized game start() method game.start(); } }

i didn't have bufferedimageloader, used next code load image:

try { background = imageio.read(new file("/background.png")); } grab (ioexception e) { }

don't forget draw image in render!

private void render(){ bufferstrategy bs = this.getbufferstrategy(); if(bs == null){ this.createbufferstrategy(3); return; } graphics g = bs.getdrawgraphics(); g.setcolor(color.black); g.fillrect(0, 0, getwidth(), getheight()); // draw background!! g.drawimage(background, 0, 0, null); g.dispose(); bs.show(); }

java bufferedimage

No comments:

Post a Comment