package IHM;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import Character.Races.Race;
import Character.Races.RInstances.Elf;
import Character.Races.RInstances.Human;
import Character.Races.RInstances.Orc;
import Character.Races.RInstances.Undead;
public class StartFrame extends JDialog implements ActionListener {
private MainFrame mf;
private JRadioButton team1J1, team2J1, team3J1, team4J1, team1J2, team2J2,
team3J2, team4J2;
public StartFrame(MainFrame mainFrame) {
mf = mainFrame;
setPreferredSize(new Dimension(600, 400));
setBackground(Color.lightGray);
setSize(getPreferredSize());
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// creation panel joueur1
JPanel panJ1 = new JPanel(new GridLayout(5, 1));
JLabel etiquetteJ1 = new JLabel("Joueur 1");
Font f = new Font(null, Font.BOLD, 20);
etiquetteJ1.setFont(f);
etiquetteJ1.setHorizontalAlignment(JLabel.CENTER);
etiquetteJ1.setVerticalAlignment(JLabel.CENTER);
panJ1.add(etiquetteJ1);
ButtonGroup groupJ1 = new ButtonGroup();
team1J1 = new JRadioButton("Elfes");
team2J1 = new JRadioButton("Humains");
team3J1 = new JRadioButton("Morts-vivants");
team4J1 = new JRadioButton("Orques");
team1J1.setHorizontalAlignment(JRadioButton.CENTER);
team2J1.setHorizontalAlignment(JRadioButton.CENTER);
team3J1.setHorizontalAlignment(JRadioButton.CENTER);
team4J1.setHorizontalAlignment(JRadioButton.CENTER);
groupJ1.add(team1J1);
groupJ1.add(team2J1);
groupJ1.add(team3J1);
groupJ1.add(team4J1);
panJ1.add(team1J1);
panJ1.add(team2J1);
panJ1.add(team3J1);
panJ1.add(team4J1);
panJ1.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(10,
10, 10, 10), new EtchedBorder()));
team1J1.setSelected(true);
// creation panel joueur1
JPanel panJ2 = new JPanel(new GridLayout(5, 1));
JLabel etiquetteJ2 = new JLabel("Joueur 2");
etiquetteJ2.setFont(f);
etiquetteJ2.setHorizontalAlignment(JLabel.CENTER);
etiquetteJ2.setVerticalAlignment(JLabel.CENTER);
panJ2.add(etiquetteJ2);
ButtonGroup groupJ2 = new ButtonGroup();
team1J2 = new JRadioButton("Elfes");
team2J2 = new JRadioButton("Humains");
team3J2 = new JRadioButton("Morts-vivants");
team4J2 = new JRadioButton("Orques");
team1J2.setHorizontalAlignment(JRadioButton.CENTER);
team2J2.setHorizontalAlignment(JRadioButton.CENTER);
team3J2.setHorizontalAlignment(JRadioButton.CENTER);
team4J2.setHorizontalAlignment(JRadioButton.CENTER);
groupJ2.add(team1J2);
groupJ2.add(team2J2);
groupJ2.add(team3J2);
groupJ2.add(team4J2);
panJ2.add(team1J2);
panJ2.add(team2J2);
panJ2.add(team3J2);
panJ2.add(team4J2);
panJ2.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(10,
10, 10, 10), new EtchedBorder()));
team1J2.setSelected(true);
// creation panel boutons
JPanel panButton = new JPanel();
JButton annul = new JButton("Annuler");
annul.addActionListener(this);
JButton valid = new JButton("Jouer");
valid.addActionListener(this);
annul.setPreferredSize(new Dimension(100, 30));
valid.setPreferredSize(new Dimension(100, 30));
panButton.add(valid);
panButton.add(annul);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.1;
c.weighty = 0.1;
c.gridheight = 2;
c.insets = new Insets(2, 2, 0, 0);
add(panJ1, c);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.1;
c.weighty = 0.1;
c.gridheight = 2;
c.insets = new Insets(2, 2, 0, 2);
add(panJ2, c);
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 2;
c.weightx = 0.1;
c.weighty = 0.1;
c.gridwidth = 2;
c.insets = new Insets(2, 2, 2, 2);
add(panButton, c);
setLocationRelativeTo(null);
setModal(true);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// Si on clique sur Annuler alors on tue l'application
if (e.getActionCommand().equals("Annuler"))
dispose();
else {
// Sinon, on a clique sur Jouer et on cree une nouvelle partie
int teamJ1 = 0;
if (team2J1.isSelected())
teamJ1 = 1;
else if (team3J1.isSelected())
teamJ1 = 2;
else if (team4J1.isSelected())
teamJ1 = 3;
int teamJ2 = 0;
if (team2J2.isSelected())
teamJ2 = 1;
else if (team3J2.isSelected())
teamJ2 = 2;
else if (team4J2.isSelected())
teamJ2 = 3;
// Creation de la partie en fonction des choix des equipes
Race race1 = null, race2 = null;
switch (teamJ1) {
case 0:
race1 = new Elf();
break;
case 1:
race1 = new Human();
break;
case 2:
race1 = new Undead();
break;
case 3:
race1 = new Orc();
break;
default:
System.err.println("Error choosing race from Player 1");
System.exit(1);
}
switch (teamJ2) {
case 0:
race2 = new Elf();
break;
case 1:
race2 = new Human();
break;
case 2:
race2 = new Undead();
break;
case 3:
race2 = new Orc();
break;
default:
System.err.println("Error choosing race from Player 2");
System.exit(1);
}
if (race1 == null || race2 == null)
System.err.println("Error choosing races !!!!");
mf.initEngine(race1, race2);
dispose();
}
}
}