Ajouter un commentaire

olihya
Inversion de cases dans un jtable

Hello Niroken, :D
Voici le code.
J'ai laissé uniquement le niveau 4.
Pour ce qui est de l'inversion du tableau pour faire la rotation de la grille sur la diagonale, j'ai essayé mais sans résultat pour le moment.
C'est pour ça que j'essayai d'enchainer les 2 méthodes pour arriver au même résultat.

package reverso;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Fenetre extends JFrame implements ActionListener, Icon {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel pan = new JPanel();
	private JMenuBar barreMenus;
	private JMenu niveaux;
	private JMenu raz0;
	private JMenu solu;
	private JMenuItem raz;
	private JMenuItem niv11, niv12, niv13, niv14;
	private JMenuItem niveau1, niveau2, niveau3, niveau4;
	private ImageIcon b1;
	private ImageIcon b2;
	private JButton[] b;
	private String selectedNiveau;

	// mise en place de la fenetre
	public Fenetre() {

		this.setTitle("Reverso");
		this.setSize(450, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(true);
		this.setLocationRelativeTo(null);
		// mise en place des menus
		barreMenus = new JMenuBar();
		setJMenuBar(barreMenus);
		niveaux = new JMenu("Niveaux");
		raz0 = new JMenu("Reset");
		solu = new JMenu("Solutions");
		barreMenus.add(niveaux);
		barreMenus.add(raz0);
		barreMenus.add(solu);
		raz = new JMenuItem("Raz");
		raz0.add(raz);
		raz.addActionListener(this);
		niv11 = new JMenuItem("Niveau 1.1");
		solu.add(niv11);
		niv11.addActionListener(this);
		niv12 = new JMenuItem("Niveau 1.2");
		solu.add(niv12);
		niv12.addActionListener(this);
		niv13 = new JMenuItem("Niveau 1.3");
		solu.add(niv13);
		niv13.addActionListener(this);
		niv14 = new JMenuItem("Niveau 1.4");
		solu.add(niv14);
		niv14.addActionListener(this);
		niveau1 = new JMenuItem("Niveau1");
		niveaux.add(niveau1);
		niveau1.addActionListener(this);
		niveau2 = new JMenuItem("Niveau2");
		niveaux.add(niveau2);
		niveau2.addActionListener(this);
		niveau3 = new JMenuItem("Niveau3");
		niveaux.add(niveau3);
		niveau3.addActionListener(this);
		niveau4 = new JMenuItem("Niveau4");
		niveaux.add(niveau4);
		niveau4.addActionListener(this);
		// mise en place du gridLayout

		// Message de bienvenue
		JOptionPane
				.showMessageDialog(pan,
						"<html>BIENVENU DANS LE JEU <BR>VEUILLEZ SELECTIONNER UN NIVEAU.</html>");
		// positionnement des boutons sur le plateaux dans un tableau à une
		// dimension sur un gridlayout
		JButton[] b = new JButton[25];
		this.b = b;
		for (int i = 0; i < b.length; i++) {
			b1 = new ImageIcon(getClass().getResource("b1.gif"));
			b2 = new ImageIcon(getClass().getResource("b2.gif"));
			b[i] = new JButton(b1);
			b[i].setSelectedIcon(b2);
			b[i].addActionListener(this);
			pan.setLayout(new GridLayout(5, 5));
			pan.add(b[i]);
		}
		setContentPane(pan);
	}

	{

		this.setContentPane(pan);
		this.setVisible(true);
	}

	// creation de la méthode de réinitialisation
	private void reset() {
		for (int i = 0; i < b.length; i++) {
			b[i].setSelected(false);
		}
	}

	private void resetsol() {
		for (int i = 0; i < b.length; i++) {
			b[i].setBackground(null);
		}
	}

	private void inverseCasesVerticalement(int pTabLargeur, int pTabHauteur) {
		JButton[] vButtons = new JButton[pTabLargeur * pTabHauteur];
		pan.removeAll();

		for (int i = 0; i < pTabHauteur; i++) {
			for (int j = 0; j < pTabLargeur; j++) {
				vButtons[(i * pTabHauteur) + j] = b[(i * pTabHauteur)
						+ pTabLargeur - 1 - j];
				pan.add(vButtons[(i * pTabHauteur) + j]);
			}
		}

		b = vButtons;
		pan.updateUI();
	}

	private void inverseCasesHorizontalement(int pTabLargeur, int pTabHauteur) {
		JButton[] hButtons = new JButton[pTabLargeur * pTabHauteur];
		pan.removeAll();

		for (int i = 0; i < pTabHauteur; i++) {
			for (int j = 0; j < pTabLargeur; j++) {
				hButtons[(j * pTabLargeur) + i] = b[(j * pTabLargeur)
						+ pTabHauteur - 1 - i];
				pan.add(hButtons[(j * pTabLargeur) + i]);
			}
		}

		b = hButtons;
		pan.updateUI();

	}

	private void nive1() {
		for (int i = 0; i < b.length; i++) {
			b[1].setBackground(Color.magenta);
			b[2].setBackground(Color.magenta);
			b[4].setBackground(Color.magenta);
			b[6].setBackground(Color.magenta);
			b[7].setBackground(Color.magenta);
			b[8].setBackground(Color.magenta);
			b[12].setBackground(Color.magenta);
			b[13].setBackground(Color.magenta);
			b[14].setBackground(Color.magenta);
			b[15].setBackground(Color.magenta);
			b[16].setBackground(Color.magenta);
			b[18].setBackground(Color.magenta);
			b[19].setBackground(Color.magenta);
			b[20].setBackground(Color.magenta);
			b[21].setBackground(Color.magenta);
		}
	}

	private void nive2() {
		for (int i = 0; i < b.length; i++) {
			b[3].setBackground(Color.magenta);
			b[4].setBackground(Color.magenta);
			b[5].setBackground(Color.magenta);
			b[6].setBackground(Color.magenta);
			b[8].setBackground(Color.magenta);
			b[9].setBackground(Color.magenta);
			b[10].setBackground(Color.magenta);
			b[11].setBackground(Color.magenta);
			b[12].setBackground(Color.magenta);
			b[16].setBackground(Color.magenta);
			b[17].setBackground(Color.magenta);
			b[18].setBackground(Color.magenta);
			b[20].setBackground(Color.magenta);
			b[22].setBackground(Color.magenta);
			b[23].setBackground(Color.magenta);
		}
	}

	private void nive3() {
		for (int i = 0; i < b.length; i++) {
			b[0].setBackground(Color.magenta);
			b[1].setBackground(Color.magenta);
			b[5].setBackground(Color.magenta);
			b[6].setBackground(Color.magenta);
			b[8].setBackground(Color.magenta);
			b[9].setBackground(Color.magenta);
			b[12].setBackground(Color.magenta);
			b[13].setBackground(Color.magenta);
			b[14].setBackground(Color.magenta);
			b[16].setBackground(Color.magenta);
			b[17].setBackground(Color.magenta);
			b[18].setBackground(Color.magenta);
			b[21].setBackground(Color.magenta);
			b[22].setBackground(Color.magenta);
			b[24].setBackground(Color.magenta);
		}
	}

	private void nive4() {
		for (int i = 0; i < b.length; i++) {
			b[0].setBackground(Color.magenta);
			b[2].setBackground(Color.magenta);
			b[3].setBackground(Color.magenta);
			b[6].setBackground(Color.magenta);
			b[7].setBackground(Color.magenta);
			b[8].setBackground(Color.magenta);
			b[10].setBackground(Color.magenta);
			b[11].setBackground(Color.magenta);
			b[12].setBackground(Color.magenta);
			b[15].setBackground(Color.magenta);
			b[16].setBackground(Color.magenta);
			b[18].setBackground(Color.magenta);
			b[19].setBackground(Color.magenta);
			b[23].setBackground(Color.magenta);
			b[24].setBackground(Color.magenta);
		}
	}

	// creation de la méthode testant si tout les pions sont retournés
	private boolean isAllGridSelected() {
		boolean vResult = true;

		for (int i = 0; i < b.length; i++) {
			vResult = vResult && b[i].isSelected();
		}

		return vResult;
	}

	// gestion des évènements sur les boutons
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if (source instanceof JMenuItem) {
			if (source == raz) {
				// mise en place de la méthode reset sur le JMenuitem raz
				reset();
			}
		}
		if (source instanceof JMenuItem) {
			// remise à zéro à chaque sélection de niveau
			reset();
			resetsol();
			// sélection des niveaux
			if (source == niveau1) {
				selectedNiveau = "niveau1";
				JOptionPane
						.showMessageDialog(
								niveau1,
								"<html>NIVEAU 1<br>VOUS DEVEZ RETOURNER TOUT LES PIONS.<br>SACHANT QUE LORSQUE VOUS CLIQUEZ SUR UN PION, <BR>LES PIONS AU DESSUS, EN DESSOUS ET SUR LES CÔTES SE RETOURNENT AUSSI</html>");

			} else if (source == niveau2) {
				selectedNiveau = "niveau2";
				JOptionPane
						.showMessageDialog(
								niveau2,
								"<html>NIVEAU 2<br>VOUS DEVEZ RETOURNER TOUT LES PIONS.<br>LORSQUE VOUS CLIQUEZ SUR UN PION, <BR>LES PIONS AU DESSUS, EN DESSOUS ET SUR LES CÔTES SE RETOURNENT AUSSI<BR>ET LA GRILLE PIVOTE SUR L'AXE VERTICAL.</html>");

			} else if (source == niveau3) {
				selectedNiveau = "niveau3";
				JOptionPane
						.showMessageDialog(
								niveau3,
								"<html>NIVEAU 3<br>VOUS DEVEZ RETOURNER TOUT LES PIONS.<br>LORSQUE VOUS CLIQUEZ SUR UN PION, <BR>LES PIONS AU DESSUS, EN DESSOUS ET SUR LES CÔTES SE RETOURNENT AUSSI<br>ET LA GRILLE PIVOTE SUR L'AXE HORIZONTAL.</html>");

			} else if (source == niveau4) {
				selectedNiveau = "niveau4";
				JOptionPane
						.showMessageDialog(
								niveau4,
								"<html>NIVEAU 4<br>VOUS DEVEZ RETOURNER TOUT LES PIONS.<br>LORSQUE VOUS CLIQUEZ SUR UN PION, <BR>LES PIONS AU DESSUS, EN DESSOUS ET SUR LES CÔTES SE RETOURNENT AUSSI<br>ET LA GRILLE PIVOTE SUR LA DIAGONALE.</html>");

			}
			if (source == niv11) {
				selectedNiveau = "niv11";
				JOptionPane
						.showMessageDialog(
								niv11,
								"<html>Solution 1.1<br>Selectionnez les pions ayant un fond magenta, <br>en partant du haut et en allant de gauche à droite.</html>");

			} else if (source == niv12) {
				selectedNiveau = "niv12";
				JOptionPane
						.showMessageDialog(
								niv12,
								"<html>Solution 1.2<br>Selectionnez les pions ayant un fond magenta, <br>en partant du haut et en allant de gauche à droite.</html>");

			} else if (source == niv13) {
				selectedNiveau = "niv13";
				JOptionPane
						.showMessageDialog(
								niv13,
								"<html>Solution 1.3<br>Selectionnez les pions ayant un fond magenta, <br>en partant du haut et en allant de gauche à droite.</html>");

			} else if (source == niv14) {
				selectedNiveau = "niv14";
				JOptionPane
						.showMessageDialog(
								niv14,
								"<html>Solution 1.4<br>Selectionnez les pions ayant un fond magenta, <br>en partant du haut et en allant de gauche à droite..</html>");

			}
			// Gestion des inversions de pions des différents niveaux
		} else if (source instanceof JButton) {

			if (selectedNiveau.equals("niveau1")) {
				

				// appel dela méthode isAllgGridSelected et affichage du message
				// de fin de niveau
				if (isAllGridSelected()) {
					JOptionPane
							.showMessageDialog(niveau1,
									"<html>BRAVO, C'EST GAGNE!!!<br>ESSAYEZ LE NIVEAU 2.</html>");
				}
			}
			if (selectedNiveau.equals("niveau2")) {
			

				inverseCasesVerticalement(5, 5);
				if (isAllGridSelected()) {
					JOptionPane
							.showMessageDialog(niveau2,
									"<html>BRAVO, C'EST GAGNE!!!<br>ESSAYEZ LE NIVEAU 3.</html>");
				}
			}
		}
		if (selectedNiveau.equals("niveau3")) {

	
			inverseCasesHorizontalement(5, 5);
			if (isAllGridSelected()) {
				JOptionPane
						.showMessageDialog(niveau3,
								"<html>BRAVO, C'EST GAGNE!!!<br>ESSAYEZ LE NIVEAU 4.</html>");
			}
		}
		if (selectedNiveau.equals("niveau4")) {
			for (int i = 6; i <= 8; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
				}
			}
			for (int i = 11; i <= 13; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
				}
			}

			for (int i = 16; i <= 18; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
				}
			}
			for (int i = 0; i <= 0; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
				}
			}
			for (int i = 4; i <= 4; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
				}
			}
			for (int i = 20; i <= 20; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
				}
			}
			for (int i = 24; i <= 24; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
				}
			}
			for (int i = 1; i <= 3; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
				}
			}
			for (int i = 5; i <= 5; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
				}
			}
			for (int i = 10; i <= 10; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
				}
			}
			for (int i = 15; i <= 15; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
				}
			}
			for (int i = 21; i <= 23; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i + 1].setSelected(!b[i + 1].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
				}
			}
			for (int i = 9; i <= 9; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
				}
			}
			for (int i = 14; i <= 14; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
				}
			}
			for (int i = 19; i <= 19; i++) {
				if (source == b[i]) {
					b[i].setSelected(!b[i].isSelected());
					b[i - 1].setSelected(!b[i - 1].isSelected());
					b[i - 5].setSelected(!b[i - 5].isSelected());
					b[i + 5].setSelected(!b[i + 5].isSelected());
				}
			}

			inverseCasesHorizontalement(5, 5);
			inverseCasesVerticalement(5, 5);

			if (isAllGridSelected()) {
				JOptionPane
						.showMessageDialog(niveau4,
								"<html>BRAVO, C'EST GAGNE!!!<br>REVERSO N'A PLUS DE SECRET POUR VOUS.</html>");
			}
		}
		if (selectedNiveau.equals("niv11")) {
			nive1();
			

			// appel dela méthode isAllgGridSelected et affichage du message de
			// fin de niveau
			if (isAllGridSelected()) {
				JOptionPane.showMessageDialog(niv11,
						"<html>ESSAYEZ LA SOLUTION DU NIVEAU 1.2</html>");
			}
		}
		if (selectedNiveau.equals("niv12")) {
			nive2();
			

			// appel dela méthode isAllgGridSelected et affichage du message de
			// fin de niveau
			if (isAllGridSelected()) {
				JOptionPane.showMessageDialog(niv12,
						"<html>ESSAYEZ LA SOLUTION DU NIVEAU 1.3</html>");
			}
		}
		if (selectedNiveau.equals("niv13")) {
			nive3();
			
			// appel dela méthode isAllgGridSelected et affichage du message de
			// fin de niveau
			if (isAllGridSelected()) {
				JOptionPane.showMessageDialog(niv13,
						"<html>ESSAYEZ LA SOLUTION DU NIVEAU 1.4</html>");
			}
		}
		if (selectedNiveau.equals("niv14")) {
			nive4();
			
			// appel dela méthode isAllgGridSelected et affichage du message de
			// fin de niveau
			if (isAllGridSelected()) {
				JOptionPane.showMessageDialog(niv14,
						"<html>ESSAYEZ LA SOLUTION DU NIVEAU 2.1.</html>");
			}
		}
	}

	@Override
	public int getIconHeight() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getIconWidth() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		// TODO Auto-generated method stub

	}

}

Bonne soirée

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 TTTTTT  W     W   AA    AA   FFFF 
TT W W A A A A F
TT W W W AAAA AAAA FFF
TT W W W A A A A F
TT W W A A A A F