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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
| import gestionReg.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
public class Echiquier extends JLayeredPane {
public static final int TAILLE_CASE =55;
public static final int MAX = (TAILLE_CASE * 7);
private static final int largeurLabelLigne = 15;
private static final int hauteurLabelLigne = TAILLE_CASE;
private static final int largeurLabelColonne = TAILLE_CASE;
private static final int hauteurLabelColonne = 15;
public HashMap icons = new HashMap();
private JLabel[] labelLigne = new JLabel[16];
private JLabel[] labelColonne = new JLabel[16];
public JLabel[][] arrierePlan = new JLabel[8][8];
public JLayeredPane carreau = new JLayeredPane();
private EchiquierVirtuel EchVir =null;
private FenetrePr Fen;
private Horloge horloge = new Horloge(Fen);
public static List listeEchiquier ;
private boolean jeuxEstFini = false;
public void nouveauJeux() {
listeEchiquier = new Vector();
listeEchiquier.add(new EchiquierVirtuel());
EchVir = (EchiquierVirtuel)listeEchiquier.get(0);
EchVir.initialiser();
horloge.arreterHorloge();
horloge.reinitialiserHorloge();
jeuxEstFini = false;
}
public void faireCoup(Coup coup) {
horloge.arreterHorloge();
EchVir =(EchiquierVirtuel)(listeEchiquier.get( listeEchiquier.size()-1));
try {
EchVir.faireCoup(coup);
} catch (EchiquierVirtuel.ExceptionCoupImpossible e) {
}
horloge.commencerHorloge();
VerifieSiFini();
}
public void VerifieSiFini() {
if (EchVir.jeuEstFini()) {
if (EchVir.seulRoisQuiReste()) {
//showMessage("Draw!");
} else if (EchVir.getAuBlancDeJouer()) {
if (EchVir.estAttaquerParNoir(EchVir.getPosRoiBlanc())) {
Object[] options = { "Accept", "Reject" };
// showMessage("Black wins!");
JOptionPane.showMessageDialog(null, "Black wins!");
} else {
//showMessage("Draw (stalemate)!");
JOptionPane.showMessageDialog(null, "Draw!");
}
} else {
if (EchVir.estAttaquerParBlanc(EchVir.getPosRoiNoir())) {
// showMessage("White wins!");
JOptionPane.showMessageDialog(null, "White wins!");
} else {
// showMessage("Draw (stalemate)!");
JOptionPane.showMessageDialog(null, "Draw!");
}
}
horloge.arreterHorloge();
} else if ((horloge.gettempsBlanc() <= 0 || horloge.gettempsNoir() <= 0)) {
horloge.arreterHorloge();
if (horloge.gettempsBlanc() <= 0) {
//showMessage("Black wins (time forfeit)!");
JOptionPane.showMessageDialog(null, "Black wins!");
} else {
//showMessage("White wins (time forfeit)!");
JOptionPane.showMessageDialog(null, "White wins!");
}
// showMessage(
// "White: "
// + horloge.formatTemps(temps)(horloge.gettempsBlanc())
// + ", Black: ");
//showMessage("The clocks have been disabled, you can continue playing, if you want to.");
}
}
class FigurePiece extends JLabel implements MouseListener, MouseMotionListener {
int ligne, colonne;
int mousePressedX, mousePressedY;
boolean estPieceBlanche;
int type;
private EchiquierVirtuel EchVir;
private FenetrePr Fen;
//constructeur
public FigurePiece(int _type) {
super((ImageIcon) icons.get(new Integer(_type)));
type = _type;
estPieceBlanche = type >0;
setSize(getIcon().getIconWidth(), getIcon().getIconHeight());
addMouseMotionListener(this);
addMouseListener(this);
}
public int getligne() {
return 7 - ((getLocation().y + (Echiquier.TAILLE_CASE / 2)) / Echiquier.TAILLE_CASE);
}
public int getcolonne() {
return ((getLocation().x + (Echiquier.TAILLE_CASE / 2)) / Echiquier.TAILLE_CASE);
}
public void setChamps(int nouveauLigne, int nouveauColonne) {
setLocation(nouveauColonne * Echiquier.TAILLE_CASE, (7 - nouveauLigne) * Echiquier.TAILLE_CASE);
}
//methodes pour MouseListener et MouseMotionListener
public void mousePressed(MouseEvent e) {
ligne = getligne();
colonne = getcolonne();
mousePressedX = e.getX();
mousePressedY = e.getY();
EchVir = (EchiquierVirtuel)listeEchiquier.get(listeEchiquier.size()-1);
if (EchVir.champs[(ligne << 3) + colonne] == EchiquierVirtuel.CV) {
Echiquier.this.mettreAJour();
return;
}
for (int l = 0; l < 8; l++)
for (int c = 0; c < 8; c++)
{
if (EchVir.estCoupPossible(new Coup(ligne, colonne, l, c)))
{
arrierePlan[7 - l][c].setBackground(new Color(0,0,0,0));
}
}
}
public void mouseDragged(MouseEvent e) {
int nouveauX, nouveauY;
nouveauX = getLocation().x + e.getX() - mousePressedX;
if (nouveauX > MAX)
nouveauX = MAX;
if (nouveauX < 0)
nouveauX = 0;
nouveauY = getLocation().y + e.getY() - mousePressedY;
if (nouveauY > MAX)
nouveauY = MAX;
if (nouveauY < 0)
nouveauY = 0;
setLocation(nouveauX, nouveauY);
carreau.setLayer(this, 10, 0);
}
public void mouseReleased(MouseEvent e) {
int nouveauLigne = getligne();
int nouveauColonne = getcolonne();
carreau.setLayer(this, 2, 0);
if (ligne != nouveauLigne || colonne != nouveauColonne)
{
final Coup coup = new Coup(ligne, colonne, nouveauLigne, nouveauColonne);
if (EchVir.estCoupPossible(coup))
{
if ((EchVir.champs[coup.getSource()] == EchiquierVirtuel.PB && coup.getDestinationLigne() == 7)
|| (EchVir.champs[coup.getSource()] == EchiquierVirtuel.PN && coup.getDestinationLigne() == 0)) {
coup.setPionPromu(selectionnerPromotion());
}
faireCoup(coup);
} else
Echiquier.this.mettreAJour();
} else
Echiquier.this.mettreAJour();
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
}
/**
* fenetre pour selectionner la promotion
* @return entier représentant la piece selectionné
*/
public byte selectionnerPromotion() {
Object[] option = new Object[4];
if (EchVir.getAuBlancDeJouer()) {
option[0] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.RB));
option[1] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.TB));
option[2] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.FB));
option[3] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.CB));
} else {
option[0] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.RN));
option[1] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.TN));
option[2] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.FN));
option[3] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.CN));
}
int choix =
JOptionPane.showOptionDialog(
null,
"Promu le pion par:",
"",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
option,
option[0]);
if (choix == 0)
return EchiquierVirtuel.REINE;
else if (choix == 1)
return EchiquierVirtuel.TOUR;
else if (choix == 2)
return EchiquierVirtuel.FOU;
else if (choix == 3)
return EchiquierVirtuel.CAVALIER;
else
return EchiquierVirtuel.RIEN;
}
//constructeur
public Echiquier(FenetrePr fen) {
Fen = fen;
initializer();
}
/**
* créer l'arriére plan
*/
private void restaurerArrierePlan() {
ImageIcon img = new ImageIcon(Echiquier.class.getResource("/images/caseBlanche.jpg"));
ImageIcon img1 = new ImageIcon(Echiquier.class.getResource("/images/caseNoire.jpg"));
for (int ligne = 0; ligne < 8; ligne++)
for (int colonne = 0; colonne < 8; colonne++)
if ((ligne + colonne & 1) == 1)
{
arrierePlan[ligne][colonne].setIcon(img);
}
else
arrierePlan[ligne][colonne].setIcon(img1);
}
private void chargerIcon(int type, String nomFichier) {
String nomCompletFichier = "/images/" + nomFichier;
URL url = Echiquier.class.getResource(nomCompletFichier);
ImageIcon icon = url != null ? new ImageIcon((URL) url) : new ImageIcon("null");
if (icon == null) {
System.out.println("Impossible de trouver le fichier: " + nomFichier + "\n");
System.exit(-1);
}
icons.put(new Integer(type), icon);
}
private void initializer() {
chargerIcon(EchiquierVirtuel.KB, "WK.png");
chargerIcon(EchiquierVirtuel.RB, "WQ.png");
chargerIcon(EchiquierVirtuel.TB, "WR.png");
chargerIcon(EchiquierVirtuel.FB, "WB.png");
chargerIcon(EchiquierVirtuel.CB, "WN.png");
chargerIcon(EchiquierVirtuel.PB, "WP.png");
chargerIcon(EchiquierVirtuel.KN, "BK.png");
chargerIcon(EchiquierVirtuel.RN, "BQ.png");
chargerIcon(EchiquierVirtuel.TN, "BR.png");
chargerIcon(EchiquierVirtuel.FN, "BB.png");
chargerIcon(EchiquierVirtuel.CN, "BN.png");
chargerIcon(EchiquierVirtuel.PN, "BP.png");
nouveauJeux();
EchVir = (EchiquierVirtuel)listeEchiquier.get(0);
setLayout(null);
setBackground(new Color(255,255,255,255));
setOpaque(true);
add(carreau);
carreau.setBounds(largeurLabelLigne, hauteurLabelColonne, 8 * TAILLE_CASE, 8 * TAILLE_CASE);
// Create field labels
for (int n = 0; n < 8; n++) {
labelLigne[n] = new JLabel("" + (8 - n), SwingConstants.CENTER);
labelLigne[n].setFont(new Font("SansSerif", Font.ITALIC, 12));
add(labelLigne[n]);
labelLigne[n].setBounds(0, hauteurLabelColonne + n * hauteurLabelLigne, largeurLabelLigne, hauteurLabelLigne);
labelLigne[n].setForeground(new Color(0,0,0,255));
labelLigne[n + 8] = new JLabel("" + (8 - n), SwingConstants.CENTER);
labelLigne[n+8].setFont(new Font("SansSerif", Font.ITALIC, 12));
add(labelLigne[n + 8]);
labelLigne[n
+ 8].setBounds(
largeurLabelLigne + 8 * TAILLE_CASE,
hauteurLabelColonne + n * hauteurLabelLigne,
largeurLabelLigne,
hauteurLabelLigne);
labelLigne[n + 8].setForeground(new Color(0, 0,0,255));
labelColonne[n] = new JLabel("" + (char) (((int) 'a') + n), SwingConstants.CENTER);
labelColonne[n].setFont(new Font("SansSerif", Font.ITALIC, 12));
add(labelColonne[n]);
labelColonne[n].setBounds(
largeurLabelLigne + n * largeurLabelColonne,
8 * TAILLE_CASE + hauteurLabelColonne,
largeurLabelColonne,
hauteurLabelColonne);
labelColonne[n].setForeground(new Color(0, 0,0,255));
labelColonne[n + 8] = new JLabel("" + (char) (((int) 'a') + n), SwingConstants.CENTER);
labelColonne[n+8].setFont(new Font("SansSerif", Font.ITALIC, 12));
add(labelColonne[n + 8]);
labelColonne[n + 8].setBounds(largeurLabelLigne + n * largeurLabelColonne, 0, largeurLabelColonne, hauteurLabelColonne);
labelColonne[n + 8].setForeground(new Color(0, 0,0,255));
}
// Create the background:
for (int x = 0; x < 8; x++)
for (int y = 0; y < 8; y++) {
JLabel labelArrierePlan = new JLabel();
labelArrierePlan.setOpaque(true);
labelArrierePlan.setBounds(x * TAILLE_CASE, y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
carreau.add(labelArrierePlan, new Integer(1), 0);
arrierePlan[y][x] = labelArrierePlan;
}
mettreAJour();
Dimension taillePanneau = null;
taillePanneau = new Dimension(8 * TAILLE_CASE + 2 * largeurLabelLigne, 8 * TAILLE_CASE + 2 * hauteurLabelColonne);
setPreferredSize(taillePanneau);
setMinimumSize(taillePanneau);
}
public void mettreAJour() {
EchVir = new EchiquierVirtuel();
EchVir=(EchiquierVirtuel)listeEchiquier.get(0);
redessnier();
}
public void redessnier() {
FigurePiece figure;
restaurerArrierePlan();
synchronized (EchVir) {
for (int ligne = 0; ligne < 8; ligne++) {
for (int colonne = 0; colonne < 8; colonne++) {
int type = EchVir.champs[(ligne << 3) + colonne];
if (type != EchiquierVirtuel.CV) {
figure = new FigurePiece(type);
carreau.add(figure, new Integer(2), 0);
figure.setChamps(ligne, colonne);
}
}}}
}
} |
Partager