java 如何在 Swing 中淡化图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2228735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I fade an image in swing?
提问by blank
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.
我有一个从 JPanel 继承的类,上面有一个图像,我想设置一个小动画来显示面板/图像,然后在事件触发时将其淡出。
I presumably set up a thread and fire off the animation but how do I do actually do the fade?
我大概设置了一个线程并启动动画,但我如何实际执行淡入淡出?
采纳答案by Ash
You can do the threading yourself, but it might be easier to use the Tridentlibrary 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 docson how to use Trident).
您可以自己进行线程处理,但使用Trident库来处理它可能更容易。如果你在你的类上创建一个叫做(比如,setOpacity)的 setter ,你可以让三叉戟在特定的时间段内将“不透明度”字段从 1.0 插入到 0.0(这里有一些关于如何使用 Trident的文档)。
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.
当您绘制图像时,您可以AlphaComposite使用合成的 alpha 参数的更新的“不透明度”值来实现透明度。有一个包含alpha 复合示例的 Sun 教程。
回答by trashgod
Here's an example using alpha transparency. You can use this composite toolto see the result of using different colors, modes and alphas.
这是使用 alpha 透明度的示例。您可以使用此合成工具查看使用不同颜色、模式和 alpha 的结果。
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class AlphaTest extends JPanel implements ActionListener {
private static final Font FONT = new Font("Serif", Font.PLAIN, 32);
private static final String STRING = "Mothra alert!";
private static final float DELTA = -0.1f;
private static final Timer timer = new Timer(100, null);
private float alpha = 1f;
AlphaTest() {
this.setPreferredSize(new Dimension(256, 96));
this.setOpaque(true);
this.setBackground(Color.black);
timer.setInitialDelay(1000);
timer.addActionListener(this);
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(FONT);
int xx = this.getWidth();
int yy = this.getHeight();
int w2 = g.getFontMetrics().stringWidth(STRING) / 2;
int h2 = g.getFontMetrics().getDescent();
g2d.fillRect(0, 0, xx, yy);
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_IN, alpha));
g2d.setPaint(Color.red);
g2d.drawString(STRING, xx / 2 - w2, yy / 2 + h2);
}
@Override
public void actionPerformed(ActionEvent e) {
alpha += DELTA;
if (alpha < 0) {
alpha = 1;
timer.restart();
}
repaint();
}
static public void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setLayout(new GridLayout(0, 1));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new AlphaTest());
f.add(new AlphaTest());
f.add(new AlphaTest());
f.pack();
f.setVisible(true);
}
});
}
}
回答by Adamski
There is some useful information describing image transparency here.
没有描述图像的透明度一些有用的信息在这里。
Your approach would be something like this:
你的方法是这样的:
- Define a Swing
Timerto fire anActionEventon the Event Dispatch thread every N milliseconds. - Add an
ActionListenerto theTimerwhich should callrepaint()on yourComponentcontaining theImage. - Override the
Component'spaintComponent(Graphics)method to do the following:- Cast the
Graphicsobject to aGraphics2D. - Set an
AlphaCompositeon theGraphics2DusingsetComposite. This controls the level of transparency. - Draw the image.
- Cast the
- 定义一个 Swing
Timer,ActionEvent每 N 毫秒在 Event Dispatch 线程上触发一次。 - 将一个添加
ActionListener到Timer应该调用repaint()您的Component包含Image. - 覆盖
Component的paintComponent(Graphics)方法以执行以下操作:- 将
Graphics对象投射到Graphics2D. AlphaComposite在Graphics2Dusing上设置setComposite。这控制了透明度。- 绘制图像。
- 将
For each iteration of your fade-out animation you would change the values of the AlphaCompositeto make the image more transparent.
对于淡出动画的每次迭代,您将更改 的值AlphaComposite以使图像更透明。


