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

Graphisme Java Discussion :

[Débutant] Bouton avec les bords arrondis


Sujet :

Graphisme Java

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

    Informations forums :
    Inscription : Juin 2006
    Messages : 2
    Points : 2
    Points
    2
    Par défaut [Débutant] Bouton avec les bords arrondis
    Bonjour,

    J'ai un JPanel avec des boutons dessus. Je voudrais que certains de ces boutons aient les bords arrondis.
    Pour cela, j'ai défini une classe héritant de JButton dans laquelle je redéfinit la méthode paintComponent comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    public paintComponent(Graphics g)
    {
    	super.paintComponent(g);
    	g.setColor(Color.BLACK);
    	g.drawRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
    }
    Je me retrouve avec un rectangle à bord arrondi à l'intérieur du rectangle du bouton.
    Comment faire pour avoir seulement le contour avec les bords arrondis ?

    De plus, je voudrais pouvoir changer la couleur de ce bouton : le setBakground colore le rectangle normal et non le rectangle à bord arrondi.


    J'ai vu plusieurs sujets traitant plus ou moins de ce problème mais je n'ai toujours pas compris !

    Merci d'avance pour votre aide.

  2. #2
    Membre averti Avatar de dazz_x
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    269
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations forums :
    Inscription : Mars 2006
    Messages : 269
    Points : 328
    Points
    328
    Par défaut
    bon un truc à la va vite :
    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
     
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.geom.RoundRectangle2D;
     
    import javax.swing.JButton;
     
    public class JRoundButton extends JButton {
     
     
    	public JRoundButton(String aNameString){
    		super(aNameString);
                    setContentAreaFilled(false);
    	}
     
    	@Override
    	protected void paintComponent(Graphics g) {
     
    		Graphics2D g2d=(Graphics2D) g;
     
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
    		int w = getWidth();
                    int h = getHeight();
    		RoundRectangle2D.Float r2d =new RoundRectangle2D.Float(0, 0, w-1 , h-1, 30, 30);
    		g2d.clip(r2d);
     
            GradientPaint gradient = new GradientPaint(0, 0, Color.blue, 0, h, Color.gray, false);
    		g2d.setPaint(gradient);
    		g2d.fillRect(0,0,w,h);
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_OFF);	
    		super.paintComponent(g);
     
    	}
    }
     
    voilà voilà, c'est pas le plus beau bouton du monde, mais bon ! Fauidrait essayer d'y ajouter une bordure etc... :mrgreen:
    La différence entre la théorie et la pratique est plus mince en théorie qu'en pratique

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

    Informations forums :
    Inscription : Juin 2006
    Messages : 2
    Points : 2
    Points
    2
    Par défaut
    Ok je vais me débrouiller avec ça.
    Merci beaucoup

  4. #4
    Membre averti Avatar de dazz_x
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    269
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations forums :
    Inscription : Mars 2006
    Messages : 269
    Points : 328
    Points
    328
    Par défaut
    j'ai trouvé un truc un p'tit peu mieux en adaptant un truc trouvé ici :
    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
     
    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Paint;
    import java.awt.RenderingHints;
    import java.awt.geom.RoundRectangle2D;
     
    import javax.swing.JButton;
     
    public class JCoolButton extends JButton {
     
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 1671314658637614873L;
    	private int inset = 5;
    	private Color buttonColor = Color.black.brighter().brighter().brighter().brighter();
     
     
    	public JCoolButton(String aNameString){
    		super(aNameString);
    		setContentAreaFilled(false);
    		setForeground(Color.white);
    	}
     
     
     
    	protected void paintComponent(Graphics g)
    	{
     
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
    		int vWidth = getWidth();
    		int vHeight = getHeight();
     
    		// Calculate the size of the button
    		int vButtonHeight = vHeight - (inset * 2);
    		int vButtonWidth = vWidth - (inset * 2);
    		int vArcSize = vButtonHeight;
     
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
    		// Create the gradient paint for the first layer of the button
    		Color vGradientStartColor =  buttonColor.darker().darker().darker();
    		Color vGradientEndColor = buttonColor.brighter().brighter().brighter();
    		Paint vPaint = new GradientPaint(0, inset, vGradientStartColor, 0, vButtonHeight, vGradientEndColor, false);
    		g2d.setPaint(vPaint);
     
    		// Paint the first layer of the button
    		g2d.fillRoundRect(inset, inset, vButtonWidth, vButtonHeight, vArcSize, vArcSize);
     
    		// Calulate the size of the second layer of the button
    		int vHighlightInset = 2;
    		int vButtonHighlightHeight = vButtonHeight - (vHighlightInset * 2);
    		int vButtonHighlightWidth = vButtonWidth - (vHighlightInset * 2);
    		int vHighlightArcSize = vButtonHighlightHeight;
     
    		// Create the paint for the second layer of the button
    		vGradientStartColor = Color.WHITE;
    		vGradientEndColor = buttonColor.brighter();
    		vPaint = new GradientPaint(0,inset+vHighlightInset,vGradientStartColor,0,inset+vHighlightInset+(vButtonHighlightHeight/2), buttonColor.brighter(), false);
     
    		// Paint the second layer of the button
    		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,.8f));
    		g2d.setPaint(vPaint);
     
    		g2d.fillRoundRect(inset+vHighlightInset,inset+vHighlightInset,vButtonHighlightWidth,vButtonHighlightHeight,vHighlightArcSize,vHighlightArcSize);
     
    		RoundRectangle2D.Float r2d =new RoundRectangle2D.Float(inset, inset, vButtonWidth, vButtonHeight, vArcSize, vArcSize);
    		g2d.clip(r2d);		
    		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f));
    		super.paintComponent(g);
     
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    	}
    }
    Je trouve ça assez joli
    La différence entre la théorie et la pratique est plus mince en théorie qu'en pratique

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [débutant] problème avec les arguments de fopen
    Par Anouschka dans le forum C++
    Réponses: 13
    Dernier message: 23/02/2006, 14h56
  2. [Débutant] Problème avec les paramètres d'une proc stockée
    Par babulior dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 28/06/2005, 15h38
  3. [Débutant]Problème avec les timers
    Par mickael777 dans le forum MFC
    Réponses: 1
    Dernier message: 11/04/2005, 11h00
  4. [Débutant] Pb avec les paramètres dans lien dynamique
    Par hackwell69 dans le forum Struts 1
    Réponses: 2
    Dernier message: 21/02/2005, 11h33
  5. [Débutant]Commencer avec les BDD
    Par Pill_S dans le forum Débuter
    Réponses: 6
    Dernier message: 29/06/2004, 14h02

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