FadingPanel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:2k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: FadingPanel.java,v 1.1 2005/05/25 23:13:25 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  */
  7. package org.jdesktop.demo.login.romain;
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.awt.Rectangle;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import javax.swing.JComponent;
  14. import javax.swing.Timer;
  15. public class FadingPanel extends JComponent implements ActionListener {
  16.     private Timer ticker = null;
  17.     private int alpha = 0;
  18.     private int step;
  19. private FadeListener fadeListener;
  20.     public FadingPanel(FadeListener fadeListener) {
  21. this.fadeListener = fadeListener;
  22. }
  23. public void setVisible(boolean visible) {
  24.         super.setVisible(visible);
  25.         if (visible) {
  26.             if (ticker != null) {
  27.                 ticker.stop();
  28.             }
  29.             alpha = 0;
  30.             step = 25;
  31.             ticker = new Timer(50, this);
  32.             ticker.start();
  33.         } else {
  34.             if (ticker != null) {
  35.                 ticker.stop();
  36.                 ticker = null;
  37.             }
  38.         }
  39.     }
  40.     protected void paintComponent(Graphics g) {
  41.         super.paintComponent(g);
  42.         g.setColor(new Color(255, 255, 255, alpha));
  43.         Rectangle clip = g.getClipBounds();
  44.         g.fillRect(clip.x, clip.y, clip.width, clip.height);
  45.     }
  46.     
  47.     public void switchDirection() {
  48.      step = -step;
  49.      ticker.start();
  50.     }
  51.     public void actionPerformed(ActionEvent e) {
  52.         alpha += step;
  53.         if (alpha >= 255) {
  54.             alpha = 255;
  55.             ticker.stop();
  56.             fadeListener.fadeOutFinished();
  57.         } else if (alpha < 0) {
  58.          alpha = 0;
  59.          ticker.stop();
  60.          fadeListener.fadeInFinished();
  61.         }
  62.         repaint();
  63.     }
  64. }