/** * Copyright (c) 2005-2006, Sun Microsystems, Inc * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * Neither the name of the TimingFramework project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package animtransarticledemo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; import org.jdesktop.animation.timing.Animator; import org.jdesktop.animation.transitions.ScreenTransition; import org.jdesktop.animation.transitions.TransitionTarget; /** * This appliction shows some of the basic functionality of the * AnimatedTransitions library. Clicking on the More button will add * a text field to bottom of the current text fields. Clicking on the * Less button will remove the bottom text field. Either action will * cause an animation that will fade the bottom text field in or out * and move the Submit button up or down. * * @author Chet Haase */ public class FieldsOfText extends JComponent implements TransitionTarget, ActionListener { static final int MAX_FIELDS = 7; static final int COMPONENT_W = 200; static final int COMPONENT_H = 200; int numFields = 1; JButton moreButton = new JButton("More"); JButton lessButton = new JButton("Less"); JPanel moreOrLess = new JPanel(); // holds More/Less buttons JPanel submit = new JPanel(); // holds Submit button JComponent textFields[] = new JComponent[MAX_FIELDS]; // all text fields Animator animator = new Animator(500); // The animator used ScreenTransition transition = new ScreenTransition(this, this, animator); GradientPaint bgGradient = null; // background for the GUI int prevHeight = 0; // need this for gradient recreation public FieldsOfText() { // Non-linear motion for the transition looks better animator.setAcceleration(.2f); animator.setDeceleration(.2f); // Init our container and text fields setMaximumSize(new Dimension(COMPONENT_W, COMPONENT_H)); setPreferredSize(new Dimension(COMPONENT_W, COMPONENT_H)); for (int i = 0; i < textFields.length; ++i) { textFields[i] = new JTextField(); textFields[i].setMaximumSize(new Dimension(COMPONENT_W-40, 20)); } // Put More/Less buttons in a panel to get side-by-side layout setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); moreOrLess.setLayout(new BoxLayout(moreOrLess, BoxLayout.X_AXIS)); moreOrLess.add(moreButton); moreOrLess.add(lessButton); // Listen for button clicks, which trigger a transition moreButton.addActionListener(this); lessButton.addActionListener(this); // Put Submit button in separate panel for better layout submit.setLayout(new BorderLayout()); submit.add(new JButton("Submit"), BorderLayout.CENTER); submit.setMaximumSize(new Dimension(80, 20)); // Force the GUI to set up for the initial screen setupNextScreen(); } /** * Handle clicks on More and Less buttons. More adds a text field, * Less subtracts one, either one starts a transition running */ public void actionPerformed(ActionEvent ae) { boolean changed = false; if (ae.getSource().equals(moreButton)) { if (numFields < MAX_FIELDS) { // Don't grow without bound numFields++; changed = true; } } else if (ae.getSource().equals(lessButton)) { if (numFields > 1) { // Can't have less than one text field numFields--; changed = true; } } if (changed) { // Start our transition transition.start(); } } /** * Paints a gradient in the background of this component */ @Override protected void paintComponent(Graphics g) { if (getHeight() != prevHeight) { // Gradient is based on component height, so re-create when // height changes prevHeight = getHeight(); bgGradient = new GradientPaint(0, 0, new Color(0xEBF4FA), 0, prevHeight, new Color(0xBBD9EE)); } ((Graphics2D)g).setPaint(bgGradient); g.fillRect(0, 0, getWidth(), prevHeight); } /** * This is the place where we set up the GUI for the screen that * we're about to display. This might be the first screen, since * we call this at startup time, or it might be the next screen * that we want to transition to. */ public void setupNextScreen() { // First, clear out the previous GUI and start from scratch removeAll(); // Add the More/Less buttons add(moreOrLess); // Next, add the proper number of text fields for (int i = 0; i < numFields; ++i) { add(textFields[i]); } // Finally, add the Submit button at the bottom add(submit); } /** * Create the JFrame and populate it with our component */ private static void createAndShowGUI() { try { String name = UIManager.getSystemLookAndFeelClassName(); UIManager.setLookAndFeel(name); } catch(Exception e) { } JFrame f = new JFrame("Fields of Text"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new FieldsOfText()); f.getContentPane().setBackground(Color.WHITE); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } /** * From main(), we force our GUI creation to happen on the Swing * Event Display Thread */ public static void main(String args[]) { Runnable doCreateAndShowGUI = new Runnable() { public void run() { createAndShowGUI(); } }; SwingUtilities.invokeLater(doCreateAndShowGUI); } }