import java.awt.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class CadreDeConnexion extends JFrame {
//--------------------------------------------------------------------------
// 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;
}
public CadreDeConnexion() {
// 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");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Centre la fenêtre
// 3. Style du panneau principal (Gris foncé)
// JPanel panel = new JPanel(new GridBagLayout());
JPanel panel = new ZoneDessin(panelBackground);
panel.setLayout(new GridBagLayout());
// Conteneur pour les éléments (pour les empiler verticalement)
Box box = Box.createVerticalBox();
// 4. Création des composants
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);
// 5. Ajout des composants avec des espaces (Struts)
box.add(titre);
box.add(Box.createVerticalStrut(30)); // Espace
box.add(new JLabel("Utilisateur :"));
box.add(userField);
box.add(Box.createVerticalStrut(15));
box.add(new JLabel("Mot de passe :"));
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
setContentPane(panel); // Branche enfin le panneau personnalisé à la fenêtre
// 6. Logique du bouton
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);
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);
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("Nouvel utilisateur :"));
createBox.add(newUserField);
createBox.add(Box.createVerticalStrut(15));
createBox.add(new JLabel("Mot de passe :"));
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);
}
}