SAE21_2021/FrameMenu.java

129 lines
4.5 KiB
Java
Raw Normal View History

2022-04-25 17:10:55 +02:00
import javax.swing.*;
2022-05-21 17:30:12 +02:00
import java.awt.event.*;
2022-05-24 23:41:34 +02:00
import java.io.File;
2022-04-25 17:10:55 +02:00
import java.awt.*;
// Cette classe à pour but d'afficher un menu et de réagir aux directives de l'utilisateur (lancer le jeu, le quitter...)
2022-05-21 17:30:12 +02:00
public class FrameMenu extends JFrame{
2022-05-20 13:39:53 +02:00
private int lignes;
private int colonnes;
private int mines;
2022-05-21 17:03:31 +02:00
private JSlider sliderLignes;
private JSlider sliderColonnes;
private JSlider sliderMines;
2022-05-23 12:56:40 +02:00
private JLabel showLines;
private JLabel showColumns;
private JLabel showMines;
2022-05-25 08:47:55 +02:00
private JButton charger;
2022-05-23 12:56:40 +02:00
2022-04-25 17:10:55 +02:00
public FrameMenu() {
2022-05-23 23:02:19 +02:00
super();
2022-04-25 17:10:55 +02:00
// On récupère les dimensions de l'écran pour adapter la taille de notre fenêtre
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// On crée ensuite notre fenêtre
2022-05-21 17:30:12 +02:00
this.setTitle("Démineur - Menu");
Dimension frameSize = new Dimension (screenSize.width *2/5,screenSize.height * 2/3);
2022-05-23 13:45:34 +02:00
this.setSize(frameSize);
this.setLocation(screenSize.width * 3/10, screenSize.height * 1/6);
2022-05-21 17:30:12 +02:00
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
2022-05-23 23:02:19 +02:00
this.getContentPane().setBackground( new Color(0, 236, 96));
2022-05-18 14:42:02 +02:00
2022-05-23 23:02:19 +02:00
// Ajout du titre
JLabel titre = new JLabel("Démineur");
titre.setFont(new Font("Rockwell", Font.PLAIN, 45));
titre.setBounds((frameSize.width/3), frameSize.height/15,frameSize.width/2,frameSize.height*2/15);
this.add(titre);
2022-05-18 14:42:02 +02:00
2022-05-23 23:02:19 +02:00
// On créer les sliders et les JLabel qui afficheront leur valeur
2022-05-21 23:01:08 +02:00
this.sliderLignes = new JSlider(4,30,20);
this.sliderColonnes = new JSlider(4,30,25);
this.sliderMines = new JSlider(1,900, 50);
2022-05-19 08:12:15 +02:00
2022-05-21 17:03:31 +02:00
sliderLignes.addChangeListener(new SettingsListener(0,this));
sliderColonnes.addChangeListener(new SettingsListener(1,this));
sliderMines.addChangeListener(new SettingsListener(2,this));
2022-05-19 08:12:15 +02:00
2022-05-20 13:39:53 +02:00
this.lignes=sliderLignes.getValue();
this.colonnes=sliderColonnes.getValue();
this.mines=sliderMines.getValue();
2022-05-21 17:03:31 +02:00
2022-05-23 23:02:19 +02:00
sliderLignes.setBounds(frameSize.width/3, frameSize.height*5/15, frameSize.width/3, frameSize.height/20);
sliderColonnes.setBounds(frameSize.width/3, frameSize.height*7/15, frameSize.width/3, frameSize.height/20);
sliderMines.setBounds(frameSize.width/3, frameSize.height*9/15, frameSize.width/3, frameSize.height/20);
2022-05-23 13:45:34 +02:00
2022-05-21 17:30:12 +02:00
this.add(sliderLignes);
this.add(sliderColonnes);
this.add(sliderMines);
2022-05-23 12:56:40 +02:00
this.showLines= new JLabel("Lignes : 20");
this.showColumns= new JLabel("Colonnes : 25");
this.showMines= new JLabel("Mines : 50");
2022-05-23 23:02:19 +02:00
this.showLines.setBounds(frameSize.width/3, frameSize.height*4/15, frameSize.width/3, frameSize.height/20);
this.showColumns.setBounds(frameSize.width/3, frameSize.height*6/15, frameSize.width/3, frameSize.height/20);
this.showMines.setBounds(frameSize.width/3, frameSize.height*8/15, frameSize.width/3, frameSize.height/20);
2022-05-23 13:45:34 +02:00
2022-05-23 12:56:40 +02:00
this.add(showLines);
this.add(showColumns);
this.add(showMines);
2022-05-21 17:03:31 +02:00
2022-05-23 19:32:09 +02:00
JButton newGame = new JButton("Jouer");
2022-05-21 17:03:31 +02:00
newGame.addActionListener(new NewGameListener(this));
2022-05-24 23:41:34 +02:00
newGame.setBounds(frameSize.width*7/10, frameSize.height*12/15, frameSize.width/5, frameSize.height/15);;
2022-05-21 17:30:12 +02:00
this.add(newGame);
JButton exit = new JButton("Quitter");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
exit.setBounds(frameSize.width/10, frameSize.height*12/15, frameSize.width/5, frameSize.height/15);
2022-05-21 17:30:12 +02:00
this.add(exit);
2022-05-24 23:41:34 +02:00
File save = new File("Save.txt");
if(save.exists()){
2022-05-25 08:47:55 +02:00
charger = new JButton("Charger");
2022-05-24 23:41:34 +02:00
charger.addActionListener(new Chargement(this));
charger.setBounds(frameSize.width*4/10, frameSize.height*12/15, frameSize.width/5, frameSize.height/15);
this.add(charger);
}
2022-05-21 17:03:31 +02:00
this.setResizable(false);
2022-05-21 17:30:12 +02:00
this.setVisible(true);
2022-04-25 17:10:55 +02:00
}
2022-05-21 17:03:31 +02:00
2022-05-23 12:56:40 +02:00
// Méthode pour récupérer les valeurs des paramètres et les afficher à l'écran
2022-05-21 17:03:31 +02:00
public void setSettings(int settingToSet, int settingValue){
if (settingToSet==0){
this.lignes=settingValue;
2022-05-23 12:56:40 +02:00
showLines.setText("Lignes : "+settingValue);
2022-05-21 17:03:31 +02:00
} else if (settingToSet==1){
this.colonnes=settingValue;
2022-05-23 12:56:40 +02:00
showColumns.setText("Colonnes : "+settingValue);
2022-05-21 17:03:31 +02:00
} else if (settingToSet==2){
this.mines=settingValue;
2022-05-23 12:56:40 +02:00
showMines.setText("Mines : "+settingValue);
2022-05-21 17:03:31 +02:00
}
this.sliderMines.setMaximum(this.colonnes*this.lignes);
}
// Methode pour connaître les paramètres de la partie
public int[] getSettings(){
int[] settings = new int[3];
settings[0]=this.lignes;
settings[1]=this.colonnes;
settings[2]=this.mines;
return settings;
}
2022-05-25 08:47:55 +02:00
// Methode pour supprimer le bouton charger après un chargement
public void deleteCharger(){
this.remove(this.charger);
}
2022-04-25 17:10:55 +02:00
}