Cours:LampePOO

De troyesGEII
Aller à : navigation, rechercher
class LampeGraphique extends Lampe
{
  PVector position;
  float rayon;
  color couleur;
  
  LampeGraphique(int puis, PVector pos, float r, color c)
  {
    super(puis);
    position = pos;
    rayon = r;
    couleur = c;
  }
  void afficherEtat()
  {
    super.afficherEtat();
    if (estAllumee == true) fill(couleur);
                       else fill(0);
    ellipse(position.x, position.y, 2*rayon, 2*rayon);
  }
}

class Lampe
{
  int puissance;
  boolean estAllumee=false;
  Lampe(int p)
  {
    puissance = p;
  }
  void allumer()
  {
    estAllumee = true;
    afficherEtat();
  }
  void eteindre()
  {
    estAllumee = false;
    afficherEtat();
  }
  void changerEtat()
  {
    if ( estAllumee == false) allumer();
                         else eteindre();
  }
  void afficherEtat()
  
  {
    print("Lampe de puissance " + puissance + "W ");
    if (estAllumee == true) println("allumée.");
                       else println("éteinte.");
  }
}

class Gradateur {
  Lampe voyant=null;
  boolean etat;
  Gradateur() { }
  Gradateur(Lampe l) {
    setLampe(l);
  }
  void setLampe(Lampe l) {
    voyant = l;
  }
  void etatSuivant() {
    etat = !etat;
    if (voyant != null) {
      if (etat == true) voyant.allumer();
                   else voyant.eteindre(); 
    }
  }
}

class Bouton
{
  Gradateur pilote=null;
  
  Bouton() {
  }
  Bouton(Gradateur g) {
    setGradateur(g);
  }
  void setGradateur(Gradateur g) {
    pilote = g;
  }
  void appuyer() {
    if (pilote!=null) pilote.etatSuivant();
  }
}

Lampe l1,l2;
Gradateur g1;
Bouton bp;
int n;
void settings()
{
}
 
void setup()
{
  bp = new Bouton();
  g1 = new Gradateur();
  PVector p = new PVector(width/2,height/2);
  l1 = new LampeGraphique(100,p,width/4,color(0,255,0));
  l2 = new Lampe(50);
  
  bp.setGradateur(g1);
  g1.setLampe(l1);
}
 
void draw()
{
  n++;
  if (n==30)
  {
    bp.appuyer();
    n=0;
  }
}