1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  package demo.view.anim;
29  
30  import demo.view.DemoDefaults;
31  import y.anim.AnimationFactory;
32  import y.anim.AnimationObject;
33  import y.anim.AnimationPlayer;
34  import y.anim.CompositeAnimationObject;
35  import y.util.DefaultMutableValue2D;
36  import y.util.Value2D;
37  import y.view.Graph2D;
38  import y.view.Graph2DView;
39  import y.view.Graph2DViewRepaintManager;
40  import y.view.NodeRealizer;
41  import y.view.ViewAnimationFactory;
42  import javax.swing.JFrame;
43  import javax.swing.JPanel;
44  import javax.swing.JRootPane;
45  import javax.swing.Timer;
46  import java.awt.BorderLayout;
47  import java.awt.EventQueue;
48  import java.awt.event.ActionEvent;
49  import java.awt.event.ActionListener;
50  
51  /**
52   * Demonstrates usage and effects of ease in and/or ease out for animation
53   * effects.
54   *
55   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/animation" target="_blank">Section Animations for Graph Elements</a> in the yFiles for Java Developer's Guide
56   */
57  public class EaseInEaseOutDemo {
58    private static final int PREFERRED_DURATION = 2000;
59  
60    private final Graph2DView view;
61    private Value2D[][] positions;
62    private Timer timer;
63  
64    AnimationPlayer player;
65    ViewAnimationFactory factory;
66  
67    /**
68     * Creates a new EaseInEaseOutDemo and initializes a Timer that triggers the
69     * animation effects.
70     */
71    public EaseInEaseOutDemo() {
72      this.view = new Graph2DView();
73      DemoDefaults.configureDefaultRealizers(view);
74      init();    
75    }
76   
77    /**
78     * Initializes the start and end points for the animated movement effects.
79     * Creates nodes to demonstrate animated movement.
80     */
81    private void init() {
82      positions = new Value2D[4][2];
83      for (int i = 0, n = positions.length; i < n; ++i) {
84        positions[i][0] = DefaultMutableValue2D.create(70, 110 + i * 60);
85        positions[i][1] = DefaultMutableValue2D.create(410, 110 + i * 60);
86      }
87  
88      final String[] labels = {
89          "Normal", "Ease In", "Ease In, Ease Out", "Ease Out"
90      };
91  
92      final Graph2D graph = view.getGraph2D();
93  
94      for (int i = 0, n = positions.length; i < n; ++i) {
95        final Value2D pos = positions[i][0];
96        final NodeRealizer nr = graph.getRealizer(
97            graph.createNode(pos.getX(), pos.getY()));
98        nr.setSize(120, 30);
99        nr.setLabelText(labels[i]);
100     }
101 
102 
103     timer = new Timer(PREFERRED_DURATION + 500,
104         new ActionListener() {
105           private boolean invert;
106 
107           public void actionPerformed(final ActionEvent e) {
108             play(invert);
109             invert = !invert;
110           }
111         });
112     timer.setInitialDelay(1000);
113     timer.start();
114   }
115 
116   /**
117    * Plays the movement animation for the nodes in the graph.
118    * Four different kinds of movement animations are created:
119    * <ul>
120    * <li> normal (i.e. no ease effect) </li>
121    * <li> ease in </li>
122    * <li> ease in and ease out </li>
123    * <li> ease out </li>
124    * </ul>
125    *
126    * @param invert   if <code>true</code> the nodes move from right to left;
127    *                 otherwise the nodes move from left to right.
128    */
129   private void play(final boolean invert) {
130     final Graph2D graph = view.getGraph2D();
131 
132     if (factory == null) {
133       factory = new ViewAnimationFactory(new Graph2DViewRepaintManager(view));
134     }
135 
136     // we want to play all four animations at the same time
137     final CompositeAnimationObject moves = AnimationFactory.createConcurrency();
138 
139     for (int i = 0, n = positions.length; i < n; ++i) {
140       final Value2D dest = positions[i][invert ? 0 : 1];
141       final NodeRealizer nr = graph.getRealizer(graph.getNodeArray()[i]);
142 
143       // create a movement effect from the realizer's current position to
144       // the specified destination
145       AnimationObject move =
146           factory.move(nr, dest, ViewAnimationFactory.APPLY_EFFECT,
147               PREFERRED_DURATION);
148 
149       switch (i) {
150         case 1:
151           // create an ease in effect
152           move = AnimationFactory.createEasedAnimation(move, 1, 1);
153           break;
154         case 2:
155           // create an ease in and ease out effect
156           move = AnimationFactory.createEasedAnimation(move);
157           break;
158         case 3:
159           // create an ease out effect
160           move = AnimationFactory.createEasedAnimation(move, 0, 0);
161           break;
162       }
163 
164       // register the individual animations for concurrent processing
165       moves.addAnimation(move);
166     }
167 
168     if (player == null) {
169       player = factory.createConfiguredPlayer();
170     }
171     // play the animations
172     player.animate(moves);
173   }
174 
175 
176   /**
177    * Creates an application frame for this demo
178    * and displays it. The given string is the title of
179    * the displayed frame.
180    */
181   private void start(final String title) {
182     final JFrame frame = new JFrame(title);
183 
184     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
185     addContentTo(frame.getRootPane());
186     frame.pack();
187     frame.setLocationRelativeTo(null);
188     frame.setVisible(true);
189   }
190 
191   public final void addContentTo(final JRootPane rootPane) {
192     final JPanel contentPane = new JPanel(new BorderLayout());
193     contentPane.add(view, BorderLayout.CENTER);
194 
195     rootPane.setContentPane(contentPane);
196   }
197 
198   public void dispose() {
199     if (timer != null) {
200       if (timer.isRunning()) {
201         timer.stop();
202       }
203       timer = null;
204     }
205   }
206 
207   public static void main(String[] args) {
208     EventQueue.invokeLater(new Runnable() {
209       public void run() {
210         DemoDefaults.initLnF();
211         (new EaseInEaseOutDemo()).start("Ease Demo");
212       }
213     });
214   }
215 }
216