提问者:小点点

使用摆动计时器和ImageIcons


我正在尝试制作一个接收图像和图像图标作为参数的函数,将其变暗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:在这个调用之后,在主窗口中会出现一个setIcon(f ),使它恢复正常。我的问题是:我应该把start()和stop()调用放在哪里?有更好的方法吗?

谢谢你,也很抱歉英语不好。


共1个答案

匿名用户

public void blinkImage(Image e, ImageIcon f)

不确定为什么有两个参数。图像是图标中的图像吗?

在图像上绘画将是永久的。所以如果它和图标是同一个图像,你就不能把图标恢复到原来的状态。

displayTimer.start();
displayTimer.stop();

您不能在启动()计时器后立即调用stop(),因为计时器永远不会触发。所以你只需要start()。

由于您只希望定时器触发一次,因此只需使用:

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);
    }
}