Java 小程序仅以 10 fps 更新

发布于 2024-12-12 20:44:14 字数 754 浏览 0 评论 0原文

我最近的 Java 作业之一(高中课程...)是制作一个小程序,其中绘制了边框,并且一个球在屏幕上移动并在边框上弹跳。我安装了 Fraps,它报告说该小程序仅以 10 fps 的速度运行,这使得动画看起来非常平庸。

我绘制动画的原始方法:

  1. 调用我的方法:paint()中的drawScreen()
  2. 有一个Thread.sleep(1000/frameRate)drawScreen() 中暂停以获取帧速率,该帧速率是从 html
  3. 调用 repaint()

传递的,效果非常好,小程序运行得像一个梦......直到我看到分级工作表,其中说我不得每次都重新绘制屏幕(也许是因为计算机异常缓慢,我的朋友一直抱怨他们的小程序闪烁足以引起眼睛不适,并且很多到处撕裂),我必须画一个球,然后画另一个与背景颜色相同的球来覆盖它,计算坐标,然后重复直到小程序退出

。这是那个

  • 无法调用 repaint()
  • 我的动画上限为 10 fps,
  • 没有可用线程来响应我单击小程序查看器上的关闭或小程序/查看器中的其他任何内容。

有没有办法让我的动画在不使用 repaint() 的情况下以 10 fps 以上的速度运行?

One of my recent Java assignments (High school course...) is to make an applet where a border is drawn and a ball moves around the screen bouncing on the borders. I have Fraps installed and it reports that the applet is only running at a mere 10 fps, which makes the animation look extremely mediocre.

My original way of drawing the animation:

  1. call my method: drawScreen() in paint()
  2. have a Thread.sleep(1000/frameRate) pause in drawScreen() for the frame rate, which is passed from the html
  3. call repaint()

This worked wonderfully well and the applet ran like a dream... Until I saw the grading sheet, where it said that I must not redraw the screen each time (Maybe it's because the computers are unusually slow and my friends have been complaining that their applets flicker enough to cause eye discomfort, and a lot of tearing all over the place), and I have to draw a ball, then draw another ball the same colour of the background to cover it up, calculate the coordinates, and repeat until the applet quits

The problem with this is that

  • repaint() can't be called
  • my animation is capped at 10 fps
  • there's no threads available to respond to me clicking close on the appletviewer, or anything else in the applet/viewer.

Is there a way for my animation to run above 10 fps without using repaint()?

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

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

发布评论

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

评论(2

梦太阳 2024-12-19 20:44:14
  1. 不:
    1. 在顶级容器中执行自定义动画,例如 Applet(/JAppet) 或 Frame(/JFrame )。而是将自定义绘图放入 Canvas/PanelJComponent/JPanel 中,然后将该组件放入顶部级容器。
    2. 在这个世纪使用 AWT 组件。
    3. 重写 Swing 组件的 paint() - 它应该是 paintComponent(Graphics)
  2. 不要在绘画线程上使用 Thread.sleep(n) 。而是使用 Swing Timer 并执行操作..
  3. 调用 repaint()
  1. Don't:
    1. Do custom animation in a top-level container such as an Applet(/JAppet), or Frame(/JFrame). Instead put the custom drawing in a Canvas/Panel or JComponent/JPanel, then put that component into the top-level container.
    2. Use AWT components in this millennium.
    3. Override paint() for the Swing components - it should be paintComponent(Graphics).
  2. Don't use Thread.sleep(n) on the painting thread. Instead use a Swing Timer and have the action..
  3. Call repaint().
青丝拂面 2024-12-19 20:44:14

结果我需要实现双缓冲或在双缓冲浏览器中运行小程序。 AppletViewer 将动画限制为 10 fps,因为它不会自动缓冲小程序,而我一直在其中运行小程序。

Turns out I needed to either implement double buffering or run the applet in a double buffered browser. AppletViewer limited the animation to 10 fps 'cause it doesn't automatically buffer the applets and I was running the applet inside it all this time.

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