2026-03-17 15:03:07 +01:00
|
|
|
import java.awt.*;
|
2026-03-27 14:43:31 +01:00
|
|
|
import java.net.URL;
|
|
|
|
|
import javax.imageio.ImageIO;
|
2026-03-17 15:03:07 +01:00
|
|
|
import javax.swing.*;
|
|
|
|
|
|
|
|
|
|
public class CadreDeConnexion extends JFrame {
|
|
|
|
|
|
2026-03-27 14:43:31 +01:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
// Méthode peremttant de charger(récupérer) l'image îcone depuis Ressources
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
private Image chargerImageDepuisRessource(String cheminRessource) {
|
|
|
|
|
try {
|
|
|
|
|
URL url = getClass().getResource(cheminRessource);
|
|
|
|
|
if (url != null) {
|
|
|
|
|
return ImageIO.read(url);
|
|
|
|
|
}
|
|
|
|
|
System.err.println("Ressource introuvable : " + cheminRessource);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println("Erreur chargement image : " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 15:03:07 +01:00
|
|
|
public CadreDeConnexion() {
|
2026-03-27 14:43:31 +01:00
|
|
|
|
|
|
|
|
// Image de fond de la page de connexion
|
|
|
|
|
Background panelBackground = null;
|
|
|
|
|
|
|
|
|
|
// 1. On change l'icône de CETTE fenêtre (this)
|
|
|
|
|
Image imageConnexion = chargerImageDepuisRessource("/images/icone.png");
|
|
|
|
|
// Si l'image est différent de null, on modifie l'icone de notre application
|
|
|
|
|
// Et on l'applique comme image de fond
|
|
|
|
|
if (imageConnexion != null) {
|
|
|
|
|
this.setIconImage(imageConnexion);
|
|
|
|
|
panelBackground = new Background(imageConnexion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Configuration de base
|
|
|
|
|
setTitle("ZENITH FLUX");
|
2026-03-17 15:03:07 +01:00
|
|
|
setSize(800, 600);
|
|
|
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
|
|
|
setLocationRelativeTo(null); // Centre la fenêtre
|
|
|
|
|
|
2026-03-27 14:43:31 +01:00
|
|
|
// 3. Style du panneau principal (Gris foncé)
|
|
|
|
|
// JPanel panel = new JPanel(new GridBagLayout());
|
|
|
|
|
JPanel panel = new ZoneDessin(panelBackground);
|
|
|
|
|
panel.setLayout(new GridBagLayout());
|
2026-03-17 15:03:07 +01:00
|
|
|
|
|
|
|
|
// Conteneur pour les éléments (pour les empiler verticalement)
|
|
|
|
|
Box box = Box.createVerticalBox();
|
|
|
|
|
|
2026-03-27 14:43:31 +01:00
|
|
|
// 4. Création des composants
|
2026-03-17 15:03:07 +01:00
|
|
|
JLabel titre = new JLabel("CONNEXION");
|
|
|
|
|
titre.setForeground(Color.WHITE);
|
|
|
|
|
titre.setFont(new Font("Arial", Font.BOLD, 24));
|
|
|
|
|
titre.setAlignmentX(Component.CENTER_ALIGNMENT);
|
|
|
|
|
|
|
|
|
|
JTextField userField = new JTextField(15);
|
|
|
|
|
userField.setMaximumSize(new Dimension(250, 30));
|
|
|
|
|
|
|
|
|
|
JPasswordField passField = new JPasswordField(15);
|
|
|
|
|
passField.setMaximumSize(new Dimension(250, 30));
|
|
|
|
|
|
|
|
|
|
JButton loginBtn = new JButton("Entrer");
|
|
|
|
|
loginBtn.setBackground(new Color(70, 130, 180)); // Bleu acier
|
|
|
|
|
loginBtn.setForeground(Color.WHITE);
|
|
|
|
|
loginBtn.setFocusPainted(false);
|
|
|
|
|
loginBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
|
|
|
|
|
|
2026-03-27 14:43:31 +01:00
|
|
|
// 5. Ajout des composants avec des espaces (Struts)
|
2026-03-17 15:03:07 +01:00
|
|
|
box.add(titre);
|
|
|
|
|
box.add(Box.createVerticalStrut(30)); // Espace
|
|
|
|
|
box.add(new JLabel("<html><font color='white'>Utilisateur :</font></html>"));
|
|
|
|
|
box.add(userField);
|
|
|
|
|
box.add(Box.createVerticalStrut(15));
|
|
|
|
|
box.add(new JLabel("<html><font color='white'>Mot de passe :</font></html>"));
|
|
|
|
|
box.add(passField);
|
|
|
|
|
box.add(Box.createVerticalStrut(30));
|
|
|
|
|
box.add(loginBtn);
|
|
|
|
|
box.add(Box.createVerticalStrut(10));
|
|
|
|
|
|
|
|
|
|
JButton createBtn = new JButton("Créer un compte");
|
|
|
|
|
createBtn.setBackground(new Color(70, 130, 180));
|
|
|
|
|
createBtn.setForeground(Color.WHITE);
|
|
|
|
|
createBtn.setFocusPainted(false);
|
|
|
|
|
createBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
|
|
|
|
|
box.add(createBtn);
|
|
|
|
|
|
|
|
|
|
panel.add(box); // Ajoute la boîte au centre du GridBagLayout
|
2026-03-27 14:43:31 +01:00
|
|
|
setContentPane(panel); // Branche enfin le panneau personnalisé à la fenêtre
|
2026-03-17 15:03:07 +01:00
|
|
|
|
2026-03-27 14:43:31 +01:00
|
|
|
// 6. Logique du bouton
|
2026-03-17 15:03:07 +01:00
|
|
|
loginBtn.addActionListener(e -> {
|
|
|
|
|
String user = userField.getText();
|
|
|
|
|
String pass = new String(passField.getPassword());
|
|
|
|
|
int userId = GestionBDD.verifierConnexion(user, pass);
|
|
|
|
|
|
|
|
|
|
if (userId != -1) {
|
|
|
|
|
dispose();
|
|
|
|
|
Jeu jeu = new Jeu(userId);
|
|
|
|
|
jeu.demarrer();
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(this, "Acces refuse");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
createBtn.addActionListener(evt -> {
|
|
|
|
|
JFrame createFrame = new JFrame("Creation de compte");
|
|
|
|
|
createFrame.setSize(800, 600);
|
|
|
|
|
createFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
|
|
|
createFrame.setLocationRelativeTo(null);
|
|
|
|
|
|
2026-03-27 14:43:31 +01:00
|
|
|
Image imageCreation = chargerImageDepuisRessource("/images/icone.png");
|
|
|
|
|
if (imageCreation != null) {
|
|
|
|
|
createFrame.setIconImage(imageCreation);
|
|
|
|
|
}
|
|
|
|
|
Background panelBackgroundCreation = (imageCreation != null) ? new Background(imageCreation) : null;
|
|
|
|
|
|
|
|
|
|
JPanel createPanel = new ZoneDessin(panelBackgroundCreation);
|
|
|
|
|
// JPanel createPanel = new JPanel(new GridBagLayout());
|
|
|
|
|
createPanel.setLayout(new GridBagLayout());
|
|
|
|
|
createFrame.setContentPane(createPanel);
|
2026-03-17 15:03:07 +01:00
|
|
|
|
|
|
|
|
Box createBox = Box.createVerticalBox();
|
|
|
|
|
|
|
|
|
|
JLabel createTitre = new JLabel("CREATION DE COMPTE");
|
|
|
|
|
createTitre.setForeground(Color.WHITE);
|
|
|
|
|
createTitre.setFont(new Font("Arial", Font.BOLD, 24));
|
|
|
|
|
createTitre.setAlignmentX(Component.CENTER_ALIGNMENT);
|
|
|
|
|
|
|
|
|
|
JTextField newUserField = new JTextField(15);
|
|
|
|
|
newUserField.setMaximumSize(new Dimension(250, 30));
|
|
|
|
|
|
|
|
|
|
JPasswordField newPassField = new JPasswordField(15);
|
|
|
|
|
newPassField.setMaximumSize(new Dimension(250, 30));
|
|
|
|
|
|
|
|
|
|
JButton createAccountBtn = new JButton("Créer");
|
|
|
|
|
createAccountBtn.setBackground(new Color(70, 130, 180));
|
|
|
|
|
createAccountBtn.setForeground(Color.WHITE);
|
|
|
|
|
createAccountBtn.setFocusPainted(false);
|
|
|
|
|
createAccountBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
|
|
|
|
|
|
|
|
|
|
createBox.add(createTitre);
|
|
|
|
|
createBox.add(Box.createVerticalStrut(30));
|
|
|
|
|
createBox.add(new JLabel("<html><font color='white'>Nouvel utilisateur :</font></html>"));
|
|
|
|
|
createBox.add(newUserField);
|
|
|
|
|
createBox.add(Box.createVerticalStrut(15));
|
|
|
|
|
createBox.add(new JLabel("<html><font color='white'>Mot de passe :</font></html>"));
|
|
|
|
|
createBox.add(newPassField);
|
|
|
|
|
createBox.add(Box.createVerticalStrut(30));
|
|
|
|
|
createBox.add(createAccountBtn);
|
|
|
|
|
|
|
|
|
|
createPanel.add(createBox);
|
|
|
|
|
|
|
|
|
|
createAccountBtn.addActionListener(e -> {
|
|
|
|
|
String u = newUserField.getText().trim();
|
|
|
|
|
String p = new String(newPassField.getPassword());
|
|
|
|
|
if (u.isEmpty() || p.isEmpty()) {
|
|
|
|
|
JOptionPane.showMessageDialog(createFrame, "Utilisateur et mot de passe requis");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean ok = GestionBDD.creerUtilisateur(u, p);
|
|
|
|
|
if (ok) {
|
|
|
|
|
JOptionPane.showMessageDialog(createFrame, "Compte cree avec succes.");
|
|
|
|
|
createFrame.dispose();
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(createFrame, "Échec : le nom d'utilisateur existe deja ou erreur.");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
createFrame.setVisible(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
}
|