events - Java GUI - Moving a circle with no "footprints" -
when run program , move circle, appears if i'm drawing paintbrush in paint. i'm not quite sure did make this, or can make stop. highly appreciated.
here code:
import java.awt.graphics; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.event.keyevent; import javax.swing.jframe; import javax.swing.timer; import javax.swing.jpanel; import java.awt.event.keylistener; public class movingcar extends jpanel implements actionlistener, keylistener { timer tm = new timer(5, this); int x = 0, y = 0, velx = 0, vely = 0; public movingcar() { tm.start(); addkeylistener(this); setfocusable(true); setfocustraversalkeysenabled(false); } protected void paintcomponent (graphics g) { super.paintcomponents(g); g.drawoval(x, y, 50, 50); } public void actionperformed(actionevent e){ x = x + velx; y = y + vely; repaint(); } public void keypressed(keyevent e){ int c = e.getkeycode(); if (c == keyevent.vk_down) { velx = -1; vely = 0; } if (c == keyevent.vk_up) { velx = 1; vely = 0; } } public void keytyped(keyevent e){} public void keyreleased(keyevent e){ if (x < 0) { velx = 0; x = 0; } if (x > 600) { velx = 0; x = 0; } repaint(); vely = 0; velx = 0; } public static void main(string[] args) { movingcar o = new movingcar(); jframe jf = new jframe(); jf.settitle("circle move"); jf.setsize(600,400); jf.setdefaultcloseoperation(jframe.exit_on_close); jf.add(o); jf.setvisible(true); } }
you're calling super.paintcomponents(g);
instead of super.paintcomponent(g);
Comments
Post a Comment