import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class grille extends JFrame{ bouton[][] tableauButton = new bouton[9][9]; private JPanel panneau = new JPanel(); private JLabel pourtour = new JLabel(); private JLabel panel11 = new JLabel();//création des neufs panneaux qui serviront plus tard private JLabel panel12 = new JLabel(); private JLabel panel13 = new JLabel(); private JLabel panel21 = new JLabel(); private JLabel panel22 = new JLabel(); private JLabel panel23 = new JLabel(); private JLabel panel31 = new JLabel(); private JLabel panel32 = new JLabel(); private JLabel panel33 = new JLabel(); private GridLayout carre = new GridLayout(3,3,3,3); private GridLayout carre11 = new GridLayout(3,3); private GridLayout carre12 = new GridLayout(3,3); private GridLayout carre13 = new GridLayout(3,3); private GridLayout carre21 = new GridLayout(3,3); private GridLayout carre22 = new GridLayout(3,3); private GridLayout carre23 = new GridLayout(3,3); private GridLayout carre31 = new GridLayout(3,3); private GridLayout carre32 = new GridLayout(3,3); private GridLayout carre33 = new GridLayout(3,3); public static void main(String arg[]) { new grille(); } public grille(){ super("sudoku"); setBounds(50,50,400,400); setDefaultCloseOperation(EXIT_ON_CLOSE); for(int i=0;i<=8;i++) {for(int j=0;j<=8;j++) {tableauButton[i][j]=new bouton();tableauButton[i][j].setText(String.valueOf((9*i)+j)); tableauButton[i][j].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { new sonDiffuseur("dontspeak.mid"); } }); } } File monFichier = new File("sudoku.txt"); //Debut implementation d'une grille FileReader fr = null; try { fr = new FileReader(monFichier); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } BufferedReader br =new BufferedReader(fr); String []c=new String [81]; String h=null; int t=0; try {for(t=0;t<=80;t++){h=br.readLine();c[t]=h;}} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(int v=0;v<=8;v++) { for(int j=0;j<=8;j++) { int k=((9*v)+j); String p="0"; if(c[k].equals(p)){tableauButton[v][j].setText("");}else{ tableauButton[v][j].setText(c[k]);} } } //fin implementation de la grille panel11.setLayout(carre11); for(int i=0;i<=2;i++) {for(int j=0;j<=2;j++) {final bouton bU=tableauButton[j][i];int o=((9*i)+j); bU.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { bU.setText("2"); } }); panel11.add(tableauButton[i][j]); } } panel12.setLayout(carre12); for(int i=0;i<=2;i++) {for(int j=3;j<=5;j++) { tableauButton[j][i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { } }); panel12.add(tableauButton[i][j]); } } panel13.setLayout(carre13); for(int i=0;i<=2;i++) {for(int j=6;j<=8;j++) {tableauButton[j][i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { } }); panel13.add(tableauButton[i][j]); } } panel21.setLayout(carre21); for(int i=3;i<=5;i++) {for(int j=0;j<=2;j++) {tableauButton[j][i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { } }); panel21.add(tableauButton[i][j]); } } panel22.setLayout(carre22); for(int i=3;i<=5;i++) {for(int j=3;j<=5;j++) {final bouton bJ=tableauButton[i][j];bJ.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { bJ.setForeground(Color.red); bJ.setText("2"); } }); panel22.add(bJ); } } panel23.setLayout(carre23); for(int i=3;i<=5;i++) {for(int j=6;j<=8;j++) {tableauButton[j][i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { } }); panel23.add(tableauButton[i][j]); } } panel31.setLayout(carre31); for(int i=6;i<=8;i++) {for(int j=0;j<=2;j++) {tableauButton[j][i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { } }); panel31.add(tableauButton[i][j]); } } panel32.setLayout(carre32); for(int i=6;i<=8;i++) {for(int j=3;j<=5;j++) {tableauButton[j][i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { } }); panel32.add(tableauButton[i][j]); } } panel33.setLayout(carre33); for(int i=6;i<=8;i++) {for(int j=6;j<=8;j++) {tableauButton[j][i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { } }); panel33.add(tableauButton[i][j]); } } pourtour.setLayout(carre); pourtour.add(panel11); pourtour.add(panel12); pourtour.add(panel13); pourtour.add(panel21); pourtour.add(panel22); pourtour.add(panel23); pourtour.add(panel31); pourtour.add(panel32); pourtour.add(panel33); panneau.add(pourtour); panneau.setOpaque(false); setContentPane(panneau); this.setVisible(true); } }