1
14 package demo.view.anim;
15
16 import demo.view.ViewActionDemo;
17 import y.anim.AnimationFactory;
18 import y.anim.AnimationObject;
19 import y.anim.AnimationPlayer;
20 import y.anim.CompositeAnimationObject;
21 import y.util.DefaultMutableValue2D;
22 import y.util.Value2D;
23 import y.view.Graph2D;
24 import y.view.Graph2DView;
25 import y.view.Graph2DViewRepaintManager;
26 import y.view.NodeRealizer;
27 import y.view.ViewAnimationFactory;
28
29 import javax.swing.JFrame;
30 import javax.swing.JPanel;
31 import javax.swing.Timer;
32 import javax.swing.JRootPane;
33 import java.awt.BorderLayout;
34
35
39 public class EaseInEaseOutDemo
40 {
41 private static final int PREFERRED_DURATION = 2000;
42
43 private final Graph2DView view;
44 private Value2D[][] positions;
45 private Timer timer;
46
47 AnimationPlayer player;
48 ViewAnimationFactory factory;
49
50
54 public EaseInEaseOutDemo()
55 {
56 this.view = new Graph2DView();
57 init();
58 }
59
60
64 private void init()
65 {
66 positions = new Value2D[4][2];
67 for (int i = 0, n = positions.length; i < n; ++i)
68 {
69 positions[i][0] = DefaultMutableValue2D.create( 70, 110 + i*60);
70 positions[i][1] = DefaultMutableValue2D.create(410, 110 + i*60);
71 }
72
73 final String[] labels = {
74 "Normal", "Ease In", "Ease In, Ease Out", "Ease Out"
75 };
76
77 final Graph2D graph = view.getGraph2D();
78
79 for (int i = 0, n = positions.length; i < n; ++i)
80 {
81 final Value2D pos = positions[i][0];
82 final NodeRealizer nr = graph.getRealizer(
83 graph.createNode(pos.getX(), pos.getY()));
84 nr.setSize(120, 30);
85 nr.setLabelText(labels[i]);
86 }
87
88
89 timer = new Timer(PREFERRED_DURATION + 500,
90 new java.awt.event.ActionListener()
91 {
92 private boolean invert = false;
93 public void actionPerformed(final java.awt.event.ActionEvent e)
94 {
95 play(invert);
96 invert = !invert;
97 }
98 });
99 timer.setInitialDelay(1000);
100 timer.start();
101 }
102
103
116 private void play( final boolean invert )
117 {
118 final Graph2D graph = view.getGraph2D();
119
120 if(factory == null) {
121 factory = new ViewAnimationFactory(new Graph2DViewRepaintManager(view));
122 }
123
124 final CompositeAnimationObject moves = AnimationFactory.createConcurrency();
126
127 for (int i = 0, n = positions.length; i < n; ++i)
128 {
129 final Value2D dest = positions[i][invert ? 0 : 1];
130 final NodeRealizer nr = graph.getRealizer(graph.getNodeArray()[i]);
131
132 AnimationObject move =
135 factory.move(nr, dest, ViewAnimationFactory.APPLY_EFFECT,
136 PREFERRED_DURATION);
137
138 switch (i)
139 {
140 case 1:
141 move = AnimationFactory.createEasedAnimation(move, 1, 1);
143 break;
144 case 2:
145 move = AnimationFactory.createEasedAnimation(move);
147 break;
148 case 3:
149 move = AnimationFactory.createEasedAnimation(move, 0, 0);
151 break;
152 }
153
154 moves.addAnimation(move);
156 }
157
158 if (player == null) {
159 player = factory.createConfiguredPlayer();
160 }
161 player.animate(moves);
163 }
164
165
166
171 private void start( final String title )
172 {
173 final JFrame frame = new JFrame(title);
174
175 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
176 addContentTo(frame.getRootPane());
177 frame.pack();
178 frame.setLocationRelativeTo(null);
179 frame.setVisible(true);
180 }
181
182 public final void addContentTo( final JRootPane rootPane )
183 {
184 final JPanel contentPane = new JPanel(new BorderLayout());
185 contentPane.add(view, BorderLayout.CENTER);
186
187 rootPane.setContentPane( contentPane );
188 }
189
190 public void dispose()
191 {
192 if (timer != null) {
193 if (timer.isRunning()) {
194 timer.stop();
195 }
196 timer = null;
197 }
198 }
199
200 public static void main( String[] args )
201 {
202 ViewActionDemo.initLnF();
203 final EaseInEaseOutDemo demo = new EaseInEaseOutDemo();
204 demo.start("Ease Demo");
205 }
206 }
207