| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 
 |  
import java.awt.*;
import java.awt.event.*;
 
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.io.File;
 
 
public class ImageBouton extends JButton {
 
 
    private static final long serialVersionUID = 1L;
 
 
    /** Etat du bouton à l'arrêt. */
    public static final int ETAT_NORMAL = 0;
    /** Etat du bouton quand survolé par la sourie. */
    public static final int ETAT_HOVER = 1;
    /** Etat du bouton quand cliqué. */
    public static final int ETAT_CLIC = 2;
 
 
    /**
     * Créer un bouton image sans label textuel.
     */
    public ImageBouton() {
 
        super();
        setContentAreaFilled(false);
        setBorderPainted(false);
    }
 
    /**
     * Créer un bouton image avec label textuel.
     */
    public ImageBouton(String label) {
 
        super(label);
        setContentAreaFilled(false);
        setBorderPainted(false);
    }
 
 
 
    /**
     * Définit quel fichier image utiliser pour un etat du bouton.
     * <br />Le bouton peut avoir 3 états différents, définit dans des constantes
     * statiques:
     * <br /><ul>
     *     <li><b>ImageBouton.ETAT_NORMAL</b> - Le bouton est à l'arrêt</li>
     *     <li><b>ImageBouton.ETAT_HOVER</b> - Le bouton est survolé par le curseur</li>
     *     <li><b>ImageBouton.ETAT_CLIC</b> - Le bouton est cliqué</li>
     * </ul>
     *
     * @param filePath Le chemin du fichier image
     * @param etat La constante d'état du bouton
     * @return <i>true</i> si le chargement à réussi, <i>false</i> sinon
     * @see ImageBouton#setImages(String[])
     * @see ImageBouton#ETAT_NORMAL
     * @see ImageBouton#ETAT_HOVER
     * @see ImageBouton#ETAT_CLIC
     */
    public boolean setImage(String filePath, int etat) {
 
        // On teste l'existence du fichier
        if( filePath != null && !new File(filePath).exists() )
            return false;
 
        // On test si l'état demandé existe
        if( etat < 0 || etat > 2)
            return false;
 
        // On créer l'image
        switch( etat ) {
 
            case ETAT_NORMAL:
                setIcon( new ImageIcon(filePath) );
                break;
 
            case ETAT_HOVER:
                setRolloverIcon( new ImageIcon(filePath) );
                break;
 
            case ETAT_CLIC:
                setRolloverSelectedIcon( new ImageIcon(filePath) );
                break;
 
            default:
                return false;
        }
 
        System.out.println("Chargement ok pour " + filePath);
 
        // Succès
        return true;
    }
 
 
    /**
     * Définit quels fichiers image utiliser pour le bouton.
     * <br />Il faut passer en argument les chemins vers 3 fichiers images:
     * <br /><ul>
     *     <li>Le bouton est à l'arrêt</li>
     *     <li>Le bouton est survolé par le curseur</li>
     *     <li>Le bouton est cliqué</li>
     * </ul>
     * <br />Si la méthode retourne false, il se peut que certains fichiers
     * passés en argument aient correctement été chargé.
     *
     * @param filePath Le chemin du fichier image
     * @return <i>true</i> si tous les chargements ont réussi, <i>false</i> sinon
     * @see ImageBouton#setImage(String, int)
     */
    public boolean setImages(String[] filePath) {
 
        if(filePath == null || filePath.length < 2)
            return false;
 
        boolean chargement = true;
 
        if( !setImage(filePath[ImageBouton.ETAT_NORMAL], ImageBouton.ETAT_NORMAL) )
            chargement = false;
 
        if( !setImage(filePath[ImageBouton.ETAT_HOVER], ImageBouton.ETAT_HOVER) )
            chargement = false;
 
        if( !setImage(filePath[ImageBouton.ETAT_CLIC], ImageBouton.ETAT_CLIC) )
            chargement = false;
 
        return chargement;
    }
 
 
    /**
     * Dessine le bouton, est appelé automatiquement.
     * <br />Pour forcer le dessin, appeler repaint().
     *
     * @param g Contexte graphique
     * @see AbstractButton#repaint()
     */
    public void paintComponent(Graphics g) {
 
        super.paintComponent(g);
 
        /* Ici dessin possible avec g */
    }
 
} | 
Partager