Swing 中的流畅动画

发布于 2024-11-23 19:59:35 字数 349 浏览 1 评论 0原文

如何以客户端监视器的最大刷新率制作 JPanel 动画?

我希望发生的是 paintComponent 仅在上次更新后调用。我可以通过使用 Timer 每隔 (1000 / 60) 毫秒安排一次 repaint() 来完成此操作,但我只是猜测刷新率是多少。

实际上,我希望连续调用 repaint 。但是,如果我每秒调用 repaint 200 次,并且 EDT 不忙,它就会每秒执行 paintComponent 200 次,这对处理器时间的利用效率很低,可能会产生不利影响应用程序的其余性能。

How do I animate a JPanel at the maximum refresh rate of the client's monitor?

What I would like to happen is that paintComponent is only called after the last update. I could do this by using a Timer to schedule repaint() every (1000 / 60) ms, but I'm just guessing at what the refresh rate is.

Effectively I'd like repaint to be called continuously. But if I call repaint 200 times per second and the EDT isn't busy, it executes paintComponent 200 times per second, which is inefficient use of processor time and could aversely impact the rest of the application's performance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悟红尘 2024-11-30 19:59:35

我不确定以显示器的刷新率重新绘制是个好主意。但是,如果您想知道刷新率是多少,您可以使用这段代码(无耻地从互联网上复制)。

GraphicsEnvironment ge = GraphicsEnvironment
        .getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();

for (int i = 0; i < gs.length; i++) {
    DisplayMode dm = gs[i].getDisplayMode();

    // Get refresh rate in Hz
    int refreshRate = dm.getRefreshRate();
    if (refreshRate == DisplayMode.REFRESH_RATE_UNKNOWN) {
        // Unknown rate
    } else {
        System.out.println(refreshRate);
    }
}

I am not sure repainting at the monitor's refresh rate is a good idea. But if you are looking to find out what the refresh rate is you can use this piece of code (shamelessly copied from the interwebs).

GraphicsEnvironment ge = GraphicsEnvironment
        .getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();

for (int i = 0; i < gs.length; i++) {
    DisplayMode dm = gs[i].getDisplayMode();

    // Get refresh rate in Hz
    int refreshRate = dm.getRefreshRate();
    if (refreshRate == DisplayMode.REFRESH_RATE_UNKNOWN) {
        // Unknown rate
    } else {
        System.out.println(refreshRate);
    }
}
凉宸 2024-11-30 19:59:35

我同意 @little bunny foo foo 并且您也必须使用操作系统延迟进行计算,该值取决于硬件和硬件。 SW,那么我的WinXp的值约为63ms,通过超频此频率,您会从RepaintManager得到错误,例如JTable是非常敏感的JComponent,请阅读有关Swing中绘画的更多信息 paintImmediately(), repaint() 通过默认编辑创建 EDT

:消除对 repaint() 的所有可能的疑虑;看这里绘画和javax.swing.Timer

I'm agreed with @little bunny foo foo and you have to calculate with OS Latency too, this value depends of HW & SW, then my WinXp has value around 63ms, by overlocking this frequency you get Error from RepaintManager, for example JTable is very sensitive JComponents, please read more about painting in Swing paintImmediately(), repaint() create EDT by defalut

EDIT: to blow away all possible doubts about repaint(); look here painting and javax.swing.Timer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文