简体   繁体   English

Java Swing或AWT重绘频率

[英]Java Swing or AWT repaint frequency

I tried to make a simple game in Java and ended up with this code 我试图用Java做一个简单的游戏,最终得到了这段代码

@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    ball.paint(g2d);
}

...

while (true) {
    repaint();
    Thread.sleep(10);
}

It redraws not frequently enough. 重绘频率不够高。 But if I move my mouse on top of the window it starts to repaint much more frequently. 但是,如果我将鼠标移到窗口上方,它将开始更频繁地重新绘制。 Pressing buttons on keyboard speeds up too. 按下键盘上的按钮也可以加快速度。

I'm using Arch with i3wm . 我正在将Arch与i3wm一起i3wm

窗口

Don't trust guides on the internet. 不要相信互联网上的指南。 Think yourself sometimes. 有时候想想你自己。

It was the guide with mistake. 这是错误的指导。 The problem is that the algorithm is wrong. 问题在于算法错误。 We just have to draw more frequently than update our world. 我们只需要更频繁地绘画,而不是更新世界。

Here is a stupid implementation of this. 这是一个愚蠢的实现。 It should may be really wrong in terms of concurrency. 就并发而言,这可能确实是错误的。

Timer timer1 = new Timer(1, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        game.repaint();
    }
});
timer1.start();

Timer timer2 = new Timer(10, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        game.move();
    }
});
timer2.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM