Last active
December 11, 2015 16:48
-
-
Save sumtyme/4630200 to your computer and use it in GitHub Desktop.
An Highlighting example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.util.Random; | |
import javax.swing.JButton; | |
import javax.swing.JComponent; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.Timer; | |
public class Test { | |
private static Color[] colors = new Color[]{Color.RED, Color.GREEN, Color.CYAN, Color.MAGENTA, Color.BLACK, Color.BLUE}; | |
private static Random rnd = new Random(); | |
public static void main(String[] args){ | |
JFrame f = new JFrame(); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
f.setSize(640, 480); | |
final JPanel panel = new JPanel(); | |
panel.setBackground(colors[colors.length - 1]); | |
panel.setPreferredSize(new Dimension(400, 400)); | |
final JButton btn = new JButton("ClickMe"); | |
btn.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
ComponentHighlighter highlighter = new ComponentHighlighter(panel, colors[rnd.nextInt(colors.length - 1)]); | |
highlighter.start(); | |
} | |
}); | |
panel.add(btn); | |
f.getContentPane().add(panel); | |
f.pack(); | |
f.setVisible(true); | |
} | |
private static class ComponentHighlighter extends Timer implements ActionListener{ | |
private Color backgroundEndColor; | |
private Color backgroundStartColor; | |
private Color backgroundColor; | |
private int step = 0; | |
private int maxSteps = 30; | |
private final JComponent comp; | |
public ComponentHighlighter(JComponent panel, Color highlightColor) { | |
super(1000/30, null); | |
addActionListener(this); | |
this.comp = panel; | |
backgroundStartColor = highlightColor; | |
backgroundEndColor = panel.getBackground(); | |
backgroundColor = backgroundStartColor; | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if(step >= maxSteps){ | |
this.stop(); | |
comp.setBackground(backgroundEndColor); | |
return; | |
} | |
backgroundColor = interpolateCurrentColor(); | |
comp.setBackground(backgroundColor); | |
step++; | |
} | |
private Color interpolateCurrentColor(){ | |
return new Color( | |
backgroundStartColor.getRed() + ((step * (backgroundEndColor.getRed() - backgroundStartColor.getRed())) / (maxSteps - 1)), | |
backgroundStartColor.getGreen() + ((step * (backgroundEndColor.getGreen() - backgroundStartColor.getGreen())) / (maxSteps - 1)), | |
backgroundStartColor.getBlue() + ((step * (backgroundEndColor.getBlue() - backgroundStartColor.getBlue())) / (maxSteps - 1)), | |
backgroundStartColor.getAlpha() + ((step * (backgroundEndColor.getAlpha() - backgroundStartColor.getAlpha())) / (maxSteps - 1)) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment