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
| package game;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
/**
*
* @author Florent
*/
public class Calcul extends Parent {
public Calcul(){
CalculGUI();
}
public GridPane CalculGUI(){
/*
Initialisation du Pane principal de la classe
*/
GridPane calculPane = new GridPane();
calculPane.setHgap(30.0);
calculPane.setVgap(30.0);
calculPane.setPadding(new Insets(50));
calculPane.setMinWidth(200);
/*
Initialisation d'un Pane secondaire dans lequel se trouve
un groupe de bouton radio pour le choix du niveau de difficulté.
Avec peronnalisation des boutons radio via du CSS
*/
VBox calculLevel = new VBox(2);
final ToggleGroup rbGroupLevel = new ToggleGroup();
RadioButton rbEasyLevel = new RadioButton("Niveau Facile");
RadioButton rbDifficultLevel = new RadioButton("Niveau Difficile");
rbDifficultLevel.setStyle("-fx-font: 24 arial;");
rbEasyLevel.setStyle("-fx-font: 24 arial;");
rbEasyLevel.setToggleGroup(rbGroupLevel);
rbDifficultLevel.setToggleGroup(rbGroupLevel);
calculLevel.getChildren().addAll(rbEasyLevel,rbDifficultLevel);
/*
Initialisation des différents éléments visuel avec utilisation du CSS
*/
Label calcul = new Label("Opèration : ");
calcul.setStyle("-fx-font: 24 arial;");
Label lblLigneCalcul = new Label("");
Label lblTextResult = new Label("");
lblLigneCalcul.setStyle("-fx-font: 24 arial;");
Label resultat = new Label("Rèponse :");
resultat.setStyle("-fx-font: 24 arial;");
Label lblMsgChoixLevel = new Label("Veuillez choisir votre niveau de difficulté");
lblMsgChoixLevel.setStyle("-fx-font: 25 arial;");
Label lblAfficheResult = new Label("");
lblAfficheResult.setStyle("-fx-font: 20 arial;");
lblAfficheResult.setVisible(false);
TextField tbResult = new TextField();
tbResult.setPrefHeight(50);
tbResult.setFont(Font.font ("Verdana", 20));
/*
Initialisation d'un Pane secondaire dans lequel se trouve
un groupe de boutons : "Valider", "Solution" et "Autre calcul"
*/
HBox boutons = new HBox();
Button btnValider = new Button("Valider");
btnValider.setPrefSize(100, 50);
Button btnSolution = new Button("Solution");
btnSolution.setPrefSize(100, 50);
Button btnAutreCalcul = new Button("Autre Calcul");
btnAutreCalcul.setPrefSize(100, 50);
boutons.getChildren().addAll(btnValider, btnSolution, btnAutreCalcul);
/*
'Acrochage' des différents éléments graphique dans le Pane principal
*/
calculPane.setConstraints(calculLevel, 1,0);
calculPane.setConstraints(calcul, 0,1);
calculPane.setConstraints(lblLigneCalcul, 1,1);
calculPane.setConstraints(resultat, 0,2);
calculPane.setConstraints(tbResult, 1,2);
calculPane.setConstraints(boutons, 1,3);
calculPane.setConstraints(lblTextResult, 1,4);
calculPane.setConstraints(lblAfficheResult, 1,5);
calculPane.setConstraints(lblMsgChoixLevel, 1,6);
calculPane.getChildren().add(calculLevel);
calculPane.getChildren().add(calcul);
calculPane.getChildren().add(lblLigneCalcul);
calculPane.getChildren().add(resultat);
calculPane.getChildren().add(tbResult);
calculPane.getChildren().add(boutons);
calculPane.getChildren().add(lblTextResult);
calculPane.getChildren().add(lblMsgChoixLevel);
calculPane.getChildren().add(lblAfficheResult);
/*
Evenements en fonction du choix de difficulté au nibeau des boutons radio
*/
rbGroupLevel.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
{
@Override
public void changed(ObservableValue<? extends Toggle> ov, Toggle t, Toggle t1)
{
lblMsgChoixLevel.setVisible(false);
lblAfficheResult.setVisible(false);
lblTextResult.setVisible(false);
tbResult.setDisable(false);
int diff;
int nbrOperateur;
int nbrTemp = 0;
int resultat;
RadioButton chk = (RadioButton)t1.getToggleGroup().getSelectedToggle(); // Cast object to radio button
if ("Niveau Difficile".equals(chk.getText())) {
diff = 100;
nbrOperateur = 3;
tbResult.setText("");
}
else{
diff = 9;
nbrOperateur = 2;
tbResult.setText("");
}
int nbr1 = randomNumber(diff);
int nbr2 = randomNumber(diff);
int operator = randomNumber(nbrOperateur);
if ("Niveau Facile".equals(chk.getText())) {
if (nbr1 < nbr2) {
nbr1 = nbrTemp;
nbr1 = nbr2;
nbr2 = nbrTemp;
}
}
Calcul myOperation = new Calcul();
String operateur;
switch (operator) {
case 1:
operateur = "+";
resultat = myOperation.Add(nbr1,nbr2);
break;
case 2:
operateur = "-";
resultat = myOperation.Sous(nbr1,nbr2);
break;
case 3:
operateur = "x";
resultat = myOperation.Multi(nbr1,nbr2);
break;
default:
throw new AssertionError();
}
String ope = (nbr1 + " " + operateur + " " + nbr2);
lblLigneCalcul.setText(ope);
String res = Integer.toString(resultat);
btnValider.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
String msg;
String txtColor;
if ("".equals(tbResult.getText())) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("");
alert.setHeaderText(null);
alert.setContentText("Veuillez saisir une réponse");
alert.showAndWait();
}else{
if (tbResult.getText().equals(res)) {
msg = "Bravo";
txtColor = "#00ff00";
}else{
msg = "Dommage";
txtColor = "#ff0000";
lblAfficheResult.setText("La solution était : " + res);
lblAfficheResult.setVisible(true);
}
lblTextResult.setText(msg);
lblTextResult.setStyle("-fx-font: 60 arial;");
lblTextResult.setTextFill(Color.web(txtColor));
rbEasyLevel.setSelected(false);
rbDifficultLevel.setSelected(false);
lblMsgChoixLevel.setVisible(true);
lblTextResult.setVisible(true);
tbResult.setDisable(true);
}
}
});
/*
Evenement sur clic du bouton permettant d'affichier la solution
*/
btnSolution.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
String msg;
if (lblLigneCalcul.getText().isEmpty()) {
msg = "Je ne peux pas donner de solution qi il n'y a pas de question !!";
}else{
msg = "La solution était : " + res;
}
lblAfficheResult.setText(msg);
lblAfficheResult.setVisible(true);
rbEasyLevel.setSelected(false);
rbDifficultLevel.setSelected(false);
lblMsgChoixLevel.setVisible(true);
tbResult.setDisable(true);
}
});
/*
Evenement sur clic du bouton permettant de faire un nouveau calcul
*/
btnAutreCalcul.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
rbEasyLevel.setSelected(false);
rbDifficultLevel.setSelected(false);
lblMsgChoixLevel.setVisible(true);
tbResult.setDisable(true);
lblLigneCalcul.setText("");
lblMsgChoixLevel.setVisible(true);
lblAfficheResult.setVisible(false);
lblTextResult.setVisible(false);
}
});
}
});
return calculPane;
}
/*
Methode qui retourne de chiffres aléatoire
*/
public int randomNumber(int b){
int result;
int a = 1;
result = a + (int)(Math.random()*b);
return result;
}
/*
Méthode de calcul d'addition
*/
public int Add(int a, int b){
int result;
result = a + b;
return result;
}
/*
Méthode de calcul de soustraction
*/
public int Sous(int a, int b){
int result;
result = a - b;
return result;
}
/*
Méthode de calcul de miltiplication
*/
public int Multi(int a, int b){
int result;
result = a * b;
return result;
}
} |
Partager