292 lines
8.2 KiB
Java
292 lines
8.2 KiB
Java
package reversi.view;
|
|
|
|
import java.awt.BorderLayout;
|
|
import java.awt.Color;
|
|
import java.awt.Container;
|
|
import java.awt.FlowLayout;
|
|
import java.awt.Font;
|
|
import java.awt.GridLayout;
|
|
import java.awt.Toolkit;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.KeyEvent;
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import javax.swing.JButton;
|
|
import javax.swing.JComboBox;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.SwingConstants;
|
|
import javax.swing.border.CompoundBorder;
|
|
import javax.swing.border.EmptyBorder;
|
|
|
|
import reversi.model.Board;
|
|
import reversi.model.Field;
|
|
import reversi.model.Reversi;
|
|
|
|
/**
|
|
* Create and update the gui.
|
|
*/
|
|
public class ReversiView extends JFrame {
|
|
|
|
private static final long serialVersionUID = 5382834862595792805L;
|
|
private boolean movePossible;
|
|
private ReversiPlayBoard playBoard;
|
|
private JPanel horizTableLabel;
|
|
private JPanel verticTableLabel;
|
|
private JLabel humanTiles;
|
|
private JLabel botTiles;
|
|
private JComboBox<Integer> switchLevel =
|
|
new JComboBox<Integer>(new Integer[] {1, 2, 3, 4, 5});
|
|
private JButton newGame = new JButton("New");
|
|
private JButton switchGame = new JButton("Switch");
|
|
private JButton undoMove = new JButton("Undo");
|
|
private JButton quitGame = new JButton("Quit");
|
|
|
|
/**
|
|
* Create the gui for the reversi game.
|
|
*/
|
|
public ReversiView() {
|
|
super("Reversi");
|
|
initForms();
|
|
movePossible = true;
|
|
}
|
|
|
|
/**
|
|
* Create all buttons and the playfield.
|
|
*/
|
|
private void initForms() {
|
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
this.setSize(410, 450);
|
|
this.setLayout(new BorderLayout());
|
|
|
|
playBoard = new ReversiPlayBoard();
|
|
verticTableLabel = verticTableLabel();
|
|
horizTableLabel = horizTableLabel();
|
|
humanTiles = tilesSet();
|
|
humanTiles.setForeground(Color.BLUE);
|
|
botTiles = tilesSet();
|
|
botTiles.setForeground(Color.RED);
|
|
|
|
Container menuBar = new Container();
|
|
menuBar.setLayout(new FlowLayout());
|
|
menuBar.add(humanTiles);
|
|
menuBar.add(switchLevel);
|
|
menuBar.add(newGame);
|
|
menuBar.add(switchGame);
|
|
switchLevel.setSelectedIndex(2);
|
|
menuBar.add(undoMove);
|
|
menuBar.add(quitGame);
|
|
menuBar.add(botTiles);
|
|
|
|
|
|
this.getContentPane().add(playBoard, BorderLayout.CENTER);
|
|
this.getContentPane().add(menuBar, BorderLayout.SOUTH);
|
|
this.getContentPane().add(verticTableLabel, BorderLayout.WEST);
|
|
this.getContentPane().add(horizTableLabel, BorderLayout.NORTH);
|
|
}
|
|
|
|
/**
|
|
* Create the vertical label for the playfield.
|
|
*
|
|
* @return JPanel The label.
|
|
*/
|
|
private JPanel verticTableLabel() {
|
|
JPanel panel = new JPanel();
|
|
panel.setLayout(new GridLayout(Board.SIZE, 1));
|
|
panel.setBorder(new CompoundBorder(
|
|
null, new EmptyBorder(0, 5, 10, 5)));
|
|
|
|
for (int i = 1; i <= Board.SIZE; i++) {
|
|
JLabel tmp = new JLabel(String.valueOf(i));
|
|
panel.add(tmp);
|
|
}
|
|
return panel;
|
|
}
|
|
|
|
/**
|
|
* Create the horizontal label for the playfield.
|
|
*
|
|
* @return JPanel The label.
|
|
*/
|
|
private JPanel horizTableLabel() {
|
|
JPanel panel = new JPanel();
|
|
panel.setLayout(new GridLayout(1, Board.SIZE));
|
|
panel.setBorder(new CompoundBorder(
|
|
null, new EmptyBorder(0, 20, 0, 12)));
|
|
|
|
for (int i = 1; i <= Board.SIZE; i++) {
|
|
JLabel tmp = new JLabel(String.valueOf(i));
|
|
tmp.setHorizontalAlignment(SwingConstants.CENTER);
|
|
panel.add(tmp);
|
|
}
|
|
return panel;
|
|
}
|
|
|
|
/**
|
|
* Creates both labels for the number of tiles that are set.
|
|
*
|
|
* @return JLabel The label.
|
|
*/
|
|
private JLabel tilesSet() {
|
|
JLabel tiles = new JLabel();
|
|
tiles.setBorder(new CompoundBorder(null, new EmptyBorder(0, 3, 0, 3)));
|
|
tiles.setFont(new Font(Font.DIALOG, Font.BOLD, 20));
|
|
return tiles;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return Level
|
|
*/
|
|
public final int getLevel() {
|
|
return (Integer) this.switchLevel.getSelectedItem();
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return <code>true</code> if human is allowed to perform a move.<br />
|
|
* <code>false</code> e.g. if a machine move is active.
|
|
*/
|
|
public boolean getMovePossible() {
|
|
return movePossible;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param movePossible Set if human move is possible.
|
|
*/
|
|
public final void setMovePossible(final boolean movePossible) {
|
|
this.movePossible = movePossible;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param bool Set the undo button vissibility.
|
|
*/
|
|
public final void setUndoButtonVisible(final boolean bool) {
|
|
this.undoMove.setEnabled(bool);
|
|
}
|
|
|
|
/**
|
|
* Update the Tiles display in the gui.
|
|
*
|
|
* @param human Human amount of tiles.
|
|
* @param bot Amount of machine tiles.
|
|
*/
|
|
private void setTilesSet(int human, int bot) {
|
|
this.humanTiles.setText(String.valueOf(human));
|
|
this.botTiles.setText(String.valueOf(bot));
|
|
}
|
|
|
|
/**
|
|
* Updates the players on the playfield.
|
|
*
|
|
* @param model Board to update.
|
|
*/
|
|
public final void updatePlayField(final Reversi model) {
|
|
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
|
@Override
|
|
public final void run() {
|
|
playBoard.updatePlayBoard(model);
|
|
setTilesSet(model.getNumberOfHumanTiles(),
|
|
model.getNumberOfMachineTiles());
|
|
|
|
/* check movePossible just because of thread interference
|
|
when game is over
|
|
*/
|
|
if (model.gameOver() && movePossible) {
|
|
viewMessage(winnerMessage(model));
|
|
movePossible = false;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show a message dialog and play a system sound.
|
|
*
|
|
* @param msg Message to show.
|
|
*/
|
|
public final void viewMessage(final String msg) {
|
|
Toolkit.getDefaultToolkit().beep();
|
|
JOptionPane.showMessageDialog(this, msg);
|
|
}
|
|
|
|
/**
|
|
* Checks which player has won.
|
|
*
|
|
* @return Message with the winner.
|
|
*/
|
|
private String winnerMessage(Reversi model) {
|
|
if (model.getWinner() == null) {
|
|
return "Tie game!";
|
|
} else if (model.getWinner().getType()) {
|
|
return "You have won!";
|
|
} else {
|
|
return "The Computer has won!";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Pass the field listener of the controller to ReversiPlayBoard class.
|
|
*
|
|
* @param ma Mouseadapter
|
|
* @param field Field
|
|
*/
|
|
public final void setPlayFieldListener(
|
|
final MouseAdapter ma, final Field field) {
|
|
this.playBoard.setFieldListener(ma, field);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param l Actionlistener
|
|
*/
|
|
public final void setSwitchLevelListener(final ActionListener l) {
|
|
this.switchLevel.addActionListener(l);
|
|
this.switchLevel.setToolTipText("Choose a difficulty level");
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param l Actionlistener
|
|
*/
|
|
public final void setNewGameListener(final ActionListener l) {
|
|
this.newGame.addActionListener(l);
|
|
this.newGame.setMnemonic(KeyEvent.VK_N);
|
|
this.newGame.setToolTipText("Start new game");
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param l Actionlistener
|
|
*/
|
|
public final void setSwitchGameListener(final ActionListener l) {
|
|
this.switchGame.addActionListener(l);
|
|
this.switchGame.setMnemonic(KeyEvent.VK_S);
|
|
this.switchGame.setToolTipText(
|
|
"Switch first player and start new game");
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param l Actionlistener
|
|
*/
|
|
public final void setUndoMoveListener(final ActionListener l) {
|
|
this.undoMove.addActionListener(l);
|
|
this.undoMove.setMnemonic(KeyEvent.VK_U);
|
|
this.undoMove.setToolTipText("Undo last move");
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param l Actionlistener
|
|
*/
|
|
public final void setQuitGameListener(final ActionListener l) {
|
|
this.quitGame.addActionListener(l);
|
|
this.quitGame.setMnemonic(KeyEvent.VK_Q);
|
|
this.quitGame.setToolTipText("Quit the Game");
|
|
}
|
|
} |