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 :

Dégradé dans un JPanel


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau candidat au Club
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 1
    Par défaut Dégradé dans un JPanel
    bonjour je voudrai savoir comment faire un degradé dans un JPanel
    je crois qu'il fau utiliser la classe GradientPaint mais je n'arrive pas a le faire
    J'ai regarder beucoup de site qui explquai comment faire par contre je ne comprend pas toujour et quand j'essy de faire ce qui es dit ca ne marche pas.
    Si quelqu'un pourai m'expliquer et me donner un code pour cela je vous en serrai vraiment reconnaissant.
    Ne me donnez pas de lien de site internet je crois que jes les ai tous fait
    Merci pour votre aide

  2. #2
    Membre émérite
    Avatar de bbclone
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2006
    Messages : 537
    Par défaut
    tien essaye ca:

    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
     
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.LinearGradientPaint;
    import java.awt.Paint;
    import java.awt.Rectangle;
    import java.awt.geom.Point2D;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    /**
     * Created by IntelliJ IDEA.
     * User: bebe
     * Date: 10-Jun-2006
     * Time: 17:26:43
     * To change this template use File | Settings | File Templates.
     */
    public class LinearGradientPanel extends JPanel {
        private float[] fractions;
        private Color[] colors;
     
        public LinearGradientPanel() {
            fractions = new float[]{0.0f, 0.3f, 0.5f, 0.7f, 0.9f, 1.0f};
            colors = new Color[]{Color.RED, Color.ORANGE, Color.YELLOW, Color.PINK, Color.LIGHT_GRAY, Color.DARK_GRAY};
        }
     
        public LinearGradientPanel(float[] fractions, Color[] colors) {
            this.fractions = fractions;
            this.colors = colors;
        }
     
        @Override
        public void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            Rectangle bounds = getBounds();
     
            Point2D start = new Point2D.Float(0, 0);
            Point2D end = new Point2D.Float(bounds.width, bounds.height);
     
            /**
             * start - the gradient axis start Point2D in user space
             * end - the gradient axis end Point2D in user space
             * fractions - numbers ranging from 0.0 to 1.0 specifying
             *         the distribution of colors along the gradient
             * colors - array of colors corresponding to each fractional value
             */
     
            Paint p = new LinearGradientPaint(start, end, fractions, colors);
            g2d.setPaint(p);
            g2d.fillRect(0, 0, bounds.width, bounds.height);
        }
     
        public static void main(String[] args) {
     
            JPanel myGradientPanel = new LinearGradientPanel(
                    new float[]{0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1f},
                    new Color[]{Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.RED, Color.PINK}
            );
            JFrame myFrame = new JFrame();
            myFrame.add(myGradientPanel);
            myFrame.setLocationRelativeTo(null);
            myFrame.setPreferredSize(new Dimension(400, 300));
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setVisible(true);
        }
    }
    c'est simple a comprendre.
    et le resultat ca donne ca:
    http://www.developpez.net/forums/att...1&d=1149953406
    Images attachées Images attachées  

  3. #3
    Gfx
    Gfx est déconnecté
    Expert confirmé
    Avatar de Gfx
    Inscrit en
    Mai 2005
    Messages
    1 770
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 770
    Par défaut
    Attention, cet exemple utilise une nouvelle classe qui a été ajoutée dans Mustang (futur Java SE 6). Avant cette version, il faut utiliser GradientPaint, et non LinearGradientPaint, qui n'accepte que deux couleurs.

  4. #4
    Membre émérite
    Avatar de bbclone
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2006
    Messages : 537
    Par défaut
    juste
    j'ai pas fait attention

    voila un truc que tu peut utiliser avec n'importe quel version de java > a 1.2

    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
     
     
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Paint;
    import java.awt.Rectangle;
    import java.awt.Dimension;
     
    import javax.swing.JPanel;
    import javax.swing.JFrame;
     
    /**
     * Created by IntelliJ IDEA.
     * User: bebe
     * Date: 10-Jun-2006
     * Time: 16:27:36
     * To change this template use File | Settings | File Templates.
     */
    public class GradientPanel extends JPanel {
     
        private Color firstColor = null;
        private Color secondColor = null;
     
     
        public GradientPanel(Color firstColor, Color secondColor) {
            this.firstColor = firstColor;
            this.secondColor = secondColor;
        }
     
        public GradientPanel() {
            firstColor = Color.RED;
            secondColor = Color.YELLOW;
        }
     
        @Override
        public void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
     
            Rectangle bounds = getBounds();
     
            Paint p = new GradientPaint(0, 0, firstColor, bounds.width, bounds.height, secondColor);
            g2d.setPaint(p);
            g2d.fillRect(0, 0, bounds.width, bounds.height);
        }
     
     
        public static void main(String[] args) {
            JFrame myFrame = new JFrame();
            myFrame.add(new GradientPanel());
            myFrame.setSize(new Dimension(400, 300));
            myFrame.setLocationRelativeTo(null);
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setVisible(true);
        }
    }

Discussions similaires

  1. Mettre un dégradé ou couleur dans un jPanel
    Par Telemak dans le forum 2D
    Réponses: 16
    Dernier message: 14/04/2009, 21h26
  2. [JPanel] [Débutant] Fond dégradé dans un JPanel ??
    Par dymezac dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 15/02/2006, 16h33
  3. Dessiner dans différents JPanels
    Par N@sH dans le forum Agents de placement/Fenêtres
    Réponses: 2
    Dernier message: 20/05/2005, 15h39
  4. [JPanel] parcours d'objets dans un JPanel
    Par fleur1234 dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 17/01/2005, 13h24
  5. Dessiner dans un JPanel
    Par Oliveuh dans le forum Composants
    Réponses: 5
    Dernier message: 19/07/2004, 12h13

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