package taswi;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import javax.swing.*;

public class TaSwi extends JFrame {
    private JLabel jLabel1 = new JLabel();
    private final JLabel jLabel2 = new JLabel();
    private int cnt=0,flg=0;
    
    public TaSwi() {       
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(800, 500, 400, 300);
        getContentPane().setLayout(null);
        setVisible(true);

        jLabel1.setBackground(Color.red);
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 30));
        jLabel1.setText("Pippo");
        jLabel1.setOpaque(true);
        jLabel1.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jLabel1PropertyChange(evt);
            }
        });
        getContentPane().add(jLabel1);
        jLabel1.setBounds(20, 20, 150, 79);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setText("Pippo nero su fondo rosso");
        
        getContentPane().add(jLabel2);
        jLabel2.setBounds(20, 100, 500, 150);
        
        new Timer(2500,new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                if (flg==0) {flg=1; cnt=1;}
                if(cnt<=3){
                    cnt++;
                    switch (cnt) {
                        case 1:
                            jLabel1.setText("Pippo");
                            jLabel1.setBackground(Color.red);
                            jLabel1.setForeground(Color.black);
                            break;
                        case 2:
                            jLabel1.setText("Pluto");
                            jLabel1.setBackground(Color.green);
                            jLabel1.setForeground(Color.yellow);
                            break;
                        case 3:
                            jLabel1.setText("Paperino");
                            jLabel1.setBackground(Color.blue);
                            jLabel1.setForeground(Color.white);
                            cnt=0;
                            break;
                        default:
                            break;
                    }
                }
            }
        }).start(); 
    }

    private void jLabel1PropertyChange(java.beans.PropertyChangeEvent evt) {
        String col_fondo="";
        String col_testo="";
        String testo="";
        String msg="";

        String pro = evt.getPropertyName();
        if ("text".equals(pro)){
            String tx=jLabel1.getText();
            if(null!=tx)switch (tx) {
                case "Pippo":
                    col_fondo="Rosso";
                    col_testo="Nero";
                    testo="Pippo";
                    break;
                case "Pluto":
                    col_fondo="Verde";
                    col_testo="Giallo";
                    testo="Pluto";
                    break;
                case "Paperino":
                    col_fondo="Blu";
                    col_testo="Bianco";
                    testo="Paperino";
                    break;
                default:
                    break;
            }
            msg = msg+testo +" "+ col_testo + " su fondo "+ col_fondo;
            jLabel2.setText(msg);
        }
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {           
            @Override
            public void run() {
                new TaSwi();
            }
        });
    }
}
