java版本更新太快,Java图形更新太快

I have searched the problem, but everyone seems to have problems with graphics updating too slow.

To present my situation:

I have a JFrame which I set to full screen, by using a compatible display mode.

Within the JFrame I have a couple of JPanels and JButtons...

On one of JPanels I am drawing moving objects that need to be updated.

I am updating the graphics like this : validate and repaint the JFrame, then revalidate and repaint the corresponding JPanel.

The graphics are updating too fast. (I need to mention that on the JPanel I am overriding the paintComponent method).

I have tried to use BufferStrategy on the JFrame, however this will prevent me from showing the JPanels and JButtons (have no idea why).

I also take this oppurtunity to ask some of you guys if you can give a clear distinction between paint, validate, repaint, revalidate, invalidate, etc... all the tutorials barely scratch the surface.

解决方案

"The timing mechanism is simply a loop that runs for 5 minutes using System.timeCurrentMillis to count that"

That's not going to work. The loop is ultimately blocking the painting to occur until the loop is complete, if there is no delay.

I suggest you look into using a javax.swing.Timer for the animation. You can see more at How to Use Swing Timers

The basic construct of Timer is as follows

Timer ( int delayInMillis, ActionListener listener )

where delayInMillis is the time in milliseconds to delay between "ticks" and the listener provides the actionPerformed which is called every delayInMillis milliseconds. So ultimately you do something like

Timer timer = new Timer (40, new ActionListener(){

public void actionPerformed(ActionEvent e) {

for (Ball ball: balls) {

ball.move();

repaint();

}

}

});

You can see a complete example here

cHDmY.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值