如何在 Swing 中淡化图像? [英] How do I fade an image in swing?

查看:31
本文介绍了如何在 Swing 中淡化图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 JPanel 继承的类,上面有一个图像,我想设置一个小动画来显示面板/图像,然后在事件触发时淡出它.

I've got class which inherits from JPanel with an image on it and i want to set up a little animation to display the panel/image and then fade it out when an event fires.

我大概设置了一个线程并触发动画,但我如何实际执行淡入淡出?

I presumably set up a thread and fire off the animation but how do I do actually do the fade?

推荐答案

您可以自己进行线程处理,但使用 Trident 库来处理它.如果你在你的类上创建一个叫做(比如,setOpacity)的 setter,你可以让三叉戟在特定的时间段内将opacity"字段从 1.0 插入到 0.0(这里是 一些关于如何使用 Trident 的文档).

You can do the threading yourself, but it might be easier to use the Trident library to handle it. If you create a setter on your class called (say, setOpacity), you can ask trident to interpolate the "opacity" field from 1.0 to 0.0 over a specific period of time (here's some of the docs on how to use Trident).

在绘制图像时,您可以使用 AlphaComposite 来实现透明度,使用合成的 alpha 参数的更新不透明度"值.有一个 Sun 教程,其中包含一个 alpha 复合示例.

When you're painting the image, you can do the transparency with an AlphaComposite, using the updated "opacity" value for the composite's alpha parameter. There is a Sun tutorial that includes an alpha composite example.

这篇关于如何在 Swing 中淡化图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆