fix java shit
This commit is contained in:
BIN
Reversi/reversi/view/ReversiField.class
Normal file
BIN
Reversi/reversi/view/ReversiField.class
Normal file
Binary file not shown.
83
Reversi/reversi/view/ReversiField.java
Normal file
83
Reversi/reversi/view/ReversiField.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package reversi.view;
|
||||
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.MouseAdapter;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import reversi.model.Field;
|
||||
import reversi.model.Player;
|
||||
|
||||
/**
|
||||
* Class for a single play field panel. Paints the circles with the color of
|
||||
* the Player.
|
||||
*/
|
||||
public class ReversiField extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = -5762403586769495603L;
|
||||
private int height;
|
||||
private int width;
|
||||
private int y;
|
||||
private int x;
|
||||
private final Field field;
|
||||
private Player player;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param field <code>Field</code> with coordinates of the play field.
|
||||
*/
|
||||
public ReversiField(final Field field) {
|
||||
this.field = field;
|
||||
setPlayer(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public final void paintComponent(final Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
height = (int) (super.getHeight() * 0.8);
|
||||
width = (int) (super.getWidth() * 0.8);
|
||||
y = (int) (width * 0.1 + 2);
|
||||
x = (int) (height * 0.1 + 2);
|
||||
|
||||
if (player != null && player.getType()) {
|
||||
g.setColor(Color.BLUE);
|
||||
g.fillOval(y, x, width, height);
|
||||
} else if (player != null && !player.getType()) {
|
||||
g.setColor(Color.RED);
|
||||
g.fillOval(y, x, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <code>Field</code> with coordinates of the play field.
|
||||
*/
|
||||
public final Field getField() {
|
||||
return this.field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the listener created in the controller.
|
||||
*
|
||||
* @param ma Mouseadapter
|
||||
*/
|
||||
public final void setListener(final MouseAdapter ma) {
|
||||
this.addMouseListener(ma);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a player on a field (draw the circle in right color).
|
||||
*
|
||||
* @param player Player that is set.
|
||||
*/
|
||||
public void setPlayer(Player player) {
|
||||
this.player = player;
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
BIN
Reversi/reversi/view/ReversiPlayBoard.class
Normal file
BIN
Reversi/reversi/view/ReversiPlayBoard.class
Normal file
Binary file not shown.
70
Reversi/reversi/view/ReversiPlayBoard.java
Normal file
70
Reversi/reversi/view/ReversiPlayBoard.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package reversi.view;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.MouseAdapter;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.CompoundBorder;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.border.MatteBorder;
|
||||
|
||||
import reversi.model.Board;
|
||||
import reversi.model.Field;
|
||||
import reversi.model.Reversi;
|
||||
|
||||
/**
|
||||
* Create a reversi playboard for the reversi game gui.
|
||||
*/
|
||||
public class ReversiPlayBoard extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 2725183063551812386L;
|
||||
private ReversiField[][] playField = new ReversiField
|
||||
[Board.SIZE][Board.SIZE];
|
||||
|
||||
/**
|
||||
* Init array with all reversi fields. Set background and tooltip of
|
||||
* fields.
|
||||
*/
|
||||
public ReversiPlayBoard() {
|
||||
this.setLayout(new GridLayout(Board.SIZE, Board.SIZE));
|
||||
this.setBorder(new CompoundBorder(
|
||||
null, new EmptyBorder(0, 0, 10, 10)));
|
||||
|
||||
for (int row = 0; row < Board.SIZE; row++) {
|
||||
for (int col = 0; col < Board.SIZE; col++) {
|
||||
playField[row][col] = new ReversiField(new Field(row, col));
|
||||
playField[row][col].setBorder(
|
||||
new MatteBorder(1, 1, 0, 0, Color.BLACK));
|
||||
playField[row][col].setBackground(new Color(0, 170, 0));
|
||||
playField[row][col].setToolTipText("Click to make a move!");
|
||||
this.add(playField[row][col]);
|
||||
}
|
||||
}
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update hole playboard players.
|
||||
*
|
||||
* @param board Board to update.
|
||||
*/
|
||||
public void updatePlayBoard(Reversi board) {
|
||||
for (int row = 0; row < Board.SIZE; row++) {
|
||||
for (int col = 0; col < Board.SIZE; col++) {
|
||||
this.playField[row][col].setPlayer(board.getSlot(row, col));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set mouselistener of <code>field</code>
|
||||
*
|
||||
* @param ma Mouseadapter
|
||||
* @param field Field
|
||||
*/
|
||||
public final void setFieldListener(
|
||||
final MouseAdapter ma, final Field field) {
|
||||
this.playField[field.row()][field.col()].setListener(ma);
|
||||
}
|
||||
}
|
||||
BIN
Reversi/reversi/view/ReversiView$1.class
Normal file
BIN
Reversi/reversi/view/ReversiView$1.class
Normal file
Binary file not shown.
BIN
Reversi/reversi/view/ReversiView.class
Normal file
BIN
Reversi/reversi/view/ReversiView.class
Normal file
Binary file not shown.
292
Reversi/reversi/view/ReversiView.java
Normal file
292
Reversi/reversi/view/ReversiView.java
Normal file
@@ -0,0 +1,292 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user