Last week a friend of mine pointed me to a website and said "I'm sure you can't do this in Java...".
So it was about the following flash component called MagicButton, which looks like this...
But if you move the mouse over the button it shows it's magic, which is a nice gradient that fades in slowly if you leave the mousepointer over the button and it flashes if you move the mousepointer over the button and directly move it away from it.
So thats the challenge here...
Dynamic fade in a gradient if the mouse moves over the button and fade it out again if the mouse exits the button again plus the different behavior if you leave the mouse over the button or move it over the button.
Well fortunately there's a guy named Kirill Grouchnikov that created this really nice library called trident animation library. With the help of this library it was not really a problem anymore to create some similar button in Java.
When i was playing around with the button and the effect i thought it would be a good idea to add another effect to it, in the original flash version i missed the clicked/pressed state of the button and therefor i decided to add it in my example...
If you open the source (which is a NetBeans project) you will find two classes:
- MButton
- MButtonUI
where the MButtonUI class is the ui delegate for the MButton class. In the MButtonUI you will find the painting of all the images that are needed to create the button (backgroundImage, contextImage, foregroundImage).
The painting of the effect is realized in the MButton itself and it uses the trident animation library to realize the animation effects.
That means you need to add the trident.jar file to your project if you would like to use the button.
The component allows you to set the hover effect color and also the clicked/pressed effect color.
To get a better impression of the animation you might want to try a little java webstart demo...
Source: MButton_Source.zip
Binary: MButton.jar
Enjoy coding and of course the upcoming weekend...
Awesome work as ever Gerrit! However I noticed that once you click on a button the gradient disappears and there's no indication left on which button you just clicked. Is this by design?
ReplyDeleteYou're right, i simply was not sure how to visualize a already clicked button...
ReplyDeleteSo i'm open for ideas...
Nice component! I'm starting to enjoy your friday fun stuff ;-)
ReplyDeleteCool stuff, I like it :).
ReplyDeleteHi, i like these buttons but i would like to change the background color for darker. Obviously the idisk account seem down (service unavailable) and thus i can't download the source, would you mind give a new link?
ReplyDeleteThank's for all these great components.
Bests
Hi Hoel,
DeleteUpdated the links, so you should be able to download the sources again. If you encounter other problems related to links, just let me know and I will update them as well.
Cheers,
Gerrit
Hi Han, can you update the link? Thanks
ReplyDeleteHi Nathan,
DeleteI hope all links are working again (at least all I've tested seem to work). :)