Advertisement
Guest User

Untitled

a guest
Feb 5th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package cartoosh;
  7.  
  8. import java.net.URL;
  9. import java.util.ResourceBundle;
  10. import javafx.event.ActionEvent;
  11. import javafx.fxml.FXML;
  12. import javafx.fxml.Initializable;
  13. import javafx.scene.control.TextField;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.Group;
  16. import javafx.scene.image.*;
  17. import java.io.File;
  18. import java.sql.*;
  19. import java.io.InputStream;
  20. import java.util.List;
  21. import javax.swing.SwingUtilities;
  22. import java.io.*;
  23. import java.util.ArrayList;
  24.  
  25. /**
  26.  *
  27.  * @author remyl
  28.  */
  29. public class MainController implements Initializable {
  30.    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  31.    static final String DB_URL = "jdbc:mysql://localhost/cartoosh";
  32.    private int idx = 1;
  33.    //  Database credentials
  34.    static final String DB_USER = "root";
  35.    static final String DB_PASS = "";
  36.    
  37.    private Connection conn = null;
  38.    private Statement stmt = null;
  39.    
  40.    private String img;
  41.    private ArrayList<String> imgGameList = new ArrayList();
  42.    private ArrayList<String> nameGameList = new ArrayList();
  43.    private GameController Game;
  44.    private UserController User;
  45.    private ScoreController Score;
  46.    
  47.     @FXML
  48.     private List<ImageView> gameList;
  49.     @FXML
  50.     private List<Label> gameLabels;
  51.     @FXML
  52.     private List<Label> scoreLabels;
  53.     @FXML
  54.     private List<Label> gameTitles;
  55. //On initialise la liste des ImageView - ArrayList du fichier XML
  56.    
  57.      @FXML
  58.      private Label welcome;
  59.      
  60.      @FXML
  61.      private Label notifUser;
  62.      
  63.      @FXML
  64.      private Group authBlock;
  65.      
  66.      @FXML
  67.      private Group userBlock;
  68.    
  69.      @FXML
  70.      private TextField userLogin;
  71.      
  72.      @FXML
  73.      private TextField userCode;
  74.      
  75.      @FXML
  76.      public void onEnterCredentials(ActionEvent ae) {
  77.       loginUser();
  78.     }
  79.    
  80.     @FXML
  81.      public void onPlayClick(ActionEvent ae) {
  82.       System.out.println(ae.getTarget());
  83.     }
  84.     public void initialize() {
  85.     }
  86. @Override
  87.     public void initialize(URL url, ResourceBundle rb) {
  88.         UserController User = new UserController(conn);
  89.         userBlock.setVisible(false);
  90.         User.setLogin("Guest","");
  91.         welcome.setText("Bienvenue " + User.getUserName());
  92.         try{
  93.             Class.forName(JDBC_DRIVER);
  94.             conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
  95.             stmt = conn.createStatement();
  96.  
  97.             String sql = "SELECT * FROM game;";
  98.             ResultSet rs = stmt.executeQuery(sql);
  99.             while(rs.next()){
  100.  
  101.                int id  = rs.getInt("game_id");
  102.                String name = rs.getString("game_name");
  103.                String author = rs.getString("game_author");
  104.                String img = rs.getString("game_img");
  105.                 imgGameList.add(img);
  106.                 nameGameList.add(name);
  107.                
  108.                 System.out.println(id);
  109.                
  110.             }
  111.            
  112.             System.out.println(imgGameList);
  113.            
  114.             for (int i = 0; i < gameList.size(); i++) {
  115.                     InputStream input = MainController.class.getResourceAsStream("res/" + imgGameList.get(i));
  116.                     Image image = new Image(input);
  117.                     gameList.get(i).setImage(image);
  118.                     System.out.println(gameList.get(i).toString());
  119.             }
  120.            for (int i = 0; i < gameLabels.size(); i++) {
  121.                     gameLabels.get(i).setText(nameGameList.get(i));
  122.            }
  123.             rs.close();
  124.              
  125.         }catch(SQLException se){
  126.            //Handle errors for JDBC
  127.                  System.out.println("EXCEPTION se");
  128.             se.printStackTrace();
  129.         }catch(Exception e){
  130.       //Handle errors for Class.forName
  131.             System.out.println("EXCEPTION e");
  132.             e.printStackTrace();
  133.         }finally{
  134.       //finally block used to close resources
  135.            
  136.         }
  137.               System.out.println("On a fini");
  138.     }
  139.       public void loginUser(){
  140.           User = null;
  141.           Game = null;
  142.           //P = null;
  143.           UserController User = new UserController(conn);
  144.    
  145.           boolean loginUser = User.setLogin(userLogin.getText(),userCode.getText());
  146.           if(loginUser){
  147.             GameController Game = new GameController(conn);
  148.             ScoreController Score = new ScoreController(conn,User.getID());
  149.             authBlock.setVisible(false);
  150.             welcome.setText("Bienvenue " + User.getUserName());
  151.             userBlock.setVisible(true);
  152.             int i = 1;
  153.             for (Label title : gameTitles) {
  154.                     title.setText(Game.getGameName(i));
  155.                 i++;
  156.             }
  157.             i = 1;
  158.             for (Label score : scoreLabels) {
  159.                 System.out.println("SCORE --- " +i);
  160.                     score.setText("" +Score.getScore(i));
  161.                 i++;
  162.             }
  163.          
  164.        
  165.           } else {
  166.              
  167.             notifUser.setText("Identifiants incorrects");        
  168.      //       Game.launchGame(5);
  169.           }
  170.      
  171.           }
  172.           public void launchGame(int gameID){
  173.               GameController game = new GameController(conn);
  174.               try {
  175.                 game.launchGame(gameID);
  176.               } catch(Exception e){
  177.               }
  178.           }
  179.      
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement