Vous êtes sur la page 1sur 3

TP : PROGRAMMATION GRAPHIQUE

CHARMOUH ABDELLAH

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import java.awt.Color;

import java.awt.Graphics;;

import java.awt.Dimension;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.ArrayList;

import java.util.Random;

class CercleGraphique {

private int x,y;

private Color couleur;

private int rayon;

private boolean plein;

CercleGraphique(int rayon, int x, int y, Color couleur, boolean plein) {

this.rayon = rayon;

this.x = x;

this.y = y;

this.couleur = couleur;

this.plein = plein;

public void dessiner(Graphics g) {


g.setColor(couleur);

if (plein) g.fillOval(x - rayon, y - rayon, 2*rayon, 2*rayon);

else g.drawOval(x - rayon, y - rayon, 2*rayon, 2*rayon);

class Controleur extends MouseAdapter {

private Clics panneau;

private Random alea = new Random();

Controleur(Clics panneau) {

this.panneau = panneau;

public void mousePressed(MouseEvent evt) {

CercleGraphique c;

int rayon = 5;

boolean plein = false;

if (SwingUtilities.isMiddleMouseButton(evt)) rayon = 10;

else if (SwingUtilities.isRightMouseButton(evt)) rayon = 15;

if ((evt.getModifiers() & MouseEvent.SHIFT_MASK) != 0) plein = true;

c = new CercleGraphique(rayon, evt.getX(), evt.getY(),

new Color(Math.abs(alea.nextInt())), plein);

panneau.getListe().add(c);

panneau.repaint();

class Clics extends JPanel {

private ArrayList<CercleGraphique> liste = new ArrayList<CercleGraphique>();


Clics() {

setPreferredSize(new Dimension(250, 250));

addMouseListener(new Controleur(this));

public void paintComponent(Graphics g) {

super.paintComponent(g);

for(CercleGraphique cercle : liste) cercle.dessiner(g);

ArrayList<CercleGraphique> getListe() {

return liste;

public class EssaiClics {

public static void main(String[] arg)

JFrame monCadre = new JFrame("Des bulles de couleur");

monCadre.setContentPane(new Clics());

monCadre.pack();

monCadre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

monCadre.setVisible(true);

Vous aimerez peut-être aussi