Petit jeux pour un exo (besoin de votre avis) Merci

Résolu
seichan94 Messages postés 38 Date d'inscription mardi 29 octobre 2013 Statut Membre Dernière intervention 6 novembre 2014 - Modifié par seichan94 le 25/11/2013 à 15:59
seichan94 Messages postés 38 Date d'inscription mardi 29 octobre 2013 Statut Membre Dernière intervention 6 novembre 2014 - 26 nov. 2013 à 09:12
Bonjour à tous,

Je suis débutant en java et dans le but travailler avec les bases de java on me demande de faire un jeux en JAVA basé sur JankenPon ou pierre feuille ciseaux.

donc pas de problème j'ai fait le code demandé, mais je trouve mon code bordélique et je suis sur qu'on peux enlever ce qui ne sert a rien avec une bonne méthode de conception....

bref je souhait une analyse critique de ce code.

Objectif slimfast :).

Merci par avances pour vos critiques.


le code: Fenetre.java
package jankenpon;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Fenetre extends JFrame {
 
 /// variable
 protected int scoreH = 0;
 protected int scoreCom = 0;
 protected int scoreHS = 0;
 protected int scoreComS = 0;
 protected int mancheH = 0;
 protected int mancheCom = 0;
 protected JLabel labScoreComN = new JLabel("0");
 protected JLabel labScoreHN = new JLabel("0");
 protected Image imgCom;
 protected Image imgH;
 protected Image imgStCom;
 protected Image imgStH;
 protected Image imgRules;
 protected Image imgDuel;
 protected JPanel panContainer = new JPanel();
 protected JPanel panAction = new JPanel();
 protected JPanel panActionCom = new JPanel();
 protected JLabel picLabelCom;
 protected JPanel panActionH = new JPanel();
 protected JLabel picLabelH;
 protected JPanel panActionComSt = new JPanel();
 protected JPanel panActionHSt = new JPanel();
 protected JLabel picLabelDuel;
 protected JLabel picLabelStatutH;
 protected JLabel picLabelStatutCom;
 protected JLabel labSet = new JLabel("Jouer !");
 protected JLabel labManche = new JLabel("Humain 0 manche à 0");
 protected JPanel panBox = new JPanel();
 protected JPanel panPerso = new JPanel();
 protected JPanel panWin = new JPanel();
 protected JPanel panBt = new JPanel();
 protected JPanel panScoreSet = new JPanel();
 protected JPanel panActionDuel = new JPanel();
 protected JLabel labDuel = new JLabel("Oh! Egalité !");
 protected JLabel labWin = new JLabel("Have a winner ?!");
 protected String choixCom;
 protected String choixH;
 protected JLabel labVS = new JLabel("VS");
 protected JLabel labWinOrLooseH = new JLabel();
 protected JLabel labWinOrLooseCom = new JLabel();
 protected int egaliteI = 0;
 protected int nbrT = 5;
 protected int nbrS = 3;
 protected int nbrManche = 3;
 protected int nbrMatch = 1;
 protected int nbrSE = 1;
 protected boolean winner = false;
 protected int adv;
 protected int chalanger;
 protected String win = "Win !";
 protected String loose = "Loose !";
 protected boolean start = false;
 protected boolean rez = false;
 protected Thread tAnnonce;
 protected int etatH = 5;
 protected int etatCom = 5;
 protected String imgPath = "/";
 protected String com;
 protected String player;
 protected JLabel labMatch = new JLabel("Match en "+nbrManche+"Manche | "+nbrS+"set/Manche | "+nbrT+"pt/Set |"+nbrSE+"set d'écart");
 Bouton bPerso1;
 Bouton bPerso2;
 Bouton bPerso3;
 Bouton b1;
 Bouton b2;
 Bouton b3;
 int nbrLigntabHMM = 1;
 int nbrLigntabHM;
 String tabHScoreMatchM1[][] = new String[4][nbrLigntabHMM];
 // fin variable */
 
 /// affichage 
 public Fenetre() {
  this.setTitle("Jan! Ken! Pon!");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     this.setLocation(0, 0);
     this.setResizable(false);
     this.setSize(600, 700);

     panContainer.setLayout(new BorderLayout());
     
     JPanel panScore = new JPanel();
     panScore.setLayout(new BorderLayout());
     panScore.setBackground(Color.black);
     
     Font fontlabScore = new Font("Arial", Font.BOLD, 15);
     labSet.setFont(fontlabScore);
     labMatch.setFont(fontlabScore);
     labManche.setFont(fontlabScore);
     
     JPanel panScoreManche = new JPanel();
     panScoreManche.setLayout(new BorderLayout());
     panScoreManche.setBackground(Color.lightGray);
     panScoreManche.add(labManche, BorderLayout.NORTH);
     panScoreManche.add(labMatch, BorderLayout.CENTER);
     
     panScoreSet.setBackground(Color.lightGray);
     panScore.add(panScoreSet, BorderLayout.NORTH);
     panScoreSet.setPreferredSize(new Dimension(this.getWidth(), 65));
     
     JPanel panScoreCom = new JPanel();
     panScoreCom.setLayout(new BorderLayout());
     panScoreCom.setPreferredSize(new Dimension(this.getWidth()/2-11, 20));
     panScore.add(panScoreCom, BorderLayout.EAST);
     JLabel labScoreCom = new JLabel("Score COM : ");
     labScoreCom.setForeground(Color.white);
     panScoreCom.add(labScoreCom, BorderLayout.WEST);
     panScoreCom.add(labScoreComN, BorderLayout.CENTER);
     labScoreComN.setForeground(Color.white);
     panScoreCom.setBackground(Color.red);
     
     JPanel panScoreH = new JPanel();
     panScoreH.setLayout(new BorderLayout());
     panScoreH.setPreferredSize(new Dimension(this.getWidth()/2-11, 20));
     panScore.add(panScoreH, BorderLayout.WEST);
     JLabel labScoreH = new JLabel("Score Humain : ");
     labScoreH.setForeground(Color.white);
     labScoreHN.setAlignmentX(CENTER_ALIGNMENT);
     panScoreH.add(labScoreH, BorderLayout.WEST);
     panScoreH.add(labScoreHN, BorderLayout.CENTER);
     labScoreHN.setForeground(Color.white);
     panScoreH.setBackground(Color.blue);
     
     panScoreSet.setLayout(new BorderLayout());
     panScoreSet.add(labSet, BorderLayout.NORTH);
     panScoreSet.add(panScoreManche, BorderLayout.CENTER);

     JPanel panSeparateurSet = new JPanel();
     panSeparateurSet.setBackground(Color.black);
     panSeparateurSet.setPreferredSize(new Dimension(this.getWidth(), 5));
     panScoreSet.add(panSeparateurSet, BorderLayout.SOUTH);
     
     JPanel panSeparateur = new JPanel();
     panScore.add(panSeparateur, BorderLayout.SOUTH);
     
     
     panSeparateur.setBackground(Color.black);
     panSeparateur.setPreferredSize(new Dimension(this.getWidth(), 5));
     
     panActionDuel.setBackground(Color.white);
  panActionDuel.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
     
     panAction.setLayout(new BorderLayout());
     
     panAction.add(panActionCom, BorderLayout.EAST);
     panActionCom.setBackground(Color.red);

     panAction.add(panActionH, BorderLayout.WEST);
     panActionH.setBackground(Color.blue);
     panActionH.setLayout(new BorderLayout());
     
     panActionH.setPreferredSize(new Dimension(this.getWidth()/2-(11), this.getHeight()));
     panActionCom.setPreferredSize(new Dimension(this.getWidth()/2-(11), this.getHeight()));
     panActionCom.setLayout(new BorderLayout());
     
     panActionCom.add(panActionComSt, BorderLayout.NORTH);
     panActionH.add(panActionHSt, BorderLayout.NORTH);
     
     panActionComSt.setPreferredSize(new Dimension(this.getWidth(), 250));
     panActionHSt.setPreferredSize(new Dimension(this.getWidth(), 250));
     panActionComSt.setBackground(Color.white);
     panActionHSt.setBackground(Color.white);
     panActionComSt.setLayout(new BorderLayout());
     panActionHSt.setLayout(new BorderLayout());
     
     panActionDuel.setLayout(new BorderLayout());
     
     Font fontWinOrLoose = new Font("Arial", Font.PLAIN, 20);
     labWinOrLooseCom.setFont(fontWinOrLoose);
     labWinOrLooseH.setFont(fontWinOrLoose);
     
     labWinOrLooseCom.setAlignmentX(CENTER_ALIGNMENT);
     //labWinOrLooseCom.setForeground(Color.white);
     panActionComSt.add(labWinOrLooseCom, BorderLayout.SOUTH);
     
     labWinOrLooseH.setAlignmentX(CENTER_ALIGNMENT);
     //labWinOrLooseH.setForeground(Color.white);
     panActionHSt.add(labWinOrLooseH, BorderLayout.SOUTH);
     labWinOrLooseH.setHorizontalAlignment(SwingConstants.CENTER);
     labWinOrLooseCom.setHorizontalAlignment(SwingConstants.CENTER);
     
     labVS.setHorizontalAlignment(SwingConstants.CENTER);
     
     JPanel panSeparateurBox = new JPanel();
     panSeparateurBox.setBackground(Color.black);
     panSeparateurBox.setPreferredSize(new Dimension(this.getWidth(), 5));
     panAction.add(panSeparateurBox, BorderLayout.SOUTH);
     
     panBox.setBackground(Color.white);
     panWin.setBackground(Color.white);
     panPerso.setBackground(Color.white);
     panBt.setBackground(Color.white);
     
     panBox.setVisible(true);
     panBt.setVisible(false);
     panPerso.setVisible(false);
     panWin.setVisible(false);
     
     Font fontWin = new Font("Arial", Font.PLAIN, 20);
     labWin.setFont(fontWin);
     
     bPerso1 = new Bouton("Sanji",imgPath+"wsanji.png", 175, this.getWidth());
     bPerso2 = new Bouton("Usope",imgPath+"wusope.png", 175, this.getWidth());
     bPerso3 = new Bouton("Luffy",imgPath+"wluffy.png", 175, this.getWidth());
     
     b1 = new Bouton("Jan",imgPath+"jan.png", 95, this.getWidth());
  b2 = new Bouton("Ken",imgPath+"ken.png", 95, this.getWidth());
  b3 = new Bouton("Pon",imgPath+"pon.png", 95, this.getWidth());
  
  Bouton bStop = new Bouton("Pon",imgPath+"start.jpg", 94, this.getWidth());

     panBt.add(b1);
     panBt.add(b2);
     panBt.add(b3);
     panBt.add(bStop);
     
     panWin.add(labWin);
     
     panPerso.add(bPerso2);
     panPerso.add(bPerso1);
     panPerso.add(bPerso3);
     
     panBox.add(panPerso);
     panBox.add(panWin);
     // desactivé pour gestion d'event
     //panBox.add(panBt);
     
     panAction.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()-(panScore.getWidth()+panBox.getWidth())));
     panContainer.add(panScore, BorderLayout.NORTH);
     panAction.add(panActionDuel, BorderLayout.CENTER);
     panContainer.add(panAction, BorderLayout.CENTER);
     panContainer.add(panBox, BorderLayout.SOUTH);
     this.add(panContainer);
     this.setVisible(true);
     
     b1.addActionListener(new Jan());
     b2.addActionListener(new Ken());
     b3.addActionListener(new Pon());
     bStop.addActionListener(new Restart());
     
     bPerso1.addActionListener(new Sanji());
     bPerso2.addActionListener(new Usope());
     bPerso3.addActionListener(new Luffy());

     runJKP();
 }
 // fin d'affichage */
 
 ///choix du perso
 // choix = sanji
 class Sanji implements ActionListener {
  public void actionPerformed(ActionEvent arg0) {
   //init du perso
   player = "s_";
   int n = (int) (1+2*Math.random());
   if (n==1)
    com = "u_";
   else
    com = "l_";
   initPerso();
  }
 }
 //fin sanji
 // choix = luffy
 class Luffy implements ActionListener {
  public void actionPerformed(ActionEvent arg0) {
   //init du perso
   player = "l_";
   int n = (int) (1+2*Math.random());
   if (n==1)
    com = "u_";
   else
    com = "s_";
   initPerso();
  }
 }
 // fin luffy
 //choix = usope
 class Usope implements ActionListener {
  public void actionPerformed(ActionEvent arg0) {
   //init du perso
   player = "u_";
   int n = (int) (1+2*Math.random());
   if (n==1)
    com = "l_";
   else
    com = "s_";
   initPerso();
  }
 }
 //fin usope
 // fin choix du perso */
 
 /// init Perso && init Game
 // init Perso
 private void initPerso() {
  System.out.println("------------");
  System.out.println(com);
  System.out.println(player);
  System.out.println("------------");
  // init 2 personnage 
  showPerso(1,etatH);
  showPerso(2,etatCom);
  //affiche
  refresh();
  //reinit avec le choix du joueur et com au hasard
  showPerso(1,etatH);
  showPerso(2,etatCom);
  initGame();
 }
 // fin init perso */
 // init Game
 private void initGame() {
  panBox.add(panBt);
  panBt.setVisible(true);
  panBox.setPreferredSize(new Dimension(panAction.getWidth(), 100));
  panBox.repaint();
 }
 // fin init Game
 /// fin init Perso && init Game
 
 /// affiche la regle du jeu
 private void runJKP() {
  System.out.println("run");
  Show(5, 4);
  panBox.setPreferredSize(new Dimension(panAction.getWidth(), 250));
  panBox.repaint();
 }
 // fin regle */
 
 /// restart
 class Restart implements ActionListener {
  public void actionPerformed(ActionEvent arg0) {
   refresh();
   reinitPoints();
   reinitSet();
   reinitManche();
   reinitPerso();
   refresh();
   runJKP();
  }
 }
 // fin restart */
 
 /// refresh a chaque tour
 private void refresh() {
   System.out.println("refresh");
   
   // affiche les Panel COM ET Humain
   panActionCom.setVisible(true);
   panActionH.setVisible(true);
   panPerso.setVisible(false);
   //efface les precedentes images
   panActionDuel.remove(picLabelDuel);
   // si on a deja joue un tour on efface les images 
   if (start == true) {
   panActionH.remove(picLabelH);
   panActionCom.remove(picLabelCom);
   }
   panActionHSt.remove(picLabelStatutH);
   panActionComSt.remove(picLabelStatutCom);
   // efface le lab Duel
   panActionDuel.remove(labDuel);
   // ajout VS
   panActionDuel.add(labVS, BorderLayout.CENTER);
   // on dit qu on a un gagnant au prochain refresh on efface le gagant
   if (winner == true)
    labMatch.setText("Match en 3Manche | 3set/Manche | 5pt/Set | 2set d'écart");
 }
 // fin refresh */
 
 /// add points
 private void Points(int choix, int hasard) {
  System.out.println("Add points");
  
  if(choix == hasard){
   //System.out.println("égalité");
  } else if (choix == 1) {
   if (hasard == 2) {
    scoreH+=1;
    etatH++;
    etatCom--;
    labWinOrLooseH.setText(win);
    labWinOrLooseCom.setText(loose);
   } else {
    scoreCom+=1;
    etatCom++;
    etatH--;
    labWinOrLooseCom.setText(win);
    labWinOrLooseH.setText(loose);
   }
  } else if (choix == 2) {
   if (hasard == 3) {
    scoreH+=1;
    etatH++;
    etatCom--;
    labWinOrLooseH.setText(win);
    labWinOrLooseCom.setText(loose);
   } else {
    scoreCom+=1;
    etatCom++;
    etatH--;
    labWinOrLooseCom.setText(win);
    labWinOrLooseH.setText(loose);
   }
  } else if (choix == 3) {
   if (hasard == 1) {
    scoreH+=1;
    etatH++;
    etatCom--;
    labWinOrLooseH.setText(win);
    labWinOrLooseCom.setText(loose);
   } else {
    scoreCom+=1;
    etatCom++;
    etatH--;
    labWinOrLooseCom.setText(win);
    labWinOrLooseH.setText(loose);
   }
  }
  
  // actualise les points 
  if(choix != hasard){
   labScoreHN.setText(""+scoreH);
   labScoreComN.setText(""+scoreCom);
  }
  
  // test si nbrT pour les points est atteint par Humain ou COM
  if (scoreH == nbrT || scoreCom == nbrT) {
   // methode set
   Set();
  }
 }
 // fin add points */
 
 /// add set
 private void Set() {
  System.out.println("Add set");
  
  // Ajoute les point de set pour Humain et COM 
  if (scoreH == nbrT) {
   //atribut le point de set a Humain
   scoreHS +=1;
   labWin.setForeground(Color.blue);
   labWin.setText("Humain à gagné le Set !");
  } else {
   //atribut le point de set a COM
   scoreComS +=1;
   labWin.setForeground(Color.red);
   labWin.setText("COM à gagné le Set !");
  }
  
  // actualise l'affichage des point de set 
  if (scoreHS < scoreComS) {
   //le est pour COM en rouge 
   labSet.setForeground(Color.red);
   labSet.setText("COM gagne "+scoreComS+" set à "+scoreHS);
   // variable pour définir qui à l'avantage
   adv = scoreComS;
   chalanger = scoreHS;
  } else {
   // le set est pour Humain en bleu
   labSet.setForeground(Color.blue);
   labSet.setText("Humain gagne "+scoreHS+" set à "+scoreComS);
   // variable pour définir qui à l'avantage
   adv = scoreHS;
   chalanger = scoreComS;
  }
     
  /// verifie si le score set >= on nombre de set definit par nbrS avec l'ecart de 2 set
  if (scoreHS >= nbrS || scoreComS >= nbrS) {
   if ((adv-chalanger) >= nbrSE) {
    // methode manche
    Manche();
   }
  }
   }
 // fin add set */
 
 /// add manche
 private void Manche(){
  if (scoreHS < scoreComS) {
   mancheCom +=1;
  } else {
   mancheH +=1;
  }
     
  // affiche le score de la manche avec le winner devant
  if (mancheH<mancheCom) {
   labManche.setForeground(Color.red);
   labManche.setText("COM gagne "+mancheCom+" manche à "+mancheH);
   labWin.setForeground(Color.red);
   labWin.setText("COM à gagné le Set et emporte la manche !");
  } else {
   labManche.setForeground(Color.blue);
   labManche.setText("Humain gagne "+mancheH+" manche à "+mancheCom);
   labWin.setForeground(Color.blue);
   labWin.setText("Humain à gagné le Set et emporte la manche !");
  }

  // si les manche == nbrManche definit
  if (mancheH == nbrManche || mancheCom == nbrManche) {
   Winner();
  }
 }
 // fin add manche
 
/// have winner
 private void Winner() {
  // Humain gagne
  if (mancheH == nbrManche) {
   labWin.setForeground(Color.blue);
   labWin.setText("HUMAIN A GAGNE LE JEUX !");
   
  // COM gagne
  } else if (mancheCom == nbrManche) {
   labWin.setForeground(Color.red);
   labWin.setText(" COM A GAGNE LE JEUX !  NIARC NIARC NIARC !");
   
  }
  // on dit qu on a un gagnant au prochain refresh on efface le gagant
  winner = true;
 }
 // fin have winner */
 
 // reinit score
 private void reinitScore() {
  if (scoreH == nbrT || scoreCom == nbrT) {
   // on reinit les points
   reinitPoints();
  }
  if (scoreHS >= nbrS || scoreComS >= nbrS) {
   if ((adv-chalanger) >= 2) {
    // on reinit les point les set
    reinitSet();
   }
  }
  // reinit manche
  if (mancheH == nbrManche || mancheCom == nbrManche) {
   reinitManche();
  }
 }
 // fin reinit score
 
 /// on reinit les points et actualise le set
 private void reinitPoints() {
   System.out.println("reset Point !");
   //reinit les point COM et Humain a 0
   scoreH = 0;
   scoreCom = 0;
   // actualise l'affichage des points
   labScoreHN.setText("0");
   labScoreComN.setText("0");
   
   // reinit de l'egalité
   egaliteI = 0;
 }
 // fin reinit des points

 /// on reinit les point les set et actualise la manche
 private void reinitSet() {
   System.out.println("reset Set !");
   //reinit des set
   scoreComS = 0;
   scoreHS = 0;
   // actualise l'affichage des point de set
   labSet.setForeground(Color.blue);
   labSet.setText("Humain gagne "+scoreHS+" set à "+scoreComS);
 }
 // fin de reinit set */
 
 /// on reinit les point de manche
 private void reinitManche() {
  mancheCom = 0;
  mancheH = 0;
  // actualise l'affichage des manche
  labManche.setForeground(Color.blue);
  labManche.setText("Humain gagne "+mancheCom+" manche à "+mancheH);
  runJKP();
 }
 // fin de reinit manche */
 
 /// egalite
 private void egalite() {
  System.out.println(egaliteI);
  egaliteI++;
  if (egaliteI <= 1)
   labDuel.setText("Egalité !");
  else if (egaliteI == 2)
   labDuel.setText("Woooh "+egaliteI+"eme Egalité !");
  else if (egaliteI < 5)
   labDuel.setText("Enorme "+egaliteI+"eme Egalité !");
  else if (egaliteI >= 5)
   labDuel.setText("Whouuu quel tension ! "+egaliteI+"eme Egalité !");
 }
 // fin egalite */
 
 /// personnage
 private void showPerso(int Player, int etat) {
  //player 1 = humain
  if (Player == 1) {
   System.out.println(player);
   picLabelStatutH = new JLabel(new ImageIcon(getClass().getResource(imgPath+player+etat+".png")));
   panActionHSt.add(picLabelStatutH, BorderLayout.NORTH);
   panActionHSt.repaint();
  // player 2 = COM
  } else if (Player == 2) {
   System.out.println(com);
   picLabelStatutCom = new JLabel(new ImageIcon(getClass().getResource(imgPath+com+etat+".png")));
   panActionComSt.add(picLabelStatutCom, BorderLayout.NORTH);
   panActionComSt.repaint();
  }
 }
 // fin de personnage */
  
 /// affiche les images
 private void Show (int choix, int i) {
  if (choix == 1) {
   choixH = imgPath+"pon.png";
   choixCom = imgPath+"pon.png";
  } else if (choix == 2) {
   choixH = imgPath+"jan.png";
   choixCom = imgPath+"jan.png";
  } else if (choix == 3) {
   choixH = imgPath+"ken.png";
   choixCom = imgPath+"ken.png";
  } else if (choix == 5) {
   choixH = imgPath+"rules.png";
   choixCom = imgPath+"rules.png";
  }
  if (i == 1 || i == 2 ) {
   if (i == 1) {
    System.out.println("Show main Humain");
    /// image choix H
    picLabelH = new JLabel(new ImageIcon(getClass().getResource(choixH)));
    panActionH.add(picLabelH);
    panActionH.repaint();
   } else if (i == 2) {
    System.out.println("Show main COM");
    /// image choix COM
    picLabelCom = new JLabel(new ImageIcon(getClass().getResource(choixCom)));
    panActionCom.add(picLabelCom);
    panActionCom.repaint();
   }
   start = true;
  } else if (i == 3) {
   //Methode egalite
   egalite();
   picLabelDuel = new JLabel(new ImageIcon(getClass().getResource(choixCom)));   
   panActionDuel.add(labDuel, BorderLayout.NORTH);
   labDuel.setHorizontalAlignment(SwingConstants.CENTER);
   panActionDuel.add(picLabelDuel, BorderLayout.CENTER);
   
   panActionDuel.remove(labVS);

   panActionCom.setVisible(false);
   panActionH.setVisible(false);
   panActionDuel.repaint();
  } else if (i == 4) {
   System.out.println("Show rules");
   picLabelDuel = new JLabel(new ImageIcon(getClass().getResource(choixCom)));
   panActionDuel.add(picLabelDuel, BorderLayout.CENTER);
   
   /// choose player 
   Font fontLabDuel = new Font("Arial", Font.PLAIN, 20);
   labDuel.setFont(fontLabDuel);
   labDuel.setHorizontalAlignment(SwingConstants.CENTER);
   labDuel.setText("Choisir un personnage");
   panActionDuel.add(labDuel, BorderLayout.SOUTH);
   // fin choose player */
   panActionDuel.remove(labVS);
   panBox.remove(panBt);
   panPerso.setVisible(true);
   
   panActionCom.setVisible(false);
   panActionH.setVisible(false);
   panActionDuel.repaint();
   panBox.repaint();
  } else {
   // erreur
   System.out.println("erreur sur l'affichage des images label");
  }
 }
 // fin affiche les images */
 
 /// reinit TEST SCORE / IMAGE / PERSO
 class ReinitTest implements Runnable {
  public void run() {
   if (scoreH == nbrT || scoreCom == nbrT) {
    panBt.setVisible(false);
    panWin.setVisible(true);
    try {
     Thread.sleep(2000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    reinitScore();
    reinitPerso();
    reinitImages();
   }
   
  }
 }
 // fin reinit TEST */
 
 /// reinit des perso
 private void reinitPerso() {
  etatH = 5;
  etatCom = 5;
  panActionHSt.remove(picLabelStatutH);
  panActionComSt.remove(picLabelStatutCom);
  showPerso(1,etatH);
  showPerso(2,etatCom);
  panBt.setVisible(true);
  panWin.setVisible(false);
 }
 // fin de reinit des perso */
 
 /// reinit des perso
 private void reinitImages() {
  panActionH.remove(picLabelH);
  panActionCom.remove(picLabelCom);
  panAction.repaint();
  labWinOrLooseH.setText("Play !");
  labWinOrLooseCom.setText("Waiting for you");
 }
 // fin de reinit des perso */
 
 /// jan
 class Jan implements ActionListener {
  protected int hasard;
  protected int choix = 2;
  public void actionPerformed(ActionEvent arg0) {
   hasard = (int) (1+3*Math.random());
   System.out.println("hasard ="+hasard);
   refresh();
   if (choix == hasard) {
    Show(choix, 3);
   } else {
    Show(choix, 1);
    Show(hasard, 2);
   }
   Points(choix, hasard);
   showPerso(1,etatH);
   showPerso(2,etatCom);
   tAnnonce = new Thread(new ReinitTest());
   tAnnonce.start();
  }
 }
 // fin jan */
 
 /// ken
 class Ken implements ActionListener {
  protected int hasard;
  protected int choix = 3;
  public void actionPerformed(ActionEvent arg0) {
   hasard = (int) (1+3*Math.random());
   refresh();
   if (choix == hasard) {
    Show(choix, 3);
   } else {
    Show(choix, 1);
    Show(hasard, 2);
   }
   Points(choix, hasard);
   showPerso(1,etatH);
   showPerso(2,etatCom);
   tAnnonce = new Thread(new ReinitTest());
   tAnnonce.start();
  }
 }
 // fin ken */

 /// pon
 class Pon implements ActionListener {
  protected int hasard;
  protected int choix = 1;
  public void actionPerformed(ActionEvent arg0) {
   hasard = (int) (1+3*Math.random());
   refresh();
   if (choix == hasard) {
    Show(choix, 3);
   } else {
    Show(choix, 1);
    Show(hasard, 2);
   }
   Points(choix, hasard);
   showPerso(1,etatH);
   showPerso(2,etatCom);
   tAnnonce = new Thread(new ReinitTest());
   tAnnonce.start();
  }
 }
 // fin pon */
}


Bonton.java :

package jankenpon;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
   
public class Bouton extends JButton implements MouseListener{
 private String name;
 private Image img;

 public Bouton(String str, String imgJKP,int hight, int width) {
  super(str);
     this.name = str;
     this.setPreferredSize(new Dimension(hight, width));
     try {
         img = ImageIO.read(getClass().getResourceAsStream(imgJKP));
       } catch (IOException e) {
         e.printStackTrace();
       }
     this.addMouseListener(this);
 }
  
 public void paintComponent(Graphics g){
     Graphics2D g2d = (Graphics2D)g;
     g2d.drawImage(img, 0, 0, this);
 }
  
 public void mouseClicked(MouseEvent event) {
     //Inutile                   
 }
 
 @Override
 public void mouseEntered(MouseEvent arg0) {
  // TODO Auto-generated method stub
  
 }
 
 @Override
 public void mouseExited(MouseEvent arg0) {
  // TODO Auto-generated method stub
  
 }
 
 @Override
 public void mousePressed(MouseEvent arg0) {
  // TODO Auto-generated method stub
  
 }
 
 @Override
 public void mouseReleased(MouseEvent arg0) {
  // TODO Auto-generated method stub
  
 }  
}


JanKenPon
package jankenpon;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.SwingConstants;

public class JanKenPon {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Fenetre fen = new Fenetre();
 }

}


J'ajoute le fichier JAR c'est probablement mieux ici : https://drive.google.com/?authuser=0#folders/0B832UElD9cDCMFdaUURjZkxBaG8

2 réponses

Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
26 nov. 2013 à 08:29
Salut,

Franchement les actions + les IHM dans le même code c'est trop pour mes pauvres petits yeux...
Tu devrais partir sur un modèle MVC histoire d'y voir plus clair... et aérer ton code.

Après pour ce qui est de tes tests n'utilise pas les entiers directement, favorise le passage par des constantes qui sont plus parlantes (voir mieux par des enum)
Pour le reste je pense que le simple passage par du MVC devrait t'ouvrir les yeux sur les blocs de codes redondant/etc...

2
seichan94 Messages postés 38 Date d'inscription mardi 29 octobre 2013 Statut Membre Dernière intervention 6 novembre 2014
26 nov. 2013 à 09:12
Bonjour Twinuts
Merci pour ta réponse
j'ai un ou deux tuto qui parle de MVC je vais lire cela de suite ! :)

Modèle Vue contrôle ? il semble que ce soit parfait !

Bonne journée
0
Rejoignez-nous