Comment dessiner un cercle et le faire apparaitre sur ma fenetre

herman2010 Messages postés 6 Date d'inscription   Statut Membre Dernière intervention   -  
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,
je commence a bien commprendre la programmation des IHM mais je suis bloquer a un niveau, je veux creer un bouton et un cercle au dessus du bouton . j'ai reussi a creer le bouton, mais lorsque je dessine le cercle il ne s'affiche pas:
voici la classe fenetre
package test1;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;

public class Fenetre1 extends JFrame{
private JPanel container = new JPanel();
public Fenetre1(){
Bouton bot = new Bouton("BOUTON");
this.setSize(300,300);
this.setTitle("bouton");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setBackground(Color.WHITE);
container.setLayout(new BorderLayout());

container.add(bot,BorderLayout.SOUTH);
this.setContentPane(container);
this.setVisible(true);
}
}
voici la classe bouton + cercle
package test1;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;


/**
*
* @author NGNINTEDEM NGUETSOP
*/
public class Bouton extends JButton {
private String name;
private int posX = - 50;
private int posY = -50;


public Bouton(String str){
super(str);
this.name = str;
}


public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D)g;
GradientPaint gp = new GradientPaint(0,0,Color.blue,0,20,Color.cyan,true);
g2d.setPaint(gp);
g2d.fillRect(0,0, this.getWidth(), this.getHeight());
g2d.setColor(Color.white);
g.setColor(Color.RED);
g2d.fillOval(posX, posY, 50, 50);
g2d.drawString(this.name, this.getWidth()/ 2 - (this.getWidth()/ 2/4), (this.getHeight()/2) +5);


}
}
voici la classe principale
package test1;

/**
*
* @author NGNINTEDEM NGUETSOP
*/
public class Test1 {


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Fenetre1 fen = new Fenetre1();


}
}

je comprend pas pourquoi il n'affiche pas



1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Tu ne peux pas dessiner ton cercle en dehors de la zone réservée au bouton.
Si tu veux avoir un cercle au dessus du bouton il faut que tu le dessines non pas par le bouton, mais par le parent du bouton (ton container ici).

Sinon, il faudrait redéfinir la forme de ton bouton, pour qu'il ne soit plus rectangulaire, mais inclue une zone ronde où tu pourras dessiner ton cercle.
Voir : How to Use Borders
0