// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;

public class SpecialButton extends JFrame implements MouseListener {
    private JButton x;
    private boolean state;
    private double f;
    private boolean done;
    private Updater upd;
    public SpecialButton() {
        x = new JButton("Use mouse to change");
        x.setMaximumSize(new Dimension(100, 50));
        x.addMouseListener(this);
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(x);
        setSize(200, 100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        state = true;
        f = 0.5;
        update();
    }
    public void mouseClicked(MouseEvent ev) {
    }
    public void mouseEntered(MouseEvent ev) {
    }
    public void mouseExited(MouseEvent ev) {
    }
    public void mousePressed(MouseEvent ev) {
        state = !state;
        f = 0.5;
        upd = new Updater();
        done = false;
        upd.start();
    }
    public void mouseReleased(MouseEvent ev) {
        done = true;
    }
    public void update() {
        Color basecol;
        if(state) {
            basecol = Color.RED;
        } else {
            basecol = Color.YELLOW;
        }
        Color col = new Color((int)(f * basecol.getRed()),
                              (int)(f * basecol.getGreen()),
                              (int)(f * basecol.getBlue()));
        x.setBackground(col);
    }
    public static void main(String[] args) {
        SpecialButton sb = new SpecialButton();
        sb.show();
    }
    private class Updater extends Thread {
        public void run() {
            while(!done) {
                update();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
                f += 0.1;
                if(f > 1.0) {
                    f = 1.0;
                }
            }
        }
    }
}
