私が押され、JButton
を持って、通常の状態にアクティブから背景色を変更:スイングでフェードボタンエフェクトを実装するにはどうすればよいですか?
final Color activeButtonColor = new Color(159, 188, 191);
final Color normalButtonColor = new Color(47, 55, 56);
私は戻って、特定のタスクが実行を終了した通常のボタンの色にアクティブなボタンの色をフェードします。私はSwingWorker
を使用しており、誰かがこれを行うための効率的な方法を提案できるかどうか疑問に思っていますか?
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent event) {
new SwingWorker<Object, Object>() {
protected Object doInBackground() throws Exception {
button.setBackground(activeButtonColor);
for (int note = 60; note < 120; note++) {
midiController.sendMidiMessage(1, note, 83);
midiController.stopMidiMessage(note, 83);
}
Thread.sleep(200);
return null;
}
protected void done() {
try {
Object result = get();
// Fade back
} catch (Exception ex) {
ex.printStackTrace();
if (ex instanceof java.lang.InterruptedException)
return;
}
}
}.execute();
}
});
EDIT:ただ明確にするために、私はColor
オブジェクトの膨大な数を作成することなく、バックnormalButtonColor
からactiveButtonColor
のためのRGB値をフェードするための効率的な方法を探しています。出来ますか?または、フェードステップの数を制限して、より効率的にする必要がありますか?
これは素晴らしいことですが、フェード中に毎回複数のColorオブジェクトを作成しないようにするにはどうすればよいですか。私はここで何かを見逃しているかもしれないが、Colorは不変であるようだ。私は仮定している色のRGB値を変更する必要があります。 – Jon
中間色オブジェクトの配列を作成し、それをクラスレベルに保ちますか? –