swing - Java - Pac-Man - GUI - Drawing Graphics issue, and general tips for an aspiring programmer -
i making pac-man , i'm having problem drawing graphics on frame, when draw point image looks game of snake, tried putting drawing methods background , char both in render method, point image flickers
what looks like, sense free ignore random face within joke.
also first game tips on structure, pointers on doing right (if anything) , i'm doing wrong, , general tips extremely helpful!
also aware have couple unused methods
code:package game; import graphics.map; import java.awt.borderlayout; import java.awt.canvas; import java.awt.color; import java.awt.container; import java.awt.dimension; import java.awt.font; import java.awt.graphics; import java.awt.insets; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.jbutton; import javax.swing.jframe; public class main extends canvas implements runnable{ private static final long serialversionuid = 1l; //not sure why wanted me this, maybe inquire bender, or google later public static boolean running = false; public static int height = 800; public static int width = 600; public static int posx = 50; public static int posy = 50; public static final string name = "pac man alpha 1.4"; private static final double speed = 1.2; public input input; static bufferedimage background = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage pacman = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage settingsbackground = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage level1 = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage level2 = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage points = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage point = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static jframe frame; private input keypress = new input(); private map map; private static boolean charloaded = false; public static boolean main_menu = true; public static boolean game = false; public static boolean level1test = true; public static boolean level2test = false; public static boolean level3test = false; public static boolean level4test = false; static boolean drawn = false; public static boolean key_down; public static boolean key_up; public static boolean key_right; public static boolean key_left; //private screen screen; jbutton startbutton = new jbutton("start"); //start jbutton settingsbutton = new jbutton("settings"); //settings jbutton exitbutton = new jbutton("exit"); //exit public main() { setminimumsize(new dimension(width , height )); setmaximumsize(new dimension(width , height )); // keeps canvas same size setpreferredsize(new dimension(width, height)); frame = new jframe(name); if(main_menu == true && game == false){ buttons(frame.getcontentpane()); } frame.setlayout(new borderlayout()); frame.setdefaultcloseoperation(jframe.exit_on_close); // ends programme on // close frame.addkeylistener(new input() ); frame.add(this, borderlayout.center); frame.pack(); // keeps size right frame.setresizable(false); frame.setvisible(true); this.addkeylistener(keypress); } public static void main(string[] args) { seek { background = imageio.read(new file("res\\background.png")); pacman = imageio.read(new file("res\\pacmansprites.png")); settingsbackground = imageio.read(new file("res\\background.png")); level1 = imageio.read(new file("res\\level1.png")); //level2 = imageio.read(new file("res\\level2.png")); point = imageio.read(new file("res\\points pacman.png")); } grab (ioexception e) { } running = true; new main().start(); } public void run() { long lasttime = system.nanotime(); double nspertick = 1000000000 / 60d; long lasttimer = system.currenttimemillis(); double delta = 0; int frames = 0; int ticks = 0; while (running == true) { long = system.nanotime(); delta += (now - lasttime) / nspertick; lasttime = now; boolean render = false; while (delta >= 1) { ticks++; tick(); delta -= 1; render = true; } seek { thread.sleep(3); //keep frames going high } grab (interruptedexception e) { e.printstacktrace(); } if(render == true){ frames++; render(); } if (system.currenttimemillis() - lasttimer >= 1000) { lasttimer +=1000; //system.out.println("frames: " + frames + " ticks: " + ticks); frames = 0; ticks = 0; } } } public synchronized void start() { new thread(this).start(); run(); } public synchronized void stop() { running = false; } public void tick() { if (key_up) posy -= speed / 2; if (key_down) posy += speed; if (key_left) posx -= speed / 2; if (key_right) posx += speed; } public void render() { drawn = false; if(main_menu == false && game == true) { drawmap(); drawchar(); } else if(main_menu == false && game == false) { graphics g = getgraphics(); { g.drawimage(settingsbackground,0,0,getwidth(),getheight(),null); g.dispose(); } } else { graphics g = getgraphics();{ g.drawimage(background,0,0,getwidth(), getheight(),null); g.dispose(); //kill } } } public void drawmap(){ if(level1test == true){ graphics g = getgraphics(); { g.drawimage(level1,0,0,getwidth(),getheight(),null); g.dispose(); } } if(level2test == true && drawn == false){ graphics g = getgraphics(); { g.drawimage(level2,0,0,getwidth(),getheight(),null); } g.dispose(); } drawn = true; } public void drawchar(){ //drawmap(); graphics g = getgraphics();{ g.drawimage(point,posx,posy,20, 20,null); g.dispose(); revalidate(); } } public void begin() { if (key_up) system.out.println("up"); if (key_down) system.out.println("down"); if (key_left) system.out.println("left"); if (key_right) system.out.println("right"); } public void loadmap(){ if(!drawn && level1test){ }else if(!drawn && level2test){ //draw 2nd map here }else if(!drawn && level3test){ //draw 3rd map here } } public void buttons(container pane) { pane.setlayout(null); startbutton.addactionlistener( new actionlistener() { public void actionperformed(actionevent ae) { main_menu = false; game = true; frame.remove(startbutton); frame.remove(settingsbutton); frame.remove(exitbutton); frame.revalidate(); drawmap(); system.out.println("start button clicked"); } } ); settingsbutton.addactionlistener( new actionlistener() { public void actionperformed(actionevent ae) { main_menu = false; game = false; frame.remove(startbutton); frame.remove(settingsbutton); frame.remove(exitbutton); frame.revalidate(); frame.repaint(); system.out.println("settings button clicked"); } } ); exitbutton.addactionlistener( new actionlistener() { public void actionperformed(actionevent ae) { system.out.println("exit button clicked"); system.exit(0); } } ); pane.add(startbutton); pane.add(settingsbutton); pane.add(exitbutton); insets insets = pane.getinsets(); dimension size = startbutton.getpreferredsize(); startbutton.setbackground(new color(0, 0, 0)); startbutton.setforeground(color.cyan); startbutton.setfocuspainted(false); startbutton.setfont(new font("calabri", font.bold, 16)); settingsbutton.setbackground(new color(0, 0, 0)); settingsbutton.setforeground(color.red); settingsbutton.setfocuspainted(false); settingsbutton.setfont(new font("calabri", font.bold, 16)); exitbutton.setbackground(new color(0, 0, 0)); exitbutton.setforeground(color.yellow); exitbutton.setfocuspainted(false); exitbutton.setfont(new font("calabri", font.bold, 16)); startbutton.setbounds((width - 125) + insets.left, 10 + insets.top, size.width + 50, size.height + 10); settingsbutton.setbounds((width - 125) + insets.left, 55 + insets.top, size.width + 50, size.height + 10); exitbutton.setbounds((width - 125) + insets.left, 100 + insets.top, size.width + 50, size.height + 10); } }
getgraphics
not how custom painting done. should, in case, override paint
method, , create sure phone call super.paint
before doing custom painting.
getgraphics
returns graphics
context lastly used paint component, discarded on next paint cycle, null or no longer used component
remember, painting uses "painters canvas" approach, is, painting in physical canvas, when paint it, paint on there, not erasing it.
now, if override paint
, find have flickering problem. because canvas
not double buffered
to solve this, should consider user bufferstrategy
, allows not generate multiple buffers paint to, take command of paint process itself
just don't forget clear each buffer before paint it...
java swing canvas awt
No comments:
Post a Comment