Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Johan Gustav Thrane-Nielsen
inf101.v20.sem2.losning
Commits
851ca934
Commit
851ca934
authored
Apr 17, 2020
by
Anna Maria Eilertsen
Browse files
potential refactoring of GUI code
parent
4cde06b8
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/inf101/v20/sem2/mnkgames/GUI/GUIBoard.java
0 → 100644
View file @
851ca934
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
();
}
}
}
src/main/java/inf101/v20/sem2/mnkgames/GUI/GUIMain.java
View file @
851ca934
package
inf101.v20.sem2.mnkgames.GUI
;
package
inf101.v20.sem2.mnkgames.GUI
;
import
java.util.function.Supplier
;
import
java.util.function.Supplier
;
import
inf101.v20.sem2.mnkgames.ConnectFour
;
import
inf101.v20.sem2.mnkgames.ConnectFour
;
import
inf101.v20.sem2.mnkgames.MNKGame
;
import
inf101.v20.sem2.mnkgames.MNKGame
;
import
inf101.v20.sem2.mnkgames.TicTacToe
;
import
inf101.v20.sem2.mnkgames.TicTacToe
;
...
...
src/main/java/inf101/v20/sem2/mnkgames/GUI/GUIPiece.java
0 → 100644
View file @
851ca934
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
;
}
}
}
src/main/java/inf101/v20/sem2/mnkgames/GUI/MNKGameGUI.java
View file @
851ca934
package
inf101.v20.sem2.mnkgames.GUI
;
package
inf101.v20.sem2.mnkgames.GUI
;
import
java.awt.BorderLayout
;
import
java.awt.BorderLayout
;
import
java.awt.Color
;
import
java.awt.Dimension
;
import
java.awt.Dimension
;
import
java.awt.GridLayout
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
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.JButton
;
import
javax.swing.JFrame
;
import
javax.swing.JFrame
;
import
javax.swing.JLabel
;
import
javax.swing.JLabel
;
import
javax.swing.JOptionPane
;
import
javax.swing.JOptionPane
;
import
javax.swing.JPanel
;
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
;
import
inf101.v20.sem2.mnkgames.MNKGame.Piece
;
import
inf101.v20.sem2.twoPlayerGame.TwoPlayerGame
;
import
inf101.v20.sem2.twoPlayerGame.TwoPlayerGame
;
/**
/**
...
@@ -49,14 +41,15 @@ s * Initializes a JFrame in which we place the game
...
@@ -49,14 +41,15 @@ s * Initializes a JFrame in which we place the game
private
JButton
playConnectFourButton
;
private
JButton
playConnectFourButton
;
private
JButton
playTicTacToeButton
;
private
JButton
playTicTacToeButton
;
private
JLabel
statusMessage
;
private
JLabel
statusMessage
;
private
Grid
<
GamePanel
>
clickablePanels
;
private
GameSupplier
gameSupplier
;
private
GameSupplier
gameSupplier
;
private
TwoPlayerGame
<
MNKGame
>
game
;
private
TwoPlayerGame
<
MNKGame
>
game
;
private
GUIBoard
board
;
public
MNKGameGUI
(
GameSupplier
gameSupplier
)
{
public
MNKGameGUI
(
GameSupplier
gameSupplier
)
{
this
.
gameSupplier
=
gameSupplier
;
this
.
gameSupplier
=
gameSupplier
;
//initialize to default values
//initialize to default values
game
=
new
TwoPlayerGame
<>(
gameSupplier
.
getTicTacToeGame
());
game
=
new
TwoPlayerGame
<>(
gameSupplier
.
getTicTacToeGame
());
new
Timer
(
1000
,
this
);
}
}
/*
/*
...
@@ -74,6 +67,9 @@ s * Initializes a JFrame in which we place the game
...
@@ -74,6 +67,9 @@ s * Initializes a JFrame in which we place the game
if
(
e
.
getSource
()
==
playTicTacToeButton
)
{
if
(
e
.
getSource
()
==
playTicTacToeButton
)
{
reset
(
gameSupplier
.
getTicTacToeGame
());
reset
(
gameSupplier
.
getTicTacToeGame
());
}
}
if
(
e
.
getSource
()
instanceof
Timer
)
{
endOfTurn
();
}
}
}
private
void
reset
(
MNKGame
newGame
)
{
private
void
reset
(
MNKGame
newGame
)
{
...
@@ -87,8 +83,8 @@ s * Initializes a JFrame in which we place the game
...
@@ -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
* Should be called after a click to update the UI to reflect the current game state
*/
*/
p
rivate
void
endOfTurn
()
{
p
ublic
void
endOfTurn
()
{
clickablePanels
.
forEach
(
item
->
item
.
updateToGame
()
);
updateBoard
(
);
updateMessage
();
updateMessage
();
MNKGameGUI
.
super
.
updateUI
();
MNKGameGUI
.
super
.
updateUI
();
if
(!
game
.
hasPossibleMoves
())
{
if
(!
game
.
hasPossibleMoves
())
{
...
@@ -96,6 +92,10 @@ s * Initializes a JFrame in which we place the game
...
@@ -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
* Helper method to control the status message
*/
*/
...
@@ -113,111 +113,23 @@ s * Initializes a JFrame in which we place the game
...
@@ -113,111 +113,23 @@ s * Initializes a JFrame in which we place the game
* Should be called once a game stops
* Should be called once a game stops
*/
*/
private
void
stop
()
{
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
@Override
public
Dimension
getPreferredSize
()
{
public
Dimension
getPreferredSize
()
{
return
new
Dimension
(
100
*
game
.
height
(),
100
*
game
.
width
());
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.
* Sets up the GUI.
*/
*/
public
void
initialize
()
{
public
void
initialize
()
{
setLayout
(
new
BorderLayout
());
setLayout
(
new
BorderLayout
());
JPanel
p
=
new
JPanel
();
board
=
new
GUIBoard
(
this
,
game
);
p
.
setLayout
(
new
GridLayout
(
game
.
height
(),
game
.
width
()));
add
(
board
);
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
);
JPanel
buttons
=
createButtonPanel
();
JPanel
buttons
=
createButtonPanel
();
add
(
buttons
,
BorderLayout
.
SOUTH
);
add
(
buttons
,
BorderLayout
.
SOUTH
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment