java更新速度,Java的Paint方法,可笑的刷新速度问题

I'm developing a very simple version of R-Type as work for the university, but despite it works, the craft velocity is a lot of slow, so the movement is ugly and clumsy.

I use the method repaint for refresh the screen, there are others methods or ways best than it?

Paint method at 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()

int y = (getY()

if (getY()>=maxY||getY()==0) {

if (dy==+1) y=y+1;

}

setPosicion(x, y);

}

解决方案

The image fondo should already be scaled to 1200x600.

I am not sure, but is super.paint(g) needed? You might also use paintComponent.

The event handling (you seem to be moving by 1 pixel on key down), must be done correctly. I would have set the direction and speed (1px), and leave it to a swing timer to do the continuous moving.

Repainting best is done resilient/flexible: repaint(20L) (50 frames per second);

events like key-down maybe with EventQueue.invokeLater(new Runnable() { ... });.

Especially you might use repaint with the changed area.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值