2015-11-05 69 views
2

我試圖做一個功能,接收圖像和imageicon作爲參數,使其變暗2秒,並將其恢復正常,但我不能讓計時器按計劃工作。使用擺動定時器和ImageIcons

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()調用?有更好的方法來做到這一點?

謝謝你,對不起英文不好。

+0

對於[示例](http://stackoverflow.com/a/2234020/230513)。 – trashgod

+0

因此,我應該只使用start(),刪除stop()並刪除定時器聲明中的偵聽器? – user3111762

+0

不要使用getGraphics,這不是如何進行自定義繪畫 – MadProgrammer

回答

3
public void blinkImage(Image e, ImageIcon f) 

不確定爲什麼你有兩個參數。圖像是圖標中的圖像嗎?

在圖像上繪畫將是永久性的。因此,如果圖像與圖標相同,則無法將圖標恢復到原始狀態。

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

因爲Timer永遠不會觸發,所以您不能在啓動()Timer後立即調用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); 
    } 
}