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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
| import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class Interface extends JFrame {
// Variables declaration
JPanel contentPanel;
Sudoku sudoku;
Fond supanel;
// Ecouteur et Presse-Papier
Ecouteur ecouteur;
ModifPressPap mpp;
FileLogger file;
Menu menu;
// Tableaux pour les chiffres
Image[] nbplaces = new Image[9];
Image[] nbdispo = new Image[9];
// IMAGE JPANEL
Image map = new ImageIcon("http://www.developpez.net/forums/images/sudoku.png").getImage();
Image digi = new ImageIcon("http://www.developpez.net/forums/images/digits.gif").getImage();
BufferedImage mapmodifiable = toBufferedImage(map);
BufferedImage digits = toBufferedImage(digi);
int carre = 30;
public Interface() {
super();
this.setVisible(false);
file = new FileLogger(this, "sudoku.txt");
file.repertoire();
// LANCEMENT DE SUDOKU ET MISE EN DAEMON
this.sudoku = new Sudoku(9);
sudoku.setPriority(Thread.MAX_PRIORITY);
sudoku.start();
initializeComponent();
creationdigits();
this.setVisible(true);
}
public void creationdigits() {
int indice = 0;
for (int i = 0; i <= digits.getHeight() - (carre - 1); i += (carre - 1)) {
nbplaces[indice] = digits.getSubimage(29, i, carre - 1, carre - 1);
nbdispo[indice] = digits.getSubimage(0, i, carre - 1, carre - 1);
indice++;
}
}
private void initializeComponent() {
// CONTENTPANEL
contentPanel = (JPanel) this.getContentPane();
contentPanel.setLayout(new GridBagLayout());
// SUDOKUPANEL
supanel = new Fond(map, this);
// MENU
menu = new Menu(this);
this.setJMenuBar(menu);
// ECOUTEUR ET PRESSEPAPIER
ecouteur = new Ecouteur(this);
mpp = new ModifPressPap();
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 100;
c.weighty = 100;
c.fill = GridBagConstraints.NONE;
c.ipadx = 0;
c.ipady = 0;
contentPanel.add(supanel, c);
this.setFocusable(true);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int caract = e.getKeyCode();
switch (caract) {
case KeyEvent.VK_1:
PlayCommand command = new PlayCommand(ecouteur.map
.getSudoku(), ecouteur.carre, ecouteur.map,
ecouteur.carre.getI(), ecouteur.carre.getE(), 1);
Processor.getInstance().execute(command);
break;
case KeyEvent.VK_2:
PlayCommand command2 = new PlayCommand(ecouteur.map
.getSudoku(), ecouteur.carre, ecouteur.map,
ecouteur.carre.getI(), ecouteur.carre.getE(), 2);
Processor.getInstance().execute(command2);
break;
case KeyEvent.VK_3:
PlayCommand command3 = new PlayCommand(ecouteur.map
.getSudoku(), ecouteur.carre, ecouteur.map,
ecouteur.carre.getI(), ecouteur.carre.getE(), 3);
Processor.getInstance().execute(command3);
break;
case KeyEvent.VK_4:
PlayCommand command4 = new PlayCommand(ecouteur.map
.getSudoku(), ecouteur.carre, ecouteur.map,
ecouteur.carre.getI(), ecouteur.carre.getE(), 4);
Processor.getInstance().execute(command4);
break;
case KeyEvent.VK_5:
PlayCommand command5 = new PlayCommand(ecouteur.map
.getSudoku(), ecouteur.carre, ecouteur.map,
ecouteur.carre.getI(), ecouteur.carre.getE(), 5);
Processor.getInstance().execute(command5);
break;
case KeyEvent.VK_6:
PlayCommand command6 = new PlayCommand(ecouteur.map
.getSudoku(), ecouteur.carre, ecouteur.map,
ecouteur.carre.getI(), ecouteur.carre.getE(), 6);
Processor.getInstance().execute(command6);
break;
case KeyEvent.VK_7:
PlayCommand command7 = new PlayCommand(ecouteur.map
.getSudoku(), ecouteur.carre, ecouteur.map,
ecouteur.carre.getI(), ecouteur.carre.getE(), 7);
Processor.getInstance().execute(command7);
break;
case KeyEvent.VK_8:
PlayCommand command8 = new PlayCommand(ecouteur.map
.getSudoku(), ecouteur.carre, ecouteur.map,
ecouteur.carre.getI(), ecouteur.carre.getE(), 8);
Processor.getInstance().execute(command8);
break;
case KeyEvent.VK_9:
PlayCommand command9 = new PlayCommand(ecouteur.map
.getSudoku(), ecouteur.carre, ecouteur.map,
ecouteur.carre.getI(), ecouteur.carre.getE(), 9);
Processor.getInstance().execute(command9);
break;
case KeyEvent.VK_DELETE:
ecouteur.chiffre(0);
break;
}
}
});
this.setContentPane(contentPanel);
this.setLocation(new Point(350, 350));
this.setTitle("Sudoku");
this.setResizable(false);
this.setSize(new Dimension(540, 405));
}
public BufferedImage toBufferedImage(Image image) {
/** On test si l'image n'est pas déja une instance de BufferedImage */
if (image instanceof BufferedImage) {
/** cool, rien à faire */
return ((BufferedImage) image);
} else {
/** On s'assure que l'image est complètement chargée */
image = new ImageIcon(image).getImage();
/** On crée la nouvelle image */
BufferedImage bufferedImage = new BufferedImage(
image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return (bufferedImage);
}
}
public static void main(String[] args) {
window fenetreintro = new window(440, 400, 1);
// INTERFACE GRAPHIQUE
Interface graphique = new Interface();
graphique.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public Fond getSupanel() {
return supanel;
}
public Image getNbdispo(int i) {
return nbdispo[i];
}
public Image getNbplaces(int i) {
return nbplaces[i];
}
public Sudoku getSudoku() {
return sudoku;
}
public void setSudoku(Sudoku sudoku) {
this.sudoku = sudoku;
}
public ModifPressPap getMpp() {
return mpp;
}
/**
* @return Renvoie contentPanel.
*/
public JPanel getContentPanel() {
return contentPanel;
}
/**
* @param contentPanel
* contentPanel à définir.
*/
public void setContentPanel(JPanel contentPanel) {
this.contentPanel = contentPanel;
}
} |