IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Comment mettre en place un Jlabel avec une image de fond?


Sujet :

AWT/Swing Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 418
    Points : 67
    Points
    67
    Par défaut Comment mettre en place un Jlabel avec une image de fond?
    salut,

    je desire inserer dans mon Jpanel un Jlabel avec une image de fond et du texte centre par dessus ? y a t il une methode qui permet de superposer le texte centré sur l'image?

    merci

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Février 2006
    Messages
    197
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2006
    Messages : 197
    Points : 279
    Points
    279
    Par défaut
    tu peux mettre un ImageIcon dans un jlabel.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    ImageIcon icon = createImageIcon("tonimage.gif/jpg/png",
                                     "le text de l'image");
    JLabel label = new JLabel("text du label", icon, JLabel.CENTER);
    sinon si ça ne donne pas ce que tu veux tu peux utiliser un panel avec graphics et le mettre en setOpaque(false);

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 418
    Points : 67
    Points
    67
    Par défaut
    j'ai essayé mais envain ...j'ai même essayé avec des boutons ( image de fond + texte centré) mais seule l'image s'affiche

  4. #4
    Membre actif
    Profil pro
    Inscrit en
    Février 2006
    Messages
    197
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2006
    Messages : 197
    Points : 279
    Points
    279
    Par défaut
    tu n'as pas oublié de mettre le panel en opaque(false)?

    sinon j'ai testé la procédure du site marche bien normalement.

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 418
    Points : 67
    Points
    67
    Par défaut
    j'ai essayé mais en vain

    voila ce que je veux obtenir tout simplement en piece jointe sachant que j'ai l'image de fond
    Images attachées Images attachées  

  6. #6
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Bon j'avais envie de coder un peu donc voilou:



    pour le code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
    import java.awt.RenderingHints;
    import java.awt.Shape;
    import java.awt.TexturePaint;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import java.awt.geom.Rectangle2D;
    import java.awt.geom.RoundRectangle2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.Icon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    import javax.swing.border.Border;
     
    import org.jdesktop.swingx.JXFrame;
     
    /**
     *
     */
     
    /**
     * @author Sinok
     *
     */
    public class MyLabel extends JLabel implements ComponentListener{
     
        private TexturePaint backgroundPaint;
        private Shape backgroundShape;
        private Color borderColor = Color.darkGray;
     
        /**
         *
         */
        public MyLabel() {
            super();
            init();
            // TODO Raccord de constructeur auto-généré
        }
     
        /**
         * @param image
         * @param horizontalAlignment
         */
        public MyLabel(Icon image, int horizontalAlignment) {
            super(image, horizontalAlignment);
            init();
            // TODO Raccord de constructeur auto-généré
        }
     
        /**
         * @param image
         */
        public MyLabel(Icon image) {
            super(image);
            init();
            // TODO Raccord de constructeur auto-généré
        }
     
        /**
         * @param text
         * @param icon
         * @param horizontalAlignment
         */
        public MyLabel(String text, Icon icon, int horizontalAlignment) {
            super(text, icon, horizontalAlignment);
            init();
            // TODO Raccord de constructeur auto-généré
        }
     
        /**
         * @param text
         * @param horizontalAlignment
         */
        public MyLabel(String text, int horizontalAlignment) {
            super(text, horizontalAlignment);
            init();
            // TODO Raccord de constructeur auto-généré
        }
     
        /**
         * @param text
         */
        public MyLabel(String text) {
            super(text);
            init();
            // TODO Raccord de constructeur auto-généré
        }
     
     
        private GradientPaint prepareUpperGradient() {
            return new GradientPaint(0,0,Color.lightGray,0,getHeight()/2,Color.white);
        }
        private GradientPaint prepareLowerGradient() {
            return new GradientPaint(0,getHeight()/2,Color.white,0,getHeight(),Color.lightGray);
        }
     
        private Shape prepareShape() {
            Border b= this.getBorder();
            Insets insets = new Insets(0,0,0,0);
            if(b != null) {
                insets = b.getBorderInsets(this);
            }
            System.out.println("width "+getWidth()+" height "+getHeight());
            System.out.println("insets "+insets.toString());
            return new RoundRectangle2D.Double(insets.left,insets.top,getWidth()-insets.right*2,getHeight()-insets.bottom*2,10,10);
        }
     
        private TexturePaint prepareBackgroundPaint() {
            GradientPaint upperGradient = prepareUpperGradient();
            GradientPaint lowerGradient = prepareLowerGradient();
            BufferedImage im = new BufferedImage(getWidth(), getHeight(),BufferedImage.TYPE_INT_ARGB);
            Graphics2D imG2d = im.createGraphics();
            imG2d.setPaint(upperGradient);
            imG2d.fill(new Rectangle2D.Double(0,0,getWidth(),getHeight()/2));
            imG2d.setPaint(lowerGradient);
            imG2d.fill(new Rectangle2D.Double(0,getHeight()/2,getWidth(),getHeight()));
            imG2d.dispose();
     
            Rectangle2D rect = new Rectangle2D.Double(0,0,im.getWidth(), im.getHeight());
            return new TexturePaint(im,rect);
     
        }
     
     
     
        /* (non-Javadoc)
         * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
         */
        @Override
        protected void paintComponent(Graphics g) {
            // TODO Raccord de méthode auto-généré
            if(isShowing()) {
     
                Graphics2D g2d = (Graphics2D) g;
                super.paintComponent(g2d);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                if(backgroundPaint == null) {
                    backgroundPaint = prepareBackgroundPaint();
                }
                if(backgroundShape == null) {
                    backgroundShape = prepareShape();
                }
     
                g2d.setPaint(backgroundPaint);
                g2d.fill(backgroundShape);
                super.paintComponent(g2d);
     
     
            }
     
        }
     
     
     
        /* (non-Javadoc)
         * @see javax.swing.JComponent#paintBorder(java.awt.Graphics)
         */
        @Override
        protected void paintBorder(Graphics g) {
            // TODO Raccord de méthode auto-généré
            if(isShowing()) {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setColor(borderColor);
                g2d.draw(backgroundShape);
                super.paintBorder(g);
            }
        }
     
     
     
        public void componentHidden(ComponentEvent e) {
            // TODO Raccord de méthode auto-généré
     
        }
     
        public void componentMoved(ComponentEvent e) {
            backgroundPaint = prepareBackgroundPaint();
            backgroundShape = prepareShape();
            repaint();
        }
     
        public void componentResized(ComponentEvent e) {
            backgroundPaint = prepareBackgroundPaint();
            backgroundShape = prepareShape();
            repaint();
     
        }
     
        public void componentShown(ComponentEvent e) {
            // TODO Raccord de méthode auto-généré
     
        }
     
        private void init() {
            this.addComponentListener(this);
            setOpaque(false);
     
        }
     
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Raccord de méthode auto-généré
            SwingUtilities.invokeLater(new Runnable() {
     
                public void run() {
                    JFrame f = new JFrame();
                    MyLabel l = new MyLabel("toto");
                    l.setHorizontalAlignment(JLabel.CENTER);
                    l.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                    f.add(l);
                    f.pack();
                    f.setLocationRelativeTo(null);
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setVisible(true);
     
                }
     
            });
     
        }
    }
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  7. #7
    Membre actif
    Profil pro
    Inscrit en
    Février 2006
    Messages
    197
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2006
    Messages : 197
    Points : 279
    Points
    279
    Par défaut
    [edit] bon bah trop tard en fait :p

    sinon joli code sinok

  8. #8
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Un peu sale par moment, et théoriquement il vaudrait mieux faire un véritable UIDelagate (par exemple ici un setOpaque(true) fera que le gradient ne s'affichera plus)

    Sinon une autre solution aurait été de passer par les painters Swingx
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  9. #9
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Ah oui, pour les images, il suffit de procéder de la même manière dans le paintComponent: d'abord dessiner l'image puis appeler super.paintComponent
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

Discussions similaires

  1. Comment mettre à jour un champ BLOB avec une TIBQUERY ?
    Par colorid dans le forum Bases de données
    Réponses: 4
    Dernier message: 26/02/2008, 19h00
  2. [HTML] Comment mettre un texte en dessous d'une image?
    Par Jessika dans le forum Balisage (X)HTML et validation W3C
    Réponses: 8
    Dernier message: 23/08/2007, 07h52
  3. Jlabel sur une image de fond est ce possible?
    Par jlassiramzy dans le forum AWT/Swing
    Réponses: 1
    Dernier message: 04/05/2007, 12h29
  4. comment mettre un son au survol d'une image?
    Par mussara dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 13/07/2006, 18h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo