How to use glass pane for animation (SwingX and Timingframework)
Posted June 10th, 2007 by Nazmul
We believe in the power of ONE, at ScreamingToaster.
We believe in innovation - ONE experience can change a person's life.
We believe in partnerships - ONE endeavor can establish lasting trust & mutual success.
We believe in education - ONE training course can empower a developer for life.
We believe in giving back - ONE open-source project can inspire future entrepreneur.
ONE person can become the leader that starts a revolution.
ONE company can change the world and make it a better place - join us.
| Table of contents Introduction Background information Getting started Implementation details #1 Implementation of the event listener that will trigger the animator to start #2 implementation of the custom component that performs the animation Extras Summary Related links IntroductionI needed to perform animations in the app that I’m building (http://screamingtoaster.com). I needed to build animations that move various components around on the screen, and other animations that pop up components on top of existing components, etc. After creating a few of these effects, I realized that I was doing the same thing over and over again, which is why I decided to write this tutorial to encapsulate this pattern, in the hopes that I will help others doing the same thing. The strategy outlined here is just a suggestion on how to perform animation using a glass pane. There are many more strategies that you can select, depending on your requirements. However, I feel that this is one of the simplest and most effective approaches that have worked well for me, and I use it all the time in creating the user interface for applications in my platform product – http://screamingtoaster.com. If you look at some of the animations on the video posted on this website, they all use this strategy. The following is a list of approaches that you can take to create custom components:
The following is a list of approaches that you can take to create animations:
The easiest approach is to performing animations is using the glass pane. This tutorial will outline a strategy to using the glass pane for performing your own custom animation on components, to add special effects like Spring and Glow, and to even place custom components on top of existing components to show error conditions and such, and have components fly down and move up. Background informationFor more information on glasspane and RootPaneContainers refer to these links:
For more information on SwingX, refer to these links:
I’m planning on writing more tutorials on SwingX and how to use painters… check back in a few days/weeks and I should have them uploaded on the site. For more information on the Timingframework for animation, refer to these lniks:
Getting startedThe basic idea is to have all animation effects delegated to a component that’s placed on the glass pane, on top of your container (dialog, frame, etc). When the appropriate event triggers the animation to being, the custom component has to be installed as the glass pane of your RootPaneContainer and then your animator can be started which runs the animation, and when it’s complete, the glass pane has to be unistalled. The following diagram depicts this sequence of events: ![]() Implementation detailsYou have to implement 2 things:
#1 implementation of the event listener that will trigger the animator to startIn your event listener, be sure to perform the following in your TimingTarget implementation:
Here’s is some sample code below of an Animator that you could create in an event handler in your container:
1: RootPaneContainer container = ...;
2: AnimCompGlassPane component = new AnimCompGlassPane(); 3: component.init(...);
4: component.installOnContainer(container);
5:
6: Animator animator = new Animator( 7: ANIMATION_LENGTH, ANIMATION_REPEATS, Animator.RepeatBehavior.REVERSE,
8: new TimingTargetAdapter() { 9: @Override public void begin() { 10: component.startAnimation();
11: }
12:
13:
14:
15: @Override public void timingEvent(float v) { 16: component.setAnimationPhase(v);
17: }
18:
19: @Override public void end() { 20: component.endAnimation();
21: component.uninstallFromContainer();
22: }
23: });
24:
25: animator.setInterpolator(new SplineInterpolator(0.0f, 0.99f, 0.01f, 1.0f)); 26: animator.setResolution(ANIMATION_RESOLUTION);
27: animator.start();
#2 implementation of the custom component that performs the animationIn your custom component, that actually performs the painting for the animation, be sure to perform the following:
Here is some sample code, that may be a partial implementation of your animated component that goes on the glass pane:
1: private RootPaneContainer _rootPaneContainer; 2: private Component _oldGlasspane; 3: private float _animPhase; 4:
5: public void installOnContainer(RootPaneContainer container){ 6: _rootPaneContainer = container;
7: _oldGlasspane = _rootPaneContainer.getGlassPane();
8: _rootPaneContainer.setGlassPane(this); 9: setVisible(true); 10: requestFocusInWindow();
11: }
12:
13: public void uninstallFromContainer(){ 14: setVisible(false); 15: _rootPaneContainer.setGlassPane(_oldGlasspane);
16: _rootPaneContainer = null; 17: _oldGlasspane = null; 18: }
19:
20: public void startAnimation(){...} 21:
22: public void setAnimationPhase(float v){ 23: _animPhase = v;
24: repaint();
25: }
26:
27: public void endAnimation(...); 28:
29: public void paint(Graphics g){ 30: super.paint(g);
31: // do something with _animPhase and paint... 32: }
ExtrasWhen the glass pane is installed on your RootPaneContainer, you should request that it gets keyboard focus. This way, you won’t see any strange artifacts of keypresses moving things around underneath the component in the glasspane. You can also install a sink that absorbs all key press and mouse events so that these aren’t passed on to components other that what’s on your glass pane. You can also choose to respond to mouse events or key presses by removing the glass pane (kick off the animation which hides the component in the glass pane for example). The same pattern used in the #1 would apply here! SummaryThe strategy outlined here is just a suggestion on how to perform animation using a glass pane. There are many more strategies that you can select, depending on your requirements. However, I feel that this is one of the simplest and most effective approaches that have worked well for me, and I use it all the time in creating the user interface for applications in my platform product – http://screamingtoaster.com. If you look at some of the animations on the video posted on this website, they all use this strategy. Related links
|
We do not just build mobile “apps”, we build mobile experiences
Let us build you mobile experiences for Android, BlackBerry, and iPhone. We can also build you cloud-connected experiences that span Mobile (Android, BlackBerry, iPhone), Web (Safari, Firefox, Internet Explorer) and Desktop (PC, Mac, Linux) for a truly connected real-time experience. See our work in action: download RainOrShine for BlackBerry and CityRyde for Android.
- We provide full service, full lifecycle consulting services (planning, graphic design, architecture, marketing, support, and implementation).
- We provide Architecture guidance before implementation; marketing strategy after implementation. Click here to learn more.
Zen Application Framework
All our applications are built using our Zen Application Framework, which helps you avoid the common pitfalls around mobile development. Zen drastically reduces development time & cost for mobile, web, and desktop experiences. Click here to learn more.
Our training services make You self-sufficient & sustainable
Want to learn from the best? We offer Android and BlackBerry developer training programs. Whether you are just starting out, or want advanced training, we have courses for different skill levels & technology requirements. Contact us to learn more, and sign up.
Our values: Open-source. Democratization of knowledge. To lead, never follow
We are leaders in mobile technology. We have done and seen it all. We do not follow trends set by others & we do not make the mistakes made by others. We set the trends for mobile because we are mobile visionaries.
