import javax.sound.midi.MidiSystem; import javax.sound.midi.Synthesizer; import javax.sound.midi.MidiChannel; import javax.sound.midi.MidiUnavailableException; int[] notes = { 72, 72, 72, 74, 76, 76, 74, 72, 76, 74, 74, 72, 72, 72, 72}; int timer = 0; int numnote = 0; int nChannelNumber = 0; int nNoteNumber = 61; // MIDI key number int nVelocity = 127; int nDuration = 32000; MidiChannel channel; int nbclics=0; // Compteur de clic int[] code = {1, 2, 3, 4}; // Code à trouver int[] codeAffiche = new int[code.length]; // Tableau des touches cliquées avec la même taille que le code boolean toucheValide = false; // Si la touche Valider est cliquée int rayon = 25; int tailleTexte1 = 12; int tailleTexte2 = 24; void setup() { frameRate(30); Synthesizer synth = null; try { synth = MidiSystem.getSynthesizer(); } catch (MidiUnavailableException e) { e.printStackTrace(); System.exit(1); } try { synth.open(); } catch (MidiUnavailableException e) { e.printStackTrace(); System.exit(1); } MidiChannel[] channels = synth.getChannels(); channel = channels[nChannelNumber]; size(250, 400); // Fenêtre de 250x400 px for (int i=0; i<(codeAffiche.length); i++) // Initialize le tableau des touches cliquées à -1 codeAffiche[i] = -1; background(0); fill(128); rect(25, 345, 200, 35, rayon); // Rectangle de la touche Valider fill(0); fill(128); textSize(tailleTexte2); for (int i=0; i<3 ; i++) // Pour chaque ligne { for (int j=0; j<3 ; j++) // Pour chaque colonne { fill(128); ellipse(25+rayon+(j*75), 125+rayon+(i*75), rayon*2, rayon*2); // Affiche le rectangle de la touche fill(0); text((j+1)+3*(i), 25+(j*75)+18, 125+(i*75)+33); // Affiche le numéro de la touche } } textSize(tailleTexte2); text("Valider", 90, 370); textSize(tailleTexte1); } void draw() { timer++; if(timer != 0 && timer%10 == 0 && numnote < notes.length) { channel.noteOn(notes[numnote], nVelocity); numnote++; } boolean valide = true; fill(255); rect(25, 25 , 200 , 75, rayon); // Rectangle de l'affichage fill(0); if(codeAffiche[0] == -1) // Si aucune touche n'a été cliquée { text("Entrez un code à 4 chiffres.", 35, 45); } else { for (int i=0; i<(codeAffiche.length); i++) // Pour chaque touche cliquée { if(codeAffiche[i] != -1) text(codeAffiche[i], 35+(i*7), 45); // Affiche la touche cliquée if(codeAffiche[i] != code[i]) valide = false; // Vérifie si elle correspond au code } } textSize(tailleTexte1); if(toucheValide) // Si la touche Valider a été cliquée { if(valide) // Et si le code est valide text("Code valide !", 35, 65); else text("Code invalide !", 35, 65); } text(nbclics, 190, 75); // Affiche le compteur de clic } void mousePressed() { for (int i=0; i<3 ; i++) { for (int j=0; j<3 ; j++) { if(sqrt( pow((50+(j*75) - (float)mouseX), 2) + pow((150+(i*75) - (float)mouseY), 2)) <= rayon) // Si une touche d'un chiffre est cliquée (distance entre le centre du cercle et l'endroit cliqué inférieur au rayon) { if(toucheValide) // Si la touche Valider a été cliquée précédemment { toucheValide = false; for (int k=0; k<(codeAffiche.length); k++) // Remet le tableau des touches cliquées à zéro codeAffiche[k] = -1; } int num = (j+1)+3*(i); // Chiffre cliqué channel.noteOn((num*2)+67, nVelocity); for (int k=0; k<(codeAffiche.length); k++) { if(codeAffiche[k] == -1) // Si le chiffre à la position k du tableau n'est pas définit { codeAffiche[k] = num; // Le définit avec le chiffre cliqué break; } } } } } if((mouseX >= 25) && (mouseY >= 350) && (mouseX <= 225) && (mouseY <= 375)) // Si la touche Valider est cliquée { toucheValide = true; } nbclics++; // Augmente le compteur de clics }