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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
package com.vue;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.*;
import com.modele;
public class Ihm implements Observer {
private static int rows = 9;
private static int columns = 9;
JWindow pave = new JWindow();
JFrame window;
BouttonGrille buttonPressed;
JButton[] bouttonsGrille = new JButton[rows * columns];
Solver solver;
public class BouttonGrille extends JButton implements ActionListener {
int numero;
/**
* @return the numero
*/
public int getNumero() {
return numero;
}
public BouttonGrille(int numero) {
setText("");
this.numero = numero;
addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
buttonPressed = this;
Point p = new Point(getX(), getY());
SwingUtilities.convertPointToScreen(p, window);
pave.setLocation(p);
pave.setVisible(true);
}
}
public class BouttonChiffrePave extends JButton implements ActionListener {
int numero;
public BouttonChiffrePave(int numero) {
setText(numero + "");
addActionListener(this);
this.numero = numero;
}
public void actionPerformed(ActionEvent event) {
buttonPressed.setText(numero + "");
System.out.println("Boutton Grille : " + buttonPressed.getNumero());
System.out.println("Boutton pave : " + numero);
solver.setGrille(buttonPressed.getNumero(), numero);
pave.setVisible(false);
}
}
public class BouttonEffacerPave extends JButton implements ActionListener {
public BouttonEffacerPave() {
setText("C");
addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
bouttonsGrille[row * 9 + column].setText("");
solver.setGrille(row*9+column, 0);
}
}
pave.setVisible(false);
}
}
public class BouttonBlancPave extends JButton implements ActionListener {
public BouttonBlancPave() {
setText("0");
addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
buttonPressed.setText("");
solver.setGrille(buttonPressed.getNumero(),0);
pave.setVisible(false);
}
}
public class BouttonGoPave extends JButton implements ActionListener {
public BouttonGoPave() {
setText("G");
addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
pave.setVisible(false);
new Thread(new Runnable() {
public void run() {
solver.go();
}
}).start();
}
}
public Ihm(Solver solver) {
super();
this.solver = solver;
}
@Override
public void update(Observable o, Object arg) {
afficher(((Solver) o).getGrille());
}
public void afficher(int[] grille) {
for (int i = 0; i < grille.length; i++) {
bouttonsGrille[i].setText(grille[i] + "");
}
}
public void initGrille() {
window = new JFrame("Solver de sudoku");
window.getContentPane().setLayout(new GridLayout(rows, columns));
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
JButton button = new BouttonGrille(row * 9 + column);
if ((column / 3 == 0 && row / 3 == 0)
|| (column / 3 == 2 && row / 3 == 0)
|| (column / 3 == 1 && row / 3 == 1)
|| (column / 3 == 0 && row / 3 == 2)
|| (column / 3 == 2 && row / 3 == 2)) {
button.setBackground(new Color(100, 149, 237));
} else {
button.setBackground(new Color(46, 139, 87));
}
bouttonsGrille[row * 9 + column] = button;
window.getContentPane().add(button);
}
}
window.setSize(600, 600);
window.setVisible(true);
}
public void initPave() {
pave.setLayout(new GridLayout(4, 3));
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
JButton button = new BouttonChiffrePave(
((row - 1) * 3 + column));
pave.add(button);
}
}
JButton buttonC = new BouttonEffacerPave();
pave.add(buttonC);
JButton buttonBlanc = new BouttonBlancPave();
pave.add(buttonBlanc);
JButton buttonGo = new BouttonGoPave();
pave.add(buttonGo);
pave.setSize(150, 150);
pave.setVisible(false);
}
public void init() {
initGrille();
initPave();
}
public static void main(String[] args) {
Solver solver=new Solver();
Ihm ihm = new Ihm(solver);
ihm.init();
/* La vue s'enregistre au prêt du modèle*/
solver.addObserver(ihm);
}
} |
Partager