1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| package sudo1;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import acm.graphics.GLabel;
import acm.graphics.GRect;
import acm.program.GraphicsProgram;
public class sudo1 extends GraphicsProgram{
/**
*
*/
private static final long serialVersionUID = 1L;
public GRect fond;
public int bord = 100;
public int tailleCase = 50 ;
public int espaceCase = 10 ;
public GRect [][] bloc = new GRect[3][3];
public GRect [][] cellule = new GRect[9][9];
public static int [][] valCase = new int [9][9];
public int [][] modifCase = new int [9][9];
public static GLabel [][] texteCase = new GLabel[9][9];
public void init() {
fond = new GRect(bord,bord,9*tailleCase+10*espaceCase,9*tailleCase+10*espaceCase);
fond.setFillColor(Color.BLUE);
fond.setFilled(true);
add(fond);
//Création des blocs 3x3 :
for (int indX=0; indX<3;indX++) {
for (int indY=0; indY<3;indY++) {
bloc[indX][indY]= new GRect(bord+espaceCase+indX*3*espaceCase+indX*3*tailleCase,bord+espaceCase+indY*3*espaceCase+indY*3*tailleCase,
3*tailleCase+2*espaceCase,3*tailleCase+2*espaceCase);
bloc[indX][indY].setFillColor(Color.WHITE);
bloc[indX][indY].setFilled(true);
add(bloc[indX][indY]);
}
}
// création des cases :
for (int indX=0; indX<9;indX++) {
for (int indY=0; indY<9;indY++) {
cellule[indX][indY]= new GRect(bord+(1+indX)*espaceCase+indX*tailleCase,bord+(1+indY)*espaceCase+indY*tailleCase, tailleCase,tailleCase);
cellule[indX][indY].setFillColor(Color.LIGHT_GRAY);
cellule[indX][indY].setFilled(true);
add(cellule[indX][indY]);
valCase[indX][indY]=indX;
texteCase[indX][indY] = new GLabel(String.valueOf(valCase[indX][indY]),bord+(1+indX)*espaceCase+(0.4+indX)*tailleCase,
bord+(1+indY)*espaceCase+(0.6+indY)*tailleCase);
add(texteCase[indX][indY]);
}
}
try {
lecture();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ecriture();
}
public static void ecriture() {
String ligne ;
int nombre = 123;
BufferedWriter fichier;
try {
fichier = new BufferedWriter(new FileWriter("Toto.txt"));
fichier.write("bonjour tout le monde");
fichier.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("fichier créé");
}
public static void lecture() throws IOException {
int table[] = new int[100]; // un tableau suffisamment grand pour lire une grille de 9x9 devrait faire 81 cases, mais 100 ça fonctionne
BufferedReader Lecteur = null;
try {
Lecteur = new BufferedReader(new FileReader("Toto.txt"));
// remplis ici ta grille
String ligne; // pas besoin d'un tableau, puisqu'on lit ligne à ligne
int index = 0; // déclaration et initialisation dans la même instruction
while ((ligne = Lecteur.readLine()) != null) {
System.out.println("ligne : " + index + " > " + ligne);
table[index]=Integer.parseInt(ligne);
index++;
}
/*... remplissage de la grille graphique (GLabel ou autre) ...*/
}
catch(FileNotFoundException exc) {
System.out.println("erreur");
exc.printStackTrace(); // il est toujours intéressant de connaitre l'erreur si on veut pouvoir la traiter
}
finally {
// s'exécute toujours
if ( Lecteur!=null ) {
try {
Lecteur.close();
} catch( IOException exc) {
System.out.println("Fichier mal fermé");
exc.printStackTrace();
}
}
}
for (int indX=0; indX<3;indX++) {
for (int indY=0; indY<3;indY++) {
valCase[indX][indY]= table[indX+9*indY];
if ( valCase[indX][indY]==0 ) {
texteCase[indX][indY].setLabel(""); // case vide
} else {
texteCase[indX][indY].setLabel(String.valueOf(valCase[indX][indY]));
}
}
}
}
} |
Partager