Package hexenschach.gui

Source Code of hexenschach.gui.NewGameDialog

package hexenschach.gui;
import hexenschach.gameplay.Gameplay;
import hexenschach.gui.resources.Images;
import hexenschach.player.Human;
import hexenschach.player.Player;
import hexenschach.player.Computer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;

public class NewGameDialog extends HexenschachDialog{
  private static final long serialVersionUID = -7648384823488879966L;
  private JComboBox playerChoice1;
  private JComboBox playerChoice2;
  private JComboBox playerChoice3;
 
  NewGameDialog(GUI parent) {
    super(parent, 400, 300);
  }

  @Override
  public void initLayout() {
    setLayout(new BorderLayout());
  }
 
  @Override
  public void initComponents() {
    // Hintergrundbild
    Background background = new Background(Images.getNewGameDialogBackground(), xSize, ySize);
    // Labels
    JLabel prompt = new JLabel(Dictionary.NewGameDialogPrompt);
    prompt.setForeground(new Color(236, 182, 74));
    prompt.setBounds(20, 10, 200, 40);
    background.add(prompt);
    JLabel  dropdownLabel1= new JLabel(Dictionary.NewGameDialogText1);
    dropdownLabel1.setForeground(new Color(236, 182, 74));
    dropdownLabel1.setBounds(20, 70, 150, 20);
    background.add(dropdownLabel1);
    JLabel dropdownLabel2 = new JLabel(Dictionary.NewGameDialogText2);
    dropdownLabel2.setForeground(new Color(236, 182, 74));
    dropdownLabel2.setBounds(20, 120, 150, 20);
    background.add(dropdownLabel2);
    JLabel dropdownLabel3 = new JLabel(Dictionary.NewGameDialogText3);
    dropdownLabel3.setForeground(new Color(236, 182, 74));
    dropdownLabel3.setBounds(20, 170,150, 20);
    background.add(dropdownLabel3);
    // Dropdown-Boxes
    playerChoice1 = new JComboBox();
    playerChoice1.addItem(Dictionary.NewGameDialogChoice1);
    playerChoice1.addItem(Dictionary.NewGameDialogChoice2);
    playerChoice1.addItem(Dictionary.NewGameDialogChoice3);
    playerChoice1.addItem(Dictionary.NewGameDialogChoice4);
    playerChoice1.setBounds(180, 70, 150, 20);
    playerChoice1.setBackground(new Color(170, 71, 12));
    playerChoice1.setForeground(new Color(236, 182, 74));
    playerChoice1.setBorder(null);
    playerChoice1.setEditable(false);
    background.add(playerChoice1);
    playerChoice2 = new JComboBox();
    playerChoice2.addItem(Dictionary.NewGameDialogChoice1);
    playerChoice2.addItem(Dictionary.NewGameDialogChoice2);
    playerChoice2.addItem(Dictionary.NewGameDialogChoice3);
    playerChoice2.addItem(Dictionary.NewGameDialogChoice4);
    playerChoice2.setBounds(180, 120, 150, 20);
    playerChoice2.setBackground(new Color(170, 71, 12));
    playerChoice2.setForeground(new Color(236, 182, 74));
    playerChoice2.setBorder(null);
    playerChoice2.setEditable(false);
    background.add(playerChoice2);
    playerChoice3 = new JComboBox();
    playerChoice3.addItem(Dictionary.NewGameDialogChoice1);
    playerChoice3.addItem(Dictionary.NewGameDialogChoice2);
    playerChoice3.addItem(Dictionary.NewGameDialogChoice3);
    playerChoice3.addItem(Dictionary.NewGameDialogChoice4);
    playerChoice3.setBounds(180, 170, 150, 20);
    playerChoice3.setBackground(new Color(170, 71, 12));
    playerChoice3.setForeground(new Color(236, 182, 74));
    playerChoice3.setBorder(null);
    playerChoice3.setEditable(false);
    background.add(playerChoice3);
    //Buttons
    DialogButton okButton = new DialogButton("", Images.getDialogOkButtonDefaultImage(), Images.getDialogOkButtonRolloverImage(), Images.getDialogOkButtonPressedImage());
    okButton.setBounds(140, 250, DialogButton.xSize, DialogButton.ySize);
    okButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        okButtonClicked();
      }
    });
    background.add(okButton);
    DialogButton cancelButton = new DialogButton("", Images.getDialogCancelButtonDefaultImage(), Images.getDialogCancelButtonRolloverImage(), Images.getDialogCancelButtonPressedImage());
    cancelButton.setBounds(260, 250, DialogButton.xSize, DialogButton.ySize);
    cancelButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        cancelButtonClicked();
      }
    });
    background.add(cancelButton);
   
    add(background);
  }
  /**
   * Wird aufgerufen, wenn der Benutzer den Dialog mit OK
   * bestätigt.
   */
  private void okButtonClicked() {
    // Falls schon ein Spiel läuft, fragen, ob wirklich ein neues gestartet werden soll.
    if(parent.getGameplay() != null) {
      QuestionDialog questionDialog = new QuestionDialog(parent, Dictionary.NewGameDialogQuestion) {
        private static final long serialVersionUID = -1842089290093568092L;
        @Override
        public void cancelButtonClicked() {
          //Question-Dialog schließen
          setVisible(false);
          // Neues-Spiel-Dialog auch schließen
          // geht hier nicht über setVisible(), da man zwar Zugriff auf die Methoden
          // der äußeren Klasse hat, aber die Innere Klasse die gleiche Methode besitzt
          // und somit nicht unterscheidbar ist.
          close();
        }
        @Override
        public void noButtonClicked() {
          // Den QuestionDialog schließen
          setVisible(false);
        }
        @Override
        public void yesButtonClicked() {
          // QuestionDialog schließen
          setVisible(false);
          // Neues-Spiel Dialog schließen
          close();
          // Neues Spiel starten
          startNewGame();
        }
      };
      questionDialog.setVisible(true);
    }
    else {
      startNewGame();
      close();
    }
  }
  /**
   * Wird aufgerufen, wenn der Benutzer den Dialog mit OK
   * bestätigt.
   */
  private void cancelButtonClicked() {
    close();
  }
  /**
   * Erzeugt ein Neues Gameplay, nach den Angaben der Dropdown-Auswahl
   * und weißt es dem gui als aktuelles Gameplay zu.
   */
  private void startNewGame() {
    Player player1;
    Player player2;
    Player player3;
    // Ersten Spieler erstellen
    if(playerChoice1.getSelectedItem().equals(Dictionary.NewGameDialogChoice2))
      player1 = new Computer(true, "white", 1);
    else if(playerChoice1.getSelectedItem().equals(Dictionary.NewGameDialogChoice3))
      player1 = new Computer(true, "white", 2);
    else if(playerChoice1.getSelectedItem().equals(Dictionary.NewGameDialogChoice4))
      player1 = new Computer(true, "white", 3);
    else
      player1 = new Human(false, "white");
    // Zweiten Spieler erstellen
    if(playerChoice2.getSelectedItem().equals(Dictionary.NewGameDialogChoice2))
      player2 = new Computer(true, "brown", 1);
    else if(playerChoice2.getSelectedItem().equals(Dictionary.NewGameDialogChoice3))
      player2 = new Computer(true, "brown", 2);
    else if(playerChoice2.getSelectedItem().equals(Dictionary.NewGameDialogChoice4))
      player2 = new Computer(true, "brown", 3);
    else
      player2 = new Human(false, "brown");
    // Dritten Spieler erstellen
    if(playerChoice3.getSelectedItem().equals(Dictionary.NewGameDialogChoice2))
      player3 = new Computer(true, "black", 1);
    else if(playerChoice3.getSelectedItem().equals(Dictionary.NewGameDialogChoice3))
      player3 = new Computer(true, "black", 2);
    else if(playerChoice3.getSelectedItem().equals(Dictionary.NewGameDialogChoice4))
      player3 = new Computer(true, "black", 3);
    else
      player3 = new Human(false, "black");
    // Gameplay erzeugen
    Gameplay gameplay = new Gameplay(player1, player2, player3, parent);
    parent.setGameplay(gameplay);
  }
  /**
   * Schlie�t den Dialog.
   * Kann vorallem statt setVisible() von inneren Klassen
   * benutzt werden, die selbst eine setVisible-Methode besitzen
   * und somit nicht auf die der äußeren Klasse zugreifen können.
   */
  private void close() {
    setVisible(false);
  }
}
TOP

Related Classes of hexenschach.gui.NewGameDialog

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.