Animated TextField : Animation « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » 2D Graphics GUI » Animation 




Animated TextField
    

/**
 *   Arsenal Real-Time Collaboration Server Project
 *   Copyright (C) 2003  Michael Burnside, Arsenal Project
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ***************************************************************** 
 *    $Header: /cvs/coolcollaborator/prod/coolcollaborator/prod/util/src/com/arsenal/util/AnimatedTextField.java,v 1.1.1.1 2006/01/12 04:54:02 mburnside Exp $ 
 *     
 *    File: $Workfile: AnimatedTextField.java $ 
 *     
 *    Description: 
 *     
 *    
 *     
 *    @author      [email protected] (arsenal-1) 
 *    @author      Michael Burnside 
 *    @version     %I%, %G% 
 *    @since       1.0 
 *     
 */

import java.io.File;

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


public class AnimatedTextField extends JPanel implements FocusListener {
        
        Font font = new Font("Arial", Font.PLAIN, 10);
        Color darkSlateGray = new Color(49,79,79);
        Color dimGray = new Color(105,105,105);
        Color slateGray = new Color(112,138,144);
        Color lightSlateGray = new Color(119,136,153);
        Color gray = new Color(190,190,190);
        Color lightGray = new Color(211,211,211);
        Color[] colors = Color.black, darkSlateGray, dimGray, slateGray,
                lightSlateGray, gray, lightGray, Color.white };
        
  private JTextField textField = new JTextField();
        private JLabel label = new JLabel();
        private int fadeSpeed = 50;
        private boolean animate = false;
        private boolean allowFade = false;
        private boolean fadeOut = false;
        private boolean fadeIn = false;
  private Color foreground = Color.black;
  public void setDefaultForeground(Color foreground) { this.foreground = foreground; }

        private boolean allowAnimate = true// we will animate
  
  public AnimatedTextField(String labelText, boolean useBgImage, Color defaultForeground, boolean allowAnimate) { 
    this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
          label.setText(labelText);
    textField.addFocusListener(this);
          label.setBackground(colors[0]);
    textField.setBackground(colors[0]);
    this.foreground = defaultForeground;
          this.allowAnimate = allowAnimate;
          label.setForeground(foreground);
    textField.setForeground(foreground);
          add(label);
    add(textField);
          if(allowAnimate
            runThread.start();
          if(allowFade) {
            fadeInThread.start();
            fadeOutThread.start();
          }
          setOpaque(false);
  }
  
  public AnimatedTextField(String labelText, boolean useBgImage, Color defaultForeground, boolean allowAnimate, boolean allowFade) { 
      this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
            label.setText(labelText);
      textField.addFocusListener(this);
            label.setBackground(colors[0]);
      textField.setBackground(colors[0]);
      this.foreground = defaultForeground;
            this.allowAnimate = allowAnimate;
            this.allowFade = allowFade;
            label.setForeground(foreground);
      textField.setForeground(foreground);
            add(label);
      add(textField);
            if(allowAnimate
              runThread.start();
            if(allowFade) {
              fadeInThread.start();
              fadeOutThread.start();
            }
            setOpaque(false);
    
  
    public void addActionListener(ActionListener actionListener) {
        textField.addActionListener(actionListener);
     }
        
        
        private Thread runThread = new Thread(
          new  Runnable() {    
            public void run() {
              while(true) {
                try runThread.sleep(100)catch(Exception e) { }       
                if(animate) {
                  for(int i = 0; i < colors.length; i++) {
                    label.setForeground(colors[i]);
                    try runThread.sleep(20)catch(Exception e) { }
                  }
                  try runThread.sleep(100)catch(Exception e) { }
                  for(int i = colors.length -; i >= 0; i--) {
                    label.setForeground(colors[i]);      
                    try runThread.sleep(20)catch(Exception le) { }
                  }
                // end if animate
                label.setForeground(foreground)
              //end while
            }
          }
        );
     
        
        private Thread fadeInThread = new Thread(
          new  Runnable() {    
            public void run() {
               while(true) {
                 if(fadeIn) {     
                   fadeIn();
                   fadeIn = false;
                 }
                 try fadeInThread.sleep(fadeSpeed)catch(Exception le) { }
               }
            }
          }
        );
        
        
        private Thread fadeOutThread = new Thread(
          new  Runnable() {    
            public void run() {
               while(true) {
                 if(fadeOut) {
                   fadeOut();
                   fadeOut = false;
                 }
                 try fadeOutThread.sleep(fadeSpeed)catch(Exception le) { }
               }
            }
          }
        );
        
  public void focusGained(FocusEvent e
  {
           //if(allowAnimate) fadeIn();
           animate = true;
           if(allowFade) {
             fadeIn = true;
             fadeOut = false;
           }
           else {
             textField.setBackground(Color.white);
             textField.setForeground(Color.black);
           }
  }

  public void focusLost(FocusEvent e
  {
           //if(allowAnimate) fadeOut();
           animate = false;
           if(allowFade) {
             fadeOut = true;
             fadeIn = false;
             try runThread.sleep(50)catch(Exception le) { }
           }
           else {
             textField.setBackground(Color.black);
             textField.setForeground(Color.white);
           }
  }
        /**
         *
         * disable the components on this panel
         *
         */
  public void setInputsDisabled() 
  {
           setInputsEnabled(false);
  }
        /**
         *
         * enable the components on this panel
         *
         */
  public void setInputsEnabled() 
  {
           setInputsEnabled(true);
  }

        /**
         *
         * enable or disable the components on this panel
         *
         @param enable or disable
         *
         */
  public void setInputsEnabled(boolean enable
  {
     textField.setEnabled(enable);
     //label.setEnabled(enable);
           if(!enableanimate = false;
  }
        /**
         *
         * are the inputs enabled
         *
         * @returns boolean
         *
         */
  public boolean areInputsEnabled() 
  {
     return textField.isEnabled();
  }        
        /**
         *
         * Sets the label's maximum size
         *
         @param width width, in pixels
         @param height height, in pixels
         *
         */         
        public void setLabelMaximumSize(int width, int height) {
           label.setMaximumSize(new Dimension(width, height));       
        }
        /**
         *
         * Sets the label's minimum size
         *
         @param width width, in pixels
         @param height height, in pixels
         *
         */
        public void setLabelMinimumSize(int width, int height) {
           label.setMinimumSize(new Dimension(width, height));       
        }
        /**
         *
         * Sets the label's preferred size
         *
         @param width width, in pixels
         @param height height, in pixels
         *
         */        
        public void setLabelPreferredSize(int width, int height) {
           label.setPreferredSize(new Dimension(width, height));       
        }
        /**
         *
         * Sets the text field's maximum size
         *
         @param width width, in pixels
         @param height height, in pixels
         *
         */
        public void setTextFieldMaximumSize(int width, int height) {
           textField.setMaximumSize(new Dimension(width, height));       
        }
        /**
         *
         * Sets the text field's minimum size
         *
         @param width width, in pixels
         @param height height, in pixels
         *
         */
        public void setTextFieldMinimumSize(int width, int height) {
           textField.setMinimumSize(new Dimension(width, height));       
        }
        /**
         *
         * Sets the text field's preferred size
         *
         @param width width, in pixels
         @param height height, in pixels
         *
         */        
        public void setTextFieldPreferredSize(int width, int height) {
           textField.setPreferredSize(new Dimension(width, height));       
        }
        /**
         *
         * Gets the text field's value
         *
         @return the text field value after trim()
         *
         */        
        public String getValue() {
           return textField.getText().trim();       
        }
        /**
         *
         * gets the label instance
         *
         @return jlabel component of this class
         *
         */        
        public JLabel getLabel() {
           return label;       
        }
        /**
         *
         * gets the text field instance
         *
         @return textfield component of this class
         *
         */        
        public JTextField getTextField() {
           return textField;       
        }
        
        public void setText(String text) {
           textField.setText(text);
        }
        
        private void fadeIn() {
          textField.setForeground(Color.black);
          for(int i = 0; i < colors.length; i++) {
            textField.setBackground(colors[i]);
            try Thread.sleep(fadeSpeed)catch(Exception e) { }
          }                
        }
        
        private void fadeOut() {
          textField.setForeground(Color.white);
          for(int i = colors.length -; i >= 0; i--) {
            textField.setBackground(colors[i])
            try Thread.sleep(fadeSpeed)catch(Exception le) { }
          }
        }
        
}

   
    
    
    
  














Related examples in the same category
1.Is Event Dispatcher ThreadIs Event Dispatcher Thread
2.Timer based animation
3.A rotating and scaling rectangle.
4.Fade out an image: image gradually get more transparent until it is completely invisible.
5.Font size animation
6.Hypnosis animationHypnosis animation
7.Noise ImageNoise Image
8.How to create Animation: Paint and threadHow to create Animation: Paint and thread
9.How to create animationHow to create animation
10.Animation: bounce
11.Image BouncerImage Bouncer
12.Text animationText animation
13.Buffered Animation DemoBuffered Animation Demo
14.Bouncing CircleBouncing Circle
15.Hypnosis SpiralHypnosis Spiral
16.Animator DemoAnimator Demo
17.Towers of Hanoi
18.Make your own animation from a series of images
19.Composition technique in this animation.
20.Animated Button
21.Animated Message Panel
22.Animated PasswordField
23.A simple spring simulation in one dimension
24.Shows an animated bouncing ball
25.Shows animated bouncing ballsShows animated bouncing balls
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.