Animation with Eclipse Shell – A simplistic approach – Part-II

Animation with Eclipse Shell – A simplistic approach – Part-II
An article by Debadatta Mishra
Introduction
While designing web page screens or desktop screens, it also matters how attractive is the screen design. In case of web page design, there are many APIs to enhance the beauty of the screens. However in case of modern screen design, animation in a screen plays a vital role. In case of desktop application, we can also provide better screen design with a bit of screen animation to impress the user. In this article I will show you how you can provide animated screens using Eclipse API in a simple manner.
Technicalities
As per the technicalities involved in SWT and Jface, there is no default animation effect API. All the API provided by Eclipse is generic and you can model your UI design in various ways. You can achieve the movement of a shell by dynamically setting the size of the shell. You have to understand certain methods like “setBounds”,”setLocation()” and “setSize()” provided by Eclipse API. Let us have a look into the following code structures for better understanding. In this article I will show you how you can shrink the size of the Shell dynamically and thereby to provide the effect of animation.

Code for ShrinkShellAnimation.java

package com.core.plugin.shell;

In the above program, the size of the shell is decreased dynamically.
Assumptions
I assume that reader of this article has
Exposure to eclipse plugin development
Knowledge on Java language
Knowledge on running programs in Eclipse editor

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* This class is used to show the animation
* by closing the shell.
* @author Debadatta Mishra(PIKU)
*
*/
public final class ShrinkShellAnimation
{
/**
* This method is used to place the shell in
* the centre of the monitor area.
* @param shell of type {@link Shell}
* @author Debadatta Mishra(PIKU)
*/
private static void setShellLocation( Shell shell )
{
Rectangle monitorArea = shell.getDisplay().getPrimaryMonitor().getBounds();
Rectangle shellArea = shell.getBounds();
int x = monitorArea.x + (monitorArea.width – shellArea.width)/2;
int y = monitorArea.y + (monitorArea.height – shellArea.height)/2;
shell.setLocation(x,y);
}

/**
* This method is used to show the animation
* by decreasing the x and y coordinates and
* by setting the size dynamically.
* @param shell of type {@link Shell}
*/
private static void doAnimation( Shell shell )
{
Point shellArea = shell.getSize();
int x = shellArea.x;
int y = shellArea.y;
while( x != -200 )
{
try
{
shell.setSize(x–, y–);
}
catch( Exception e )
{
e.printStackTrace();
}
}
}

//Main method to execute the test.
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell( display , SWT.CLOSE );
shell.setText(“Close and see the efffect”);
//Add the shell listener to handle the animated close event
shell.addShellListener( new ShellAdapter()
{
public void shellClosed(ShellEvent se)
{
doAnimation(shell);
}
}
);
shell.setSize(400, 200);
setShellLocation(shell);
shell.open ();
while (!shell.isDisposed ())
{
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

Test Case details
I have tested the above program in the following conditions.
OS Name : Windows Vista
Eclipse API : 3.2
Java : 1.6.0_16
Java Editor : Eclipse 3.2

Conclusion
I hope that you will enjoy my article. This article does not bear any commercial significance , it is only meant for learning and for novice developers. In case of any problem or errors , feel free to contact me in the email debadatta.mishra@gmail.com .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s