swing - Problems with Java's Paint method, ridiculous refresh velocity -
i'm developing simple version of r-type work university, despite works, craft velocity lot of slow, movement ugly , clumsy. use method repaint refresh screen, there others methods or ways best it?
paint method @ main panel
@override public void paint(graphics g) { super.paint(g); graphics2d g2 = (graphics2d) g; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2.drawimage(fondo, 0, 0,1200,600,this); pj.paint(g2); g2d=g2; }
pj's paint method
public void paint(graphics2d g) { g.drawimage(imagen,x,y,this); }
pj's move method
public void move (keyevent e) { int dx = 0; int dy = 0; int code = e.getkeycode(); switch (code) { case keyevent.vk_q: dy-=1; break; case keyevent.vk_a: dy+=1; break; case keyevent.vk_p: dx+=1; break; case keyevent.vk_o: dx-=1; break; } int x = (getx()<maxx&&getx()!=0) ? getx()+dx : getx(); int y = (gety()<maxy&&gety()!=0) ? gety()+dy : gety(); if (gety()>=maxy||gety()==0) { if (dy==+1) y=y+1; } setposicion(x, y); }
- the image
fondo
should scaled 1200x600. - i not sure,
super.paint(g)
needed? might usepaintcomponent
.
the event handling (you seem moving 1 pixel on key down), must done correctly. have set direction , speed (1px), , leave swing timer continuous moving.
repainting best done resilient/flexible: repaint(20l)
(50 frames per second); events key-down maybe eventqueue.invokelater(new runnable() { ... });
.
especially might use repaint changed area.
Comments
Post a Comment