Java 小程序仅以 10 fps 更新
我最近的 Java 作业之一(高中课程...)是制作一个小程序,其中绘制了边框,并且一个球在屏幕上移动并在边框上弹跳。我安装了 Fraps,它报告说该小程序仅以 10 fps 的速度运行,这使得动画看起来非常平庸。
我绘制动画的原始方法:
- 调用我的方法:
paint()
中的drawScreen()
- 有一个
Thread.sleep(1000/frameRate)
在drawScreen()
中暂停以获取帧速率,该帧速率是从html
- 调用
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:
- call my method:
drawScreen()
inpaint()
- have a
Thread.sleep(1000/frameRate)
pause indrawScreen()
for the frame rate, which is passed from thehtml
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Applet
(/JAppet
) 或Frame
(/JFrame )。而是将自定义绘图放入
Canvas
/Panel
或JComponent
/JPanel
中,然后将该组件放入顶部级容器。paint()
- 它应该是paintComponent(Graphics)
。Timer
并执行操作..repaint()
。Applet
(/JAppet
), orFrame
(/JFrame
). Instead put the custom drawing in aCanvas
/Panel
orJComponent
/JPanel
, then put that component into the top-level container.paint()
for the Swing components - it should bepaintComponent(Graphics)
.Thread.sleep(n)
on the painting thread. Instead use a SwingTimer
and have the action..repaint()
.结果我需要实现双缓冲或在双缓冲浏览器中运行小程序。 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.