如何使用Swing计时器来确定整体更新率? [英] How to use Swing Timers to make an overall update rate?

查看:89
本文介绍了如何使用Swing计时器来确定整体更新率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

换句话说,我希望能够以一定的频率运行程序.这是我其他问题摆脱潜在僵局的部分扩展.

In other words I want to be able to run my program at a certain level of hertz. This is a partial extension of my other question Getting out of a potential deadlock.

首先,我在

First of all, I read up a bit on the Java docs by oracle on how to use Swing Timers, however is there a simpler way so I can just, instead of calling wait() to pause the current thread for a certain time and screw up my Swing-based Pong game, to do this?

@Hover_Craft_Full_Of_Eels状态

@Hover_Craft_Full_Of_Eels states

而不是使用会阻塞EDT的while(true)循环,而应使用Swing计时器."

"Rather than a while (true) loop that will block the EDT, use a Swing Timer instead."

但是,我从未与Swing Timers一起工作过,既没有听说过它们,也没有听说过教我的导师.我可以举一个例子,说明如何使用和实现这些Swing计时器吗?

However, I have never worked with Swing Timers, neither have I heard of them nor my mentor who taught me. Can I please have an example of how I should use and implement these Swing Timers?

我发现有关如何使用Swing计时器的解释相当混乱.这就是为什么我可以使用stackoverflow的一些帮助的原因.

I find the explanation of how to use Swing Timers rather confusing. That is why I could use some help from stackoverflow please.

作为旁注:

是否有一种方法可以使图像平滑移动,方法是仅使图像一次移动一个像素,然后使用Swing计时器来使图像移动一个像素的速度或速率更快或更这样慢一点吗?这很大程度上是我想要创建的效果.否则,即使图像以高帧速率跳过像素,您也会看到图像每次移动都像5个像素一样跳跃.

Is there a way to make the images move smoothly by just having the image move one pixel at a time and then use the Swing Timer(s) to make the speed, or rate at which the image moves one pixel, faster or slower this way? This is largely the effect I want to create. Otherwise if the image is skipping pixels, even at a high frame rate, your gonna see the image jumping like 5 pixels each time it moves.

推荐答案

我可以举一个例子,说明如何使用和实现这些Swing计时器吗?

Can I please have an example of how I should use and implement these Swing Timers?

详细了解如何使用摆动计时器

这是示例代码:

int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
      //...Perform a task...
  }
};
new Timer(delay, taskPerformer).start();

此处找到示例代码

有没有一种方法可以使图像一次移动一个像素来使图像平滑移动?

Is there a way to make the images move smoothly by just having the image move one pixel at a time?

示例代码:(以200毫秒的间隔将图像左右向水平移动1px )

private int x = 1;
...
final Image image = ImageIO.read(new File("resources/1.png"));
final JPanel panel = new JPanel() {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image,x,10,null);
    }
};

int delay = 200; // milliseconds
ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        x++;
        if (x > 100) {
            x = 1;
        }
        panel.repaint();
    }
};
new Timer(delay, taskPerformer).start();

这篇关于如何使用Swing计时器来确定整体更新率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆