import java.awt.*; import javax.swing.*; public class Cercles extends JComponent { private int r; private int g; private int b; public Cercles(int r, int g, int b) { super(); this.r = r; this.g = g; this.b = b; } @Override public 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()); } // Dessine un anneau (couleurs dans le constructeur) secondPinceau.setColor(new Color(this.r, this.g, this.b)); secondPinceau.fillOval(0, 0, this.getWidth(), this.getHeight()); secondPinceau.setColor(Color.WHITE); secondPinceau.fillOval(this.getWidth()/4, this.getHeight()/4, this.getWidth()/2, this.getHeight()/2); } public static void main(String[] args) { JFrame fenetre = new JFrame(); fenetre.setSize(500,500); fenetre.setLocation(100,100); fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout layout = new GridLayout(5,5); fenetre.setLayout(layout); int[] couleur = {0, 60, 60}; for (int i = 0; i != 5; i++) { for (int j = 0; j != 5; j++) { couleur[1] -= 2; couleur[2] += 32; fenetre.add(new Cercles(couleur[0], couleur[1], couleur[2])); } couleur[1] += 10; couleur[2] -= 160; couleur[1] += 32; couleur[2] -= 2; } fenetre.setVisible(true); } }