如何淡入/淡出 Java 图形?
假设我有一个 Java Graphics 对象,并且我想在其上绘制一条线或一个矩形。当我发出绘图命令时,它立即出现在屏幕上。我只是想知道如何才能以淡入/淡出效果来实现这一进步,与在 Javascript 中实现的效果相同。
有什么想法吗?
非常感谢您提前提供的帮助和建议!
Say if I have a Java Graphics object, and I want to draw a line or a rectangle on it. When I issue the command to draw, it appears on screen immediately. I just wonder how could I make this progress as a fade-in / fade-out effect, same as what you can achieve in Javascript.
Any thoughts?
Many thanks for the help and suggestions in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试一遍又一遍地绘制图像,但使用不同的不透明度(alpha)值。从0(完全透明)开始,逐渐增加到1(不透明),以产生淡入效果。这是一些可能有帮助的测试代码:
You could try painting the image over and over again but with a different opacity (alpha) value. Start from 0 (completely transparent) and gradually increase to 1 (opaque) in order to produce a fade-in effect. Here is some test code which might help:
查看 AlphaComposite 类透明度,以及基于 Swing 的 Timer 类定时。
Java 2D API 示例程序 有演示。在“复合”标题下,显示如何将它们结合在一起。
Have a look at the AlphaComposite class for the transparency, and the Swing based Timer class for timing.
The Java 2D API Sample Programs has demos. under the Composite heading that show how to tie them together.
查看 Java 计时框架。
Take a look at the Java Timing Framework.