DEV/BUT1/CONTROLE/DEV2.1/Entrainement/SUJETB/EXO4/Ciel.java

40 lines
1.3 KiB
Java
Raw Permalink Normal View History

2024-03-09 16:05:37 +01:00
import javax.swing.JComponent;
import java.awt.Graphics;
public class Ciel extends JComponent {
@Override
protected void paintComponent(Graphics pinceau) {
// obligatoire : on crée un nouveau pinceau pour pouvoir le modifier plus tard
Graphics secondPinceau = pinceau.create();
// obligatoire : si le composant n'est pas censé être transparent
if (this.isOpaque()) {
// obligatoire : on repeint toute la surface avec la couleur de fond
secondPinceau.setColor(this.getBackground());
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
}
// maintenant on dessine ce que l'on veut
secondPinceau.setColor(this.getForeground());
secondPinceau.drawString("Bonjour !", 10, 20);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGUI();
});
}
private static void createAndShowGUI() {
JFrame fenetre = new JFrame("Ciel Bleu");
fenetre.setSize(400, 400);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Ajout de votre composant Ciel dans la fenêtre
Ciel cielComponent = new Ciel();
fenetre.add(cielComponent);
// Assurez-vous que la fenêtre est visible
fenetre.setVisible(true);
}
}