java - JFrame repainting image blinking -
i having issue command super.paintcomponents(g); when updating jframe. issue having image loading blinking while code running. pretty sure code double buffering , have not found helpful resource online solve issue. first question please tolerate formatting errors.
here entire code (snippets below):
public class game extends jframe implements keylistener, mousemotionlistener { int mousex, mousey; public static arraylist<images> images = new arraylist<images>(); public static string lastkeypressed = null; public game() { this.addmousemotionlistener(this); this.addkeylistener(this); } public void mousedragged(mouseevent e) { mousex = e.getx(); mousey = e.gety(); } public void mousemoved(mouseevent e) { mousex = e.getx(); mousey = e.gety(); } public void keypressed(keyevent e) { if (e.getkeycode() == e.vk_left) { lastkeypressed = "left"; } else if (e.getkeycode() == e.vk_right) { lastkeypressed = "right"; } else if (e.getkeycode() == e.vk_up) { lastkeypressed = "up"; } else if (e.getkeycode() == e.vk_down) { lastkeypressed = "down"; } } public void keyreleased(keyevent e) { } public void keytyped(keyevent e) { } @override public void paint(graphics g) { super.paintcomponents(g); (int = 0; < images.size(); i++) { g.drawimage(images.get(i).img, images.get(i).xpos, images.get(i).ypos, null); } } public static void main(string[] args) throws ioexception, interruptedexception { game frame = new game(); dimension dim = new dimension(800, 600); frame.setpreferredsize(dim); frame.setsize(dim); frame.setresizable(false); frame.settitle("game"); frame.setlocationrelativeto(null); frame.setvisible(true); images test = new images("unnamed.png"); images.add(test); while (true) { if (lastkeypressed == "left") { test.xpos -= 5; lastkeypressed = null; } else if (lastkeypressed == "right") { test.xpos += 5; lastkeypressed = null; } else if (lastkeypressed == "up") { test.ypos -= 5; lastkeypressed = null; } else if (lastkeypressed == "down") { test.ypos += 5; lastkeypressed = null; } frame.repaint(); thread.sleep(100); } } }
here images class:
public class images { string name; bufferedimage img; int xpos; int ypos; public images (string name) throws ioexception{ name = name; xpos = 40; ypos = 90; system.out.println(name); system.out.println("path: " + game.class.getresource(name)); url file = getclass().getclassloader().getresource(name); img = imageio.read(file); } }
what want see following:
my paint method:
@override public void paint(graphics g) { super.paintcomponents(g); (int = 0; < images.size(); i++) { g.drawimage(images.get(i).img, images.get(i).xpos, images.get(i).ypos, null); } }
and block of code in main class running game:
images test = new images("unnamed.png"); images.add(test); while (true) { if (lastkeypressed == "left") { test.xpos -= 5; lastkeypressed = null; } else if (lastkeypressed == "right") { test.xpos += 5; lastkeypressed = null; } else if (lastkeypressed == "up") { test.ypos -= 5; lastkeypressed = null; } else if (lastkeypressed == "down") { test.ypos += 5; lastkeypressed = null; } frame.repaint(); thread.sleep(100); }
(i added thread.sleep because seemed decrease rate @ blinking occurred)
here gif of happening (i used first google result 'test' image): http://i.imgur.com/wjlcsvu.gif?1
i appreciate , general suggestions better code. thank you.
don't override paint
of top level containers jframe
there aren't double buffered, instead, use jpanel
, override paintcomponent
method. jpanel
double buffered default.
then, add panel ever container want
don't forget call it's super
method (super.paintcomponent
) before custom painting.
take closer @ painting in awt , swing , performing custom painting more details how painting works in swing
this 1 reason why shouldn't (extend jframe
and) override paint
of top level containers, take at
- how can set in midst?
- graphics rendering in title bar
- java jframe .setsize(x, y) not working?
- how exact middle of screen, when re-sized
for more
Comments
Post a Comment