简体   繁体   English

如何最好地在Swing中实现淡入淡出按钮效果?

[英]How can I best implement a Fade Button Effect in Swing?

I have a JButton which, when pressed, changes background color from active to normal: 我有一个JButton ,按下该按钮会将背景颜色从活动状态更改为正常状态:

final Color activeButtonColor = new Color(159, 188, 191);
final Color normalButtonColor = new Color(47, 55, 56);

I want to fade the active button color back to the normal button color when a specific task has finished executing. 当特定任务完成执行后,我想将活动按钮的颜色退回到正常按钮的颜色。 I'm using SwingWorker and wondered if anybody could suggest an efficient way to do this please? 我正在使用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: Just to be clear, I'm looking for an efficient way to fade the RGB values for activeButtonColor back to normalButtonColor , without having to create a huge number of Color objects. 编辑:为了清楚activeButtonColor ,我正在寻找一种有效的方法来将activeButtonColor的RGB值activeButtonColornormalButtonColor ,而不必创建大量的Color对象。 Is it possible? 可能吗? Or do I just have to limit the number of fade steps to be more efficient? 还是我只需要限制淡变步骤的数量以提高效率?

Create a method that takes 3 arguments, a button, a from color and to color. 创建一个使用3个参数,一个按钮,一个from color和to color的方法。 Inside the method, create a swingworker to run in the background that does the fade of the colors. 在该方法内部,创建一个可在背景中运行的Swingworker,以使颜色褪色。

Then on your actionlistener where you do your work, before you do anything, call the fade method from active to normal. 然后,在执行操作的动作监听器上,在执行任何操作之前,请从主动到正常调用淡入淡出方法。 When its done, call the fade method from normal to active. 完成后,从正常到活动调用淡入淡出方法。

You'll end up with 3 total swingworkers being used in this situation. 在这种情况下,您最终将使用3个挥杆工人。 One for your work, one for the first fade and one for the last fade. 一个用于您的工作,一个用于第一次淡入淡出,一个用于最后淡入淡出。

Color objects should be fairly lightweight; 颜色对象应相当轻巧; they're basically just four floats plus a handful of pointers to pre-existing objects. 它们基本上只是四个浮点数,再加上一些指向预先存在的对象的指针。 If you really want to save on Color object instantiation, and you're sure the start and end colors above will not change, you could precompute the Color objects for the fade sequence, but I'm quite sure you could compute them on the fly as well, and suffer no consequences. 如果您真的想节省Color对象的实例化,并且确定上面的开始和结束颜色不会改变,则可以为渐变序列预先计算Color对象,但是我很确定您可以即时计算它们也不会遭受任何后果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM