代码之家  ›  专栏  ›  技术社区  ›  blank

如何在swing中淡入图像?

  •  23
  • blank  · 技术社区  · 16 年前

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

    我可能会设置一个线程并触发动画,但我如何做真正的褪色?

    3 回复  |  直到 9 年前
        1
  •  14
  •   Ash    16 年前

    您可以自己做线程,但是使用 Trident 图书馆来处理。如果在类上创建一个名为 setOpacity ,您可以要求Trident在特定时间段内将“不透明度”字段从1.0插值到0.0(以下是 some of the docs 关于如何使用三叉戟)。

    在绘制图像时,可以使用 AlphaComposite ,使用组合的alpha参数的更新“不透明度”值。有一个Sun教程包括 alpha composite example .

        2
  •  12
  •   trashgod    9 年前

    下面是一个使用alpha透明度的示例。你可以使用 this composite tool 查看使用不同颜色、模式和字母的结果。

    image

    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);
                }
            });
        }
    }
    
        3
  •  6
  •   Adamski    16 年前

    有一些描述图像透明度的有用信息 here .

    你的方法是这样的:

    • 定义摆动 Timer 开枪 ActionEvent 每N毫秒在事件分派线程上。
    • 添加一个 ActionListener 到 计时器 应该叫 repaint() 对你 Component 包含 Image .
    • 重写 成分 的 paintComponent(Graphics) 方法执行以下操作:
      • 铸造 Graphics 对象到 Graphics2D .
      • 设置一个 AlphaComposite 上 图形2D 使用 setComposite . 这将控制透明度的级别。
      • 画出图像。

    对于淡出动画的每次迭代,您将更改 字母合成 使图像更透明。