Bonjour,
J'ai un petit soucis, j'ai développé un mini jeu type "le pendu", j'ai tout fait (ou presque) le mode console ça fonctionne bien.
Maintenant je veux créer une interface graphique, mais ironie du sort je n'arrive pas à voir les choses simplements afin de pouvoir joindre mon interface avec mon code console.
Pouvez vous me conseiller ? Voici mon code :
Classe Lanceur :
Classe Mots :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import graphique.Fenetre; import java.io.IOException; import java.io.InputStreamReader; public class Lanceur { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Fenetre fen = new Fenetre(); fen.setVisible(true); Mots mots = new Mots("test"); mots.afficherMot(); while(true){ InputStreamReader isr = new InputStreamReader(System.in); try { mots.verificationMot((char) isr.read()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
classe Score :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Mots { private String mot; private ArrayList<Character> listCaractereMotBase = new ArrayList<Character>(); private char[] tabReponse; Score score = new Score(0); Integer erreurs = 0; //constructeur du mot public Mots(String mot){ this.mot = mot; System.out.println("mot.length = "+mot.length()); tabReponse = new char[mot.length()]; for(int i=0; i<mot.length(); i++){ listCaractereMotBase.add(mot.charAt(i)); System.out.println("contenu listCaractereMotBase "+listCaractereMotBase.get(i)+" a l'index "+i); } } //verification du mot avec les caracteres public void verificationMot(char caractere){ if(listCaractereMotBase.contains(caractere)){ score.setScore(score.getScore()+1); int indexReponse = listCaractereMotBase.indexOf(caractere); // System.out.println("valeur indexReponse : "+indexReponse); tabReponse[indexReponse] = caractere; listCaractereMotBase.set(indexReponse, null); // System.out.println("contenu à l'index reponse dans le tableau reponse "+tabReponse[indexReponse]); this.afficherMot(); if(tabReponse.length == score.getScore()){ System.out.println("bravo ! Tu as trouvé le mot caché !"); System.exit(0); } }else{ erreurs += 1; System.out.println("nombre d'erreurs "+erreurs); if(erreurs == 5){ System.out.println("Tu as perdu, tu es pendu :-P ! \"q\" pour quitter "); BufferedReader entree = new BufferedReader(new InputStreamReader(System.in)); try { String reponse = entree.readLine(); if(reponse == "q"){ System.exit(0); } } catch( IOException e ) { e.printStackTrace(); } finally{ System.exit(0); } } } } //affichage du contenu du mot et la liste de caracteres public void afficherMot(){ //System.out.println("le mot est : "+mot); //System.out.println("le premier caractere du mot est : "+ listCaractereMotBase.get(0)); System.out.println("Veux tu m'aider à trouver ce mot cachée ? essaies une lettre, Bonne Chance !"); System.out.println("Attention tu as le droit qu'à 5 erreurs ! "); for(int i=0; i<tabReponse.length; i++){ System.out.println("contenu tabReponse : "+tabReponse[i]); } System.out.println("score : "+score.getScore()); } }
Classe Fenetre :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public class Score { private Integer score = null; public Score(Integer score){ this.setScore(score); } public Integer getScore() { return score; } public void setScore(Integer score) { this.score = score; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package graphique; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Fenetre extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 1L; private JLabel motSecret = new JLabel(); private JLabel reponse = new JLabel("réponse : "); private JTextField saisiReponse = new JTextField(2); private JLabel erreurs = new JLabel(); private JLabel score = new JLabel(); private JButton bouttonValider = new JButton("Valider"); public Fenetre(){ Container ct = new Container(); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(700, 700); this.setTitle("Jeu du pendu"); ct.setLayout(new GridLayout(4, 1)); this.add(ct); //*********************** definition jpanel1 ***************************************** JPanel panneau = new JPanel(); panneau.setBackground(Color.lightGray); ct.add(panneau); //ajout des différents éléments panneau.add(motSecret); //*********************** definition jpanel2 ***************************************** JPanel panneau2 = new JPanel(); panneau2.setBackground(Color.lightGray); ct.add(panneau2); panneau2.add(reponse); panneau2.add(saisiReponse); //*********************** definition jpanel3 ***************************************** JPanel panneau3 = new JPanel(); panneau3.setBackground(Color.lightGray); ct.add(panneau3); panneau3.add(erreurs); //*********************** definition jpanel3 ***************************************** JPanel panneau4 = new JPanel(); panneau4.setBackground(Color.lightGray); ct.add(panneau4); panneau4.add(score); panneau4.add(bouttonValider); bouttonValider.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println("je clique sur le bouton valider"); } }
Partager