Java 在画布上滚动文本
我正在制作一个应用程序,需要在 java.awt.Canvas
- 目前我通过不断调用 repaint()
方法并覆盖 paint 来做到这一点()
中的方法Canvas
每次调用该方法时都会调整位置,然后在给定位置绘制字符串。
这是可行的,但它确实会消耗 CPU 一些如此简单的东西。有没有更好的硬件加速方法来完成任务?我尝试过使用 VolatileImage,但由于某种原因,性能明显变差。
我需要使用 Canvas
而不是 JPanel
- 我在同一个项目中使用 VLCJ,并且它需要一个重量级组件来播放视频。
I'm making an application where I need to scroll some text on a java.awt.Canvas
- at the moment I'm doing this by constantly calling the repaint()
method, and overriding the paint()
method in Canvas
to adjust the position each time the method is called and then draw the string at the given position.
This works, but it really eats through CPU something silly for something so simple. Is there a better, hardware accelerated way to do the task? I've tried using VolatileImage
but for some reason that made the performance noticeably worse.
I need to use Canvas
not JPanel
- I'm using VLCJ in the same project, and it needs a heavyweight component on which to play the video.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也许可以使用双缓冲和页面翻转 在这种情况下。
附录:您可能会考虑将全文预渲染到深度兼容的
BufferedImage
中;drawImage()
做得更好,它不需要缩放或改变深度。You may be able to use Double Buffering and Page Flipping in this context.
Addendum: You might look at pre-rendering the full text into a depth-compatible
BuffreredImage
;drawImage()
does better of it doesn't have to scale or change depths.