Commit 851ca934 authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

potential refactoring of GUI code

parent 4cde06b8
package inf101.v20.sem2.mnkgames.GUI;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import inf101.v20.lab4.grid.Grid;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.twoPlayerGame.TwoPlayerGame;
public class GUIBoard extends JPanel{
private Grid<GUIPiece> clickablePanels;
private TwoPlayerGame<MNKGame> game;
private MNKGameGUI gui;
private static final long serialVersionUID = 1L;
public GUIBoard(MNKGameGUI gui, TwoPlayerGame<MNKGame> game) {
this.game = game;
this.gui = gui;
setLayout(new GridLayout(game.height(), game.width()));
setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
setRequestFocusEnabled(true);
requestFocus();
fillWithClickable(game.height(), game.width());
}
public void updateToGame() {
for(int y = 0; y<clickablePanels.getHeight(); y++) {
for (int x=0; x<clickablePanels.getWidth(); x++) {
clickablePanels.get(x, y).updateToGame(game.pieceAt(x, y));
}
}
}
private void fillWithClickable(int height, int width) {
this.clickablePanels = new Grid<GUIPiece>(width, height, null);
for(int y = 0; y<height; y++) {
for (int x=0; x<width; x++) {
GUIPiece pan = new GUIPiece(x, y);
pan.addMouseListener(new GameClickListener());
add(pan);
pan.updateToGame(game.pieceAt(x, y));
clickablePanels.set(x, y, pan);
}
}
}
/**
* Makes all panels unclickable
*/
public void makeAllUnclickable() {
clickablePanels.forEach(GUIPiece::makeUnClickable);
}
/**
* Class that describes what happens on click
* connects the click with the game
* then callback to GUI
*/
class GameClickListener extends MouseAdapter{
public void mousePressed(MouseEvent me){
GUIPiece clickedPanel =(GUIPiece)me.getSource(); // get the reference to the box that was clicked
game.takeTurnforHumanPlayer(clickedPanel.X, clickedPanel.Y);
updateToGame();
gui.endOfTurn();
}
}
}
package inf101.v20.sem2.mnkgames.GUI;
import java.util.function.Supplier;
import inf101.v20.sem2.mnkgames.ConnectFour;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.TicTacToe;
......
package inf101.v20.sem2.mnkgames.GUI;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import inf101.v20.sem2.mnkgames.MNKGame.Piece;
public class GUIPiece extends JPanel{
private static final long serialVersionUID = 1L;
public final int X, Y;
private Color c;
public GUIPiece(int x, int y) {
this.X = x;
this.Y = y;
setEnabled(true);
setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
private void removeMouseListeners() {
MouseListener[] mouseListeners = this.getMouseListeners();
for (int i = 0; i < mouseListeners.length; i++) {
this.removeMouseListener(mouseListeners[i]);
}
}
public Dimension getPreferredSize() {
return new Dimension(3, 3);
}
@Override
public Color getBackground() {
return c;
}
void updateToGame(Piece p) {
if(p != Piece.NONE) {
makeUnClickable();
}
c = pieceToColor(p);
}
public void makeUnClickable() {
removeMouseListeners();
}
/**
* Maps from Piece values to colors
*
* @param pieceAt The piece to be drawn
* @return The color that this GUI implementation associates with the provided piece
*/
private static Color pieceToColor(Piece pieceAt) {
switch(pieceAt) {
case BLACK : return Color.getHSBColor(90, 65, 20.3f);
case WHITE : return Color.getHSBColor(308, 26, 34);
default: return Color.WHITE;
}
}
}
package inf101.v20.sem2.mnkgames.GUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import inf101.v20.lab4.grid.Grid;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.MNKGame.Piece;
import inf101.v20.sem2.twoPlayerGame.TwoPlayerGame;
/**
......@@ -49,14 +41,15 @@ s * Initializes a JFrame in which we place the game
private JButton playConnectFourButton;
private JButton playTicTacToeButton;
private JLabel statusMessage;
private Grid<GamePanel> clickablePanels;
private GameSupplier gameSupplier;
private TwoPlayerGame<MNKGame> game;
private GUIBoard board;
public MNKGameGUI(GameSupplier gameSupplier) {
this.gameSupplier = gameSupplier;
//initialize to default values
game = new TwoPlayerGame<>(gameSupplier.getTicTacToeGame());
new Timer(1000, this);
}
/*
......@@ -74,6 +67,9 @@ s * Initializes a JFrame in which we place the game
if(e.getSource() == playTicTacToeButton) {
reset(gameSupplier.getTicTacToeGame());
}
if(e.getSource() instanceof Timer) {
endOfTurn();
}
}
private void reset(MNKGame newGame) {
......@@ -87,8 +83,8 @@ s * Initializes a JFrame in which we place the game
/**
* Should be called after a click to update the UI to reflect the current game state
*/
private void endOfTurn() {
clickablePanels.forEach(item -> item.updateToGame());
public void endOfTurn() {
updateBoard();
updateMessage();
MNKGameGUI.super.updateUI();
if(!game.hasPossibleMoves()) {
......@@ -96,6 +92,10 @@ s * Initializes a JFrame in which we place the game
}
}
private void updateBoard() {
board.updateToGame();
}
/**
* Helper method to control the status message
*/
......@@ -113,111 +113,23 @@ s * Initializes a JFrame in which we place the game
* Should be called once a game stops
*/
private void stop() {
clickablePanels.forEach(item -> item.removeMouseListeners());
board.makeAllUnclickable();
}
//Class that defines what happens (i.e: the color changes) when a panel is clicked
class BoxListener extends MouseAdapter{
public void mousePressed(MouseEvent me){
GamePanel clickedPanel =(GamePanel)me.getSource(); // get the reference to the box that was clicked
clickedPanel.click();
}
}
/**
* Maps from Piece values to colors
*
* @param pieceAt The piece to be drawn
* @return The color that this GUI implementation associates with the provided piece
*/
protected Color pieceToColor(Piece pieceAt) {
switch(pieceAt) {
case BLACK : return Color.getHSBColor(90, 65, 20.3f);
case WHITE : return Color.getHSBColor(308, 26, 34);
default: return Color.WHITE;
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100*game.height(), 100*game.width());
}
/**
* A class for clickable panels with x,y-coordinates
*
* @author anna
*
*/
private class GamePanel extends JPanel{
private int x, y;
private static final long serialVersionUID = 1L;
public GamePanel(int x, int y) {
this.x = x;
this.y = y;
}
private void removeMouseListeners() {
MouseListener[] mouseListeners = getMouseListeners();
for (int i = 0; i < mouseListeners.length; i++) {
this.removeMouseListener(mouseListeners[i]);
}
}
private void updateToGame() {
if(filled()) {
makeUnClickable();
}
}
private void makeUnClickable() {
removeMouseListeners();
}
private boolean filled() {
return game.hasPieceAt(x, y);
}
public void click() {
game.takeTurnforHumanPlayer(x, y);
endOfTurn();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(3, 3);
}
@Override
public Color getBackground() {
return pieceToColor(game.pieceAt(x, y));
}
}
/**
* Sets up the GUI.
*/
public void initialize() {
setLayout(new BorderLayout());
JPanel p = new JPanel();
p.setLayout(new GridLayout(game.height(), game.width()));
p.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
this.clickablePanels = new Grid<GamePanel>(game.width(), game.height(), null);
for(int y = 0; y<game.height(); y++) {
for (int x=0; x<game.width(); x++) {
GamePanel pan = new GamePanel(x, y);
pan.setEnabled(true);
pan.setBorder(BorderFactory.createLineBorder(Color.GRAY));
pan.addMouseListener(new BoxListener()); // add a mouse listener to make the panels clickable
p.add(pan);
clickablePanels.set(x, y, pan);
}
}
p.setRequestFocusEnabled(true);
p.requestFocus();
add(p);
board = new GUIBoard(this, game);
add(board);
JPanel buttons = createButtonPanel();
add(buttons, BorderLayout.SOUTH);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment