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
|
package projet_tetris;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import SResultSet.SerializedResultSet;
/**
* <p>Titre : PanneauScore</p>
* <p>Description : Classe du panneau affichant les meilleurs scores </p>
* <p>Copyright : Copyright (c) 2004</p>
* <p>Société : </p>
* @author Cédric Montillot et Djilali Maghraoui
* @version 1.0
*/
public class PanneauScore extends JDialog
{
//Attribut utilisé dans la méthode de connection avec la Servlet
private JTextField txtAaa = new JTextField();
private JList listeResultat = new JList();
// Nombre de joueurs classés dans le panneau des meilleurs scores
int NbJoueurs = 5;
// Tableau contenant le nom des 5 meilleurs joueurs
String BestJoueurs[] = new String[NbJoueurs];
// Tableau contenant le score et le niveau atteint par les 5 meilleurs
// joueurs
int Attributs[][] = new int[NbJoueurs][2];
// Composants de l'interface
GridBagLayout gridBagLayoutScore = new GridBagLayout();
JButton BoutonOK = new JButton();
// Tableau de composants JLabel pour afficher la position des joueurs
JLabel [] LabelPosition = new JLabel[NbJoueurs];
// Tableau de composants JLabel pour afficher le nom des joueurs
JLabel [] LabelNom = new JLabel[NbJoueurs];
// Tableau de composants JLabel pour afficher le niveau atteint par
// les joueurs
JLabel [] LabelNiveau = new JLabel[NbJoueurs];
// Tableau de composants JLabel pour afficher le score des joueurs
JLabel [] LabelScore = new JLabel[NbJoueurs];
// Composant JLabel permettant d'afficher "Position"
JLabel LabelTxtPosition = new JLabel();
// Composant JLabel permettant d'afficher "Nom"
JLabel LabelTxtNom = new JLabel();
// Composant JLabel permettant d'afficher "Niveau"
JLabel LabelTxtNiveau = new JLabel();
// Composant JLabel permettant d'afficher "Score"
JLabel LabelTxtScore = new JLabel();
// Constructeur de la classe
public PanneauScore()
{
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void Connect_Servlet() {
String res;
String nom=txtAaa.getText();
try
{
// Connexion à la servlet
URL url=new URL("http://localhost:8080/tetris/requete");
URLConnection connexion=url.openConnection();
connexion.setDoOutput(true);
// Récupération du flux de sortie
ObjectOutputStream fluxsortie = new ObjectOutputStream(connexion.getOutputStream());
// Envoi du nom à rechercher
fluxsortie.writeObject(nom);
// Récupération du flux dentrée
ObjectInputStream fluxentree = new ObjectInputStream(connexion.getInputStream());
// Récupération du résultat de la requête
SerializedResultSet donnees=(SerializedResultSet) fluxentree.readObject();
// affichage du résultat
donnees.first();
Vector contenu=new Vector();
contenu.clear();
listeResultat.setListData(contenu);
for (int i=0; i<donnees.recordCount();i++)
{
this.BestJoueurs[i] = donnees.getString("nom");
this. Attributs[i][1] = Integer.parseInt(donnees.getString("score"));
//contenu.addElement(res);
donnees.next();
}
if (donnees.recordCount()==0)
{
/* res="Pas de personne correspondante";
contenu.addElement(res);*/
}
//listeResultat.setListData(contenu);
}
catch (Exception sql)
{
System.out.println("erreur "+sql);
}
}
private void jbInit() throws Exception {
// Variable utilisée pour fixer les scores par défaut
/* int Score = 1000;
// Initialisation du tableau contenant le nom des 5 meileurs joueurs
BestJoueurs = new String[NbJoueurs];
for(int i=0;i<NbJoueurs;i++)
BestJoueurs[i] = "Computer";
// Initialisation du tableau des attributs
Attributs = new int[NbJoueurs][2];
for(int i=0;i<NbJoueurs;i++)
{
Attributs[i][0] = 3;
Attributs[i][1] = Score;
Score = Score - 50;
}*/
Connect_Servlet();
// Initialisation des composants
this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.setTitle("Les Meilleurs Scores");
BoutonOK.setText("OK");
BoutonOK.addActionListener(new PanneauScore_BoutonOK_actionAdapter(this));
this.getContentPane().setLayout(gridBagLayoutScore);
LabelTxtPosition.setFont(new java.awt.Font("Dialog", 1, 11));
LabelTxtPosition.setForeground(Color.darkGray);
LabelTxtPosition.setVerifyInputWhenFocusTarget(true);
LabelTxtPosition.setText("Position");
LabelTxtNom.setFont(new java.awt.Font("Dialog", 1, 11));
LabelTxtNom.setHorizontalAlignment(SwingConstants.CENTER);
LabelTxtNom.setHorizontalTextPosition(SwingConstants.CENTER);
LabelTxtNom.setText("Nom");
LabelTxtNiveau.setFont(new java.awt.Font("Dialog", 1, 11));
LabelTxtNiveau.setHorizontalAlignment(SwingConstants.CENTER);
LabelTxtNiveau.setHorizontalTextPosition(SwingConstants.CENTER);
LabelTxtNiveau.setText("Niveau");
LabelTxtScore.setFont(new java.awt.Font("Dialog", 1, 11));
LabelTxtScore.setHorizontalAlignment(SwingConstants.CENTER);
LabelTxtScore.setHorizontalTextPosition(SwingConstants.CENTER);
LabelTxtScore.setText("Score");
int Pos = 1;
for(int i=0;i<NbJoueurs;i++)
{
LabelPosition[i] = new JLabel();
LabelPosition[i].setFont(new java.awt.Font("Dialog", 1, 11));
LabelPosition[i].setForeground(SystemColor.menuText);
LabelPosition[i].setHorizontalAlignment(SwingConstants.CENTER);
LabelPosition[i].setText(Pos+"-");
Pos++;
}
this.getContentPane().add(LabelTxtPosition, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 12));
this.getContentPane().add(LabelTxtNom, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 39, 0, 45), 0, 0));
this.getContentPane().add(LabelTxtNiveau, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 15, 0, 16), 0, 0));
this.getContentPane().add(LabelTxtScore, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 33, 0, 42), 0, 0));
for(int i=0;i<NbJoueurs;i++)
{
this.getContentPane().add(LabelPosition[i], new GridBagConstraints(0, i+1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
LabelNom[i] = new JLabel();
LabelNom[i].setText(BestJoueurs[i]);
this.getContentPane().add(LabelNom[i], new GridBagConstraints(1, i+1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
LabelNiveau[i] = new JLabel();
LabelNiveau[i].setText(Attributs[i][0]+"");
this.getContentPane().add(LabelNiveau[i], new GridBagConstraints(2, i+1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
LabelScore[i] = new JLabel();
LabelScore[i].setText(Attributs[i][1]+"");
this.getContentPane().add(LabelScore[i], new GridBagConstraints(3, i+1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
}
this.getContentPane().add(BoutonOK, new GridBagConstraints(1, 6, 3, 1, 0.0, 0.0
,GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(0, 67, 0, 0), 30, 0));
}
// Méthode permettant la gestion des scores
public void GestionScore(int Niveau,int Score)
{
// Parcours des meilleurs scores
for(int i=0;i<NbJoueurs;i++)
{
// Si le score réalisé par le joueur est meilleurs que l'un des
// meilleurs scores actuel
if(Score > Attributs[i][1])
{
// On décalle les enregistrements contenus dans les tableaux
// BestJoueurs et Attributs à partir de l'indice i
DecallageJoueurs(i);
String txtpos;
if(i==0) txtpos = "er";
else txtpos = "ème";
// On demande la saisie du nom du joueur
String reponse = JOptionPane.showInputDialog(this, "Vous avez réalisé le " + (i+1) + txtpos +" meilleur score!! Quel est votre nom ? ",
"Félicitations!!!!",
JOptionPane.INFORMATION_MESSAGE);
// On enregistre le nouveau score ainsi que le nom du joueur, et
// le niveau atteint
BestJoueurs[i] = reponse;
Attributs[i][0] = Niveau;
Attributs[i][1] = Score;
// Mise à jour de l'affichage des meilleurs scores avec les nouvelles
// données
MAJ_Panneau();
// On sort de la boucle
i=NbJoueurs;
}
}
}
// Méthode permettant de décaller d'un rang les enregistrements dans les
// tableaux BestJoueurs et Attributs à partir de l'indice id
public void DecallageJoueurs(int id)
{
for(int i=NbJoueurs - 1;i>id;i--)
{
BestJoueurs[i] = BestJoueurs[i-1];
Attributs[i][0] = Attributs[i-1][0];
Attributs[i][1] = Attributs[i-1][1];
}
}
// Méthode permettant la mise à jour du panneau suite à la modification
// des meilleurs joueurs
public void MAJ_Panneau()
{
for(int i=0;i<NbJoueurs;i++)
{
LabelNom[i].setText(BestJoueurs[i]);
LabelNiveau[i].setText(Attributs[i][0]+"");
LabelScore[i].setText(Attributs[i][1]+"");
}
}
// Méthode exécutée lorsque l'utilisateur clique sur le bouton OK
void BoutonOK_actionPerformed(ActionEvent e) {
// Fermeture de la fenêtre
this.dispose();
}
}
class PanneauScore_BoutonOK_actionAdapter implements java.awt.event.ActionListener {
PanneauScore adaptee;
PanneauScore_BoutonOK_actionAdapter(PanneauScore adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.BoutonOK_actionPerformed(e);
}
} |
Partager