Package IHM

Source Code of IHM.MainFrame

package IHM;

import java.awt.BorderLayout;
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.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

import Board.Board;
import Character.Races.Race;
import Engine.Engine;

public class MainFrame extends JFrame implements ActionListener {

  /*
   * Attributes
   */
  private Engine engine;
  private JPanel damier, panJ1, panJ2;
  private JLabel tokenJ1, tokenJ2;
  private JLabel type, str, hea, ran, arm, sta, cri, acc, her;
  private JTextArea land;
  private JTextPane infoGame;

  public MainFrame() {
    setPreferredSize(new Dimension(800, 600));
    setBackground(Color.lightGray);
    setSize(getPreferredSize());
    // setLayout(new GridLayout(3, 3));
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // ajoute de la barre de menu
    JMenuBar menubar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    JMenuItem it1 = new JMenuItem("Start");
    it1.setActionCommand("Start");
    JMenuItem it2 = new JMenuItem("Quitter");
    it2.setActionCommand("Quitter");
    // ajout des items au menu
    menu.add(it1);
    menu.add(it2);
    // ajout du menu a la barre de menu
    menubar.add(menu);
    // Les �v�nements sur les items seront �cout�s par la frame
    it1.addActionListener(this);
    it2.addActionListener(this);
    // on affecte la barre de menu � la frame
    setJMenuBar(menubar);

    // creation etiquette joueurs
    panJ1 = new JPanel(new GridLayout(2, 1));
    Font f = new Font(null, Font.BOLD, 17);
    JLabel etiqJ1 = new JLabel("Joueur 1");
    tokenJ1 = new JLabel("0 tokens");
    panJ2 = new JPanel(new GridLayout(2, 1));
    JLabel etiqJ2 = new JLabel("Joueur 2");
    tokenJ2 = new JLabel("0 tokens");
    etiqJ1.setHorizontalAlignment(JLabel.CENTER);
    etiqJ1.setFont(f);
    etiqJ2.setHorizontalAlignment(JLabel.CENTER);
    etiqJ2.setFont(f);
    tokenJ1.setHorizontalAlignment(JLabel.CENTER);
    tokenJ2.setHorizontalAlignment(JLabel.CENTER);
    panJ1.add(etiqJ1);
    panJ1.add(tokenJ1);
    panJ2.add(etiqJ2);
    panJ2.add(tokenJ2);
    panJ1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    panJ2.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    // creation du plateau et de son affichage
    damier = new JPanel(new GridLayout(Board.LINES, Board.COLUMNS));
    Board initBoard = new Board();
    for (int i = 0; i < Board.LINES; i++) {
      for (int j = 0; j < Board.COLUMNS; j++) {
        damier.add(initBoard.getCase(i, j));
        initBoard.getCase(i, j).setPan(damier);
        initBoard.getCase(i, j).setFrame(this);
      }
    }
    damier.setBackground(Color.BLACK);
    damier.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
    // creation du panel des affichages de informations du d�roulement du
    // jeu
    // Ajout d'un JLabel indiquant des informations sur la partie
    infoGame = new JTextPane();
    infoGame.setEditable(false);
    infoGame.setBackground(null);
    JScrollPane scrollInfoGame = new JScrollPane(infoGame);
    scrollInfoGame.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    scrollInfoGame.setPreferredSize(new Dimension(0, 70));

    // creation du panel des affichages des caracterisqtiques de la case
    // et/ou du perso
    JPanel panelCaracteristique = new JPanel(new BorderLayout());
    JLabel titreCaract = new JLabel("Caracteristiques");
    titreCaract.setFont(f);
    titreCaract.setHorizontalAlignment(JLabel.CENTER);
    // panel info charac
    Font f2 = new Font(null, Font.BOLD, 12);
    Font f3 = new Font(null, 0, 11);
    JPanel infPerso = new JPanel(new GridLayout(9, 1));
    infPerso.setPreferredSize(new Dimension(100, 300));
    type = new JLabel("Personnage : ");
    type.setHorizontalAlignment(JLabel.CENTER);
    type.setFont(f2);
    str = new JLabel("Strength : ");
    str.setFont(f3);
    hea = new JLabel("Health : ");
    hea.setFont(f3);
    ran = new JLabel("Range : ");
    ran.setFont(f3);
    arm = new JLabel("Armor : ");
    arm.setFont(f3);
    sta = new JLabel("Stamina : ");
    sta.setFont(f3);
    cri = new JLabel("Critical Chance : ");
    cri.setFont(f3);
    acc = new JLabel("Accuracy : ");
    acc.setFont(f3);
    her = new JLabel("Heritage : ");
    her.setFont(f3);
    infPerso.add(type);
    infPerso.add(str);
    infPerso.add(hea);
    infPerso.add(ran);
    infPerso.add(arm);
    infPerso.add(sta);
    infPerso.add(cri);
    infPerso.add(acc);
    infPerso.add(her);
    infPerso.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(
        10, 10, 10, 10), new EtchedBorder()));
    panelCaracteristique.add(titreCaract, BorderLayout.NORTH);
    panelCaracteristique.add(infPerso, BorderLayout.CENTER);
    panelCaracteristique.setBorder(BorderFactory
        .createLineBorder(Color.BLACK));
    // panel info terrain
    JPanel infLand = new JPanel(new GridLayout(2, 1));
    JLabel titreLand = new JLabel("Terrain :");
    titreLand.setHorizontalAlignment(JLabel.CENTER);
    titreLand.setFont(f2);
    infLand.add(titreLand);
    land = new JTextArea();
    land.setPreferredSize(new Dimension(0, 30));
    land.setEditable(false);
    land.setLineWrap(true);
    land.setWrapStyleWord(false);
    land.setBackground(null);
    land.setFont(f3);
    infLand.add(land);
    infLand.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(
        10, 10, 10, 10), new EtchedBorder()));
    panelCaracteristique.add(infLand, BorderLayout.SOUTH);

    // creation du panel des boutons du jeu
    JPanel panelButtons = new JPanel(new GridBagLayout());
    JButton bpasse = new JButton("Passer mon tour");
    bpasse.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        if (engine != null) {
          engine.changeTurn();
          setTokens(engine.getJ1().getToken(), engine.getJ2()
              .getToken());
          if (engine.myTurn(engine.getJ1()))
            setTurn(1);
          else
            setTurn(2);
        }
      }
    });
    panelButtons.add(bpasse);

    // ajout et placement dans la frame
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.1;
    c.weighty = 0.1;
    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;
    add(panJ2, c);
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.gridy = 1;
    c.weightx = 0.5;
    c.weighty = 1;
    c.gridwidth = 2;
    add(damier, c);
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 2;
    c.gridy = 1;
    c.weightx = 0.1;
    c.weighty = 0.1;
    c.gridwidth = 0;
    c.insets = new Insets(2, 2, 0, 2);
    add(panelCaracteristique, c);
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.gridy = 2;
    c.weightx = 0.1;
    c.weighty = 0.3;
    c.gridwidth = 2;
    c.insets = new Insets(2, 2, 2, 0);
    add(scrollInfoGame, c);
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 2;
    c.gridy = 2;
    c.weightx = 0;
    c.weighty = 0;
    c.gridwidth = 0;
    c.insets = new Insets(2, 2, 2, 2);
    add(panelButtons, c);

    setLocationRelativeTo(null);
    setVisible(true);
  }

  public void initEngine(Race race1, Race race2) {
    remove(damier);
    engine = new Engine(race1, race2, this);
    damier.removeAll();
    Board initBoard = engine.getBoard();
    for (int i = 0; i < Board.LINES; i++) {
      for (int j = 0; j < Board.COLUMNS; j++) {
        damier.add(initBoard.getCase(i, j));
        initBoard.getCase(i, j).setPan(damier);
        initBoard.getCase(i, j).setFrame(this);
        initBoard.getCase(i, j).setEngine(engine);
      }
    }
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.gridy = 1;
    c.weightx = 0.5;
    c.weighty = 1;
    c.gridwidth = 2;
    add(damier, c);
    setTokens(engine.getJ1().getToken(), engine.getJ2().getToken());
    if (engine.myTurn(engine.getJ1()))
      setTurn(1);
    else
      setTurn(2);
    validate();
  }

  public void actionPerformed(ActionEvent e) {
    // Si on clique sur Quitter alors on tue l'application
    if (e.getActionCommand().equals("Quitter"))
      System.exit(0);
    else
      // Sinon, on a cliqu� sur "Start" et on cr�e une nouvelle partie
      new StartFrame(this);
  }

  public void setType(String s) {
    type.setText("Personnage : " + s);
  }

  public void setStr(String s) {
    str.setText(s);
  }

  public void setHea(String s) {
    hea.setText(s);
  }

  public void setRan(String s) {
    ran.setText(s);
  }

  public void setArm(String s) {
    if (s.equals("Aucun"))
      arm.setHorizontalAlignment(JLabel.CENTER);
    else
      arm.setHorizontalAlignment(JLabel.LEFT);
    arm.setText(s);
  }

  public void setSta(String s) {
    sta.setText(s);
  }

  public void setCri(String s) {
    cri.setText(s);
  }

  public void setAcc(String s) {
    acc.setText(s);
  }

  public void setHer(String s) {
    her.setText(s);
  }

  public void setTokens(int j1, int j2) {
    tokenJ1.setText(j1 + " tokens");
    tokenJ2.setText(j2 + " tokens");
  }

  public void setLand(String s) {
    land.setText(s);
  }

  public void reportGameInfo(String s) {
    String tmp = infoGame.getText();
    infoGame.setText(tmp + s + "\n");
  }

  public void setTurn(int j) {
    if (j == 1) {
      panJ1.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
      panJ2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    } else {
      panJ2.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
      panJ1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }
  }

  public static void main(String[] args) {
    new MainFrame();
  }
}
TOP

Related Classes of IHM.MainFrame

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.