Bellow is one of my buttons code
continueGame = new JButton("");
continueGame.setIcon(new ImageIcon("resources/buttonUI/continueGame.png"));
continueGame.setRolloverEnabled(true);
continueGame.setRolloverIcon(new ImageIcon("resources/buttonUI/continueGameOnHover.png"));
continueGame.setBorderPainted(false);
continueGame.setContentAreaFilled(false);
continueGame.setBounds(frameSize.x/2-343/2, (int)((frameSize.y)*0.28), 343, 85);
continueGame.addActionListener(this);
This works perfectly fine, but when I hover the button, it instantly changes from the base icon, to the RolloverIcon. Is there a way to have a fade between them or a slow change?
"Simple" is subjective
The "biggest" issue is trying to figure out "where" the painting should occur.
The icon and roll over icon are painted by the UI delegate. This is kind of painful as you really don't want to design a UI delegate for every possible look and feel you might want to use it.
So, instead, you need to take over a lot of the functionality yourself.
The following approach basically uses a delegate pattern to off load the blending of the "normal" and "roll over" icons and sets the button's
icon
property accordingly (it's a cheat)The basic functionality is controller by a Swing
Timer
, see How to Use Swing Timers for more details.It is time based, that is, rather then running until a value is achieved, it run's over a specified period of time. This generally produces a better animation effect.