tra*_*god 13
这是使用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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
没有描述图像的透明度一些有用的信息在这里.
你的方法是这样的:
Timer
以ActionEvent
每N毫秒触发一次事件调度线程.ActionListener
到Timer
应该叫repaint()
上你的Component
包含Image
.Component
的paintComponent(Graphics)
方法做到以下几点:
Graphics
对象转换为Graphics2D
.AlphaComposite
在上Graphics2D
使用setComposite
.这可以控制透明度.对于淡出动画的每次迭代,您都会更改其值AlphaComposite
以使图像更透明.