2013-01-21 166 views
3

我想製作一個擴展爲JPanel的面板,當它變得可見時,它開始變得更透明,更透明,最終消失。我的代碼有什麼問題?如何使JPanel變得更透明,更透明並最終消失?

public class BaloonPanel extends JPanel 
{ 

private float transparency = 1f; 
Timer timer; 

public BaloonPanel() 
{ 

    setBackground(Color.white); 
    ActionListener action = new ActionListener() 
    { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      transparency = transparency - 0.01f; 

      if (transparency < 0.0f) 
      { 
       timer.stop(); 
      } 
      repaint(); 
     } 
    }; 

    timer = new Timer(100, action); 
    timer.start(); 
} 

@Override 
public void paint(Graphics g) 
{ 
    Graphics2D g2 = (Graphics2D) g.create(); 
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency)); 
    super.paint(g2); 
    g2.dispose(); 
} 

}

+1

你想要你的代碼做什麼?你的代碼實際上做了什麼?描述兩者之間的不匹配。幫助我們來幫助你。 – rossum

+0

@rossum正如我上面描述的,我希望我的面板變得更透明,更多,最後消失。我的代碼不會繼續到我想要的點,它會導致面板變得更透明(僅)... – Soheil

回答

3

因爲BallonPanel是不透明的,重繪管理器沒有費心去畫它的下面。這是對油漆工藝的優化,爲什麼油漆不需要塗漆。

您需要「說服」重繪管理員在組件下面繪製,同時仍然繪製其背景。

BallonPanel設置爲透明(setOpaque(false))並更新paint方法以填充背景。

public class FadePane { 

    public static void main(String[] args) { 
     new FadePane(); 
    } 

    public FadePane() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.getContentPane().setBackground(Color.BLUE); 
       frame.setBackground(Color.BLUE); 
       frame.add(new BaloonPanel()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 

      } 
     }); 
    } 

    public class BaloonPanel extends JPanel { 

     private float transparency = 1f; 
     Timer timer; 

     public BaloonPanel() { 

      setBackground(Color.white); 
      ActionListener action = new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        transparency = transparency - 0.1f; 

        if (transparency < 0.1f) { 
         transparency = 0; 
         timer.stop(); 
        } 
        invalidate(); 
        repaint(); 
       } 
      }; 

      timer = new Timer(100, action); 
      timer.setRepeats(true); 

      setOpaque(false); 

      final JButton fade = new JButton("Fade"); 
      fade.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        timer.start(); 
        fade.setEnabled(false); 
       } 
      }); 

      setLayout(new GridBagLayout()); 
      add(fade); 
     } 

     @Override 
     public void paint(Graphics g) { 
      Graphics2D g2 = (Graphics2D) g.create(); 
      System.out.println(transparency); 
      g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency)); 
      g2.setColor(getBackground()); 
      g2.fillRect(0, 0, getWidth(), getHeight()); 
      super.paint(g2); 
      g2.dispose(); 
     } 
    } 
} 
+0

美麗的音符! thanx ... – Soheil

+0

用於'AlphaComposite'的+1;請參閱laso [示例](http://stackoverflow.com/a/2124507/230513),它會改變_saturation_。 – trashgod