Cours:LampePOO : Différence entre versions

De troyesGEII
Aller à : navigation, rechercher
(Page créée avec « <source lang=java> class LampeGraphique extends Lampe { PVector position; float rayon; color couleur; LampeGraphique(int puis, PVector pos, float r, color c)... »)
 
 
Ligne 51 : Ligne 51 :
 
     if (estAllumee == true) println("allumée.");
 
     if (estAllumee == true) println("allumée.");
 
                       else println("éteinte.");
 
                       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;
 
Lampe l1,l2;
 +
Gradateur g1;
 +
Bouton bp;
 
int n;
 
int n;
 
void settings()
 
void settings()
 
{
 
{
 
}
 
}
 
+
 
void setup()
 
void setup()
 
{
 
{
 +
  bp = new Bouton();
 +
  g1 = new Gradateur();
 
   PVector p = new PVector(width/2,height/2);
 
   PVector p = new PVector(width/2,height/2);
 
   l1 = new LampeGraphique(100,p,width/4,color(0,255,0));
 
   l1 = new LampeGraphique(100,p,width/4,color(0,255,0));
 
   l2 = new Lampe(50);
 
   l2 = new Lampe(50);
 +
 
 +
  bp.setGradateur(g1);
 +
  g1.setLampe(l1);
 
}
 
}
 
+
 
void draw()
 
void draw()
 
{
 
{
Ligne 72 : Ligne 115 :
 
   if (n==30)
 
   if (n==30)
 
   {
 
   {
     l1.changerEtat();
+
     bp.appuyer();
 
     n=0;
 
     n=0;
 
   }
 
   }
 
}
 
}
 
</source>
 
</source>

Version actuelle datée du 10 octobre 2016 à 11:21

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;
  }
}