/** * cantonnier5 * * Une application graphique, encore plus interactive ! */ // Import d'une "bibliothèque" qui permet d'entrer du texte : import interfascia.*; // Création de quelques variables "typés" : PFont police; float gris, avantHier, hierEnPlus, aujourdhuiEnPlus, totalACreuser, aPresent, encoreACreuser, rouge, limite; int chiffre; String aPresentText, encoreACreuserText; GUIController application; IFTextField champUn, champDeux, champTrois, champQuatre; // La mise en page (malheureusement, noLoop() semble impossible avec l'interface graphique "interfascia") : void setup() { size(730, 1000); police = createFont("Ubuntu", 52); textFont(police); // Création des champs de texte : champUn = new IFTextField("", 550, 25, 50); champDeux = new IFTextField("", 385, 75, 50); champTrois = new IFTextField("", 650, 120, 50); champQuatre = new IFTextField("", 400, 175, 50); application = new GUIController(this); application.add(champUn); application.add(champDeux); application.add(champTrois); application.add(champQuatre); application.requestFocus(champUn); } // L'animation : void draw() { background(255); // Dessine une graduation : for (chiffre = 300; chiffre <= height; chiffre += 3) { gris = map(chiffre, 300, height, 0, 255); stroke(gris, gris, gris); strokeWeight(1); line(0, chiffre, width, chiffre); } // Écris de texte : fill(255, 0, 0); text("m creusés avant-hier", 25, 50); text("hier m en plus", 25, 100); text("aujourd'hui plus que hier", 25, 150); text("total à creuser", 25, 200); text("0.0 m", 470, 365); // Dessine une ligne noire : stroke(0, 0, 0); strokeWeight(48); line(665, 350, 665, 950); // Calcule les résultats : avantHier = Double.isNaN(float(champUn.getValue())) ? 0 : float(champUn.getValue()); hierEnPlus = Double.isNaN(float(champDeux.getValue())) ? 0 : float(champDeux.getValue()); aujourdhuiEnPlus = Double.isNaN(float(champTrois.getValue())) ? 0 : float(champTrois.getValue()); totalACreuser = Double.isNaN(float(champQuatre.getValue())) ? 0 : float(champQuatre.getValue()); aPresent = 3 * avantHier + 2 * hierEnPlus + aujourdhuiEnPlus; encoreACreuser = totalACreuser - aPresent > 0 ? totalACreuser - aPresent : 0; // Ceci est necessaire pour bien imprimer les chiffres de type "float" : aPresent = Math.round(aPresent * 100); aPresent = aPresent/100; encoreACreuser = Math.round(encoreACreuser * 100); encoreACreuser = encoreACreuser/100; // Écris les résultats : aPresentText = String.valueOf(aPresent); text(aPresentText + " m déjà creusés", 235 - textWidth(aPresentText), 665); encoreACreuserText = String.valueOf(encoreACreuser); text(encoreACreuserText + " m à creuser", 315 - textWidth(encoreACreuserText), 965); // Dessine le résultat sur la ligne noire : stroke(255, 0, 0); rouge = map(aPresent, 0, totalACreuser, 350, 950); limite = Double.isNaN(rouge) ? 350 : rouge; line(665, 350, 665, limite < 950 ? limite : 950); stroke(255, 255, 255); point(665, 350); }