Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package cartoosh;
- import java.net.URL;
- import java.util.ResourceBundle;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.TextField;
- import javafx.scene.control.Label;
- import javafx.scene.Group;
- import javafx.scene.image.*;
- import java.io.File;
- import java.sql.*;
- import java.io.InputStream;
- import java.util.List;
- import javax.swing.SwingUtilities;
- import java.io.*;
- import java.util.ArrayList;
- /**
- *
- * @author remyl
- */
- public class MainController implements Initializable {
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost/cartoosh";
- private int idx = 1;
- // Database credentials
- static final String DB_USER = "root";
- static final String DB_PASS = "";
- private Connection conn = null;
- private Statement stmt = null;
- private String img;
- private ArrayList<String> imgGameList = new ArrayList();
- private ArrayList<String> nameGameList = new ArrayList();
- private GameController Game;
- private UserController User;
- private ScoreController Score;
- @FXML
- private List<ImageView> gameList;
- @FXML
- private List<Label> gameLabels;
- @FXML
- private List<Label> scoreLabels;
- @FXML
- private List<Label> gameTitles;
- //On initialise la liste des ImageView - ArrayList du fichier XML
- @FXML
- private Label welcome;
- @FXML
- private Label notifUser;
- @FXML
- private Group authBlock;
- @FXML
- private Group userBlock;
- @FXML
- private TextField userLogin;
- @FXML
- private TextField userCode;
- @FXML
- public void onEnterCredentials(ActionEvent ae) {
- loginUser();
- }
- @FXML
- public void onPlayClick(ActionEvent ae) {
- System.out.println(ae.getTarget());
- }
- public void initialize() {
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- UserController User = new UserController(conn);
- userBlock.setVisible(false);
- User.setLogin("Guest","");
- welcome.setText("Bienvenue " + User.getUserName());
- try{
- Class.forName(JDBC_DRIVER);
- conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
- stmt = conn.createStatement();
- String sql = "SELECT * FROM game;";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- int id = rs.getInt("game_id");
- String name = rs.getString("game_name");
- String author = rs.getString("game_author");
- String img = rs.getString("game_img");
- imgGameList.add(img);
- nameGameList.add(name);
- System.out.println(id);
- }
- System.out.println(imgGameList);
- for (int i = 0; i < gameList.size(); i++) {
- InputStream input = MainController.class.getResourceAsStream("res/" + imgGameList.get(i));
- Image image = new Image(input);
- gameList.get(i).setImage(image);
- System.out.println(gameList.get(i).toString());
- }
- for (int i = 0; i < gameLabels.size(); i++) {
- gameLabels.get(i).setText(nameGameList.get(i));
- }
- rs.close();
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("EXCEPTION se");
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class.forName
- System.out.println("EXCEPTION e");
- e.printStackTrace();
- }finally{
- //finally block used to close resources
- }
- System.out.println("On a fini");
- }
- public void loginUser(){
- User = null;
- Game = null;
- //P = null;
- UserController User = new UserController(conn);
- boolean loginUser = User.setLogin(userLogin.getText(),userCode.getText());
- if(loginUser){
- GameController Game = new GameController(conn);
- ScoreController Score = new ScoreController(conn,User.getID());
- authBlock.setVisible(false);
- welcome.setText("Bienvenue " + User.getUserName());
- userBlock.setVisible(true);
- int i = 1;
- for (Label title : gameTitles) {
- title.setText(Game.getGameName(i));
- i++;
- }
- i = 1;
- for (Label score : scoreLabels) {
- System.out.println("SCORE --- " +i);
- score.setText("" +Score.getScore(i));
- i++;
- }
- } else {
- notifUser.setText("Identifiants incorrects");
- // Game.launchGame(5);
- }
- }
- public void launchGame(int gameID){
- GameController game = new GameController(conn);
- try {
- game.launchGame(gameID);
- } catch(Exception e){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement