Java 使用swing定时器和图像图标

Java 使用swing定时器和图像图标,java,swing,timer,awt,Java,Swing,Timer,Awt,我试图使一个功能,接收图像和图像图标作为参数,变暗2秒,并把它恢复正常,但我不能使计时器的工作计划 public void blinkImage(Image e, ImageIcon f) { ActionListener listener = new ActionListener(){ public void actionPerformed(ActionEvent event){ Graphics2D g2 = (Graphics2D)

我试图使一个功能,接收图像和图像图标作为参数,变暗2秒,并把它恢复正常,但我不能使计时器的工作计划

public void blinkImage(Image e, ImageIcon f) {
    ActionListener listener = new ActionListener(){
          public void actionPerformed(ActionEvent event){
              Graphics2D g2 = (Graphics2D) e.getGraphics();
              g2.setColor(new Color(0, 0, 0, 50));
              g2.fillRect(0, 0, f.getIconWidth(), f.getIconHeight());
          }
        };
        Timer displayTimer = new Timer(2000, listener);
        displayTimer.start();
        displayTimer.stop();
}
OBS:在此呼叫之后,主窗口中将出现一个设置图标(f),将其恢复正常。我的问题是:我应该把start()和stop()调用放在哪里?有更好的方法吗

谢谢,对不起,英语不好

public void blinkImage(Image e, ImageIcon f)
不确定为什么有两个参数。该图像是否为图标中的图像

在图像上绘画将是永久性的。因此,如果它是与图标相同的图像,则无法将图标恢复到其原始状态

displayTimer.start();
displayTimer.stop();
启动()计时器后不能立即调用stop(),因为计时器永远不会启动。因此,您只需要开始()

由于您只想让计时器启动一次,因此只需使用:

timer.setRepeats( false );
那么你就不必担心停止计时器了

一种方法是创建具有两种状态的自定义图标。然后可以切换图标的状态:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DarkIcon implements Icon
{
    private Icon icon;
    private Color color;
    private boolean dark = false;

    public DarkIcon(Icon icon, Color color)
    {
        this.icon = icon;
        this.color = color;
    }

    public void setDark(boolean dark)
    {
        this.dark = dark;
    }

    @Override
    public int getIconWidth()
    {
        return icon.getIconWidth();
    }

    @Override
    public int getIconHeight()
    {
        return icon.getIconHeight();
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y)
    {
        icon.paintIcon(c, g, x, y);

        if (dark)
        {
            g.setColor(color);
            g.fillRect(x, y, getIconWidth(), getIconHeight());
        }
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI()
    {
        Icon icon = new ImageIcon("mong.jpg");
        DarkIcon darkIcon = new DarkIcon(icon, new Color(0, 0, 0, 128));
        JLabel label = new JLabel( darkIcon );

        Action blink = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e2)
            {
                darkIcon.setDark( false );
                label.repaint();
            }
        };

        Timer timer = new Timer(2000, blink);
        timer.setRepeats( false );

        JButton button = new JButton("Blink Icon");
        button.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                darkIcon.setDark(true);
                label.repaint();
                timer.restart();
            }
        });

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(label);
        f.add(button, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo( null );
        f.setVisible(true);
    }
}

那么,我应该在计时器声明中使用start()、删除stop()和删除侦听器吗?不要使用getGraphics,这不是自定义绘制的方式