Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TicTacToe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
We are moving to Forgejo!
You are on a read-only GitLab instance.
Show more breadcrumbs
La Fée Verte
TicTacToe
Commits
b60acf7e
Commit
b60acf7e
authored
4 years ago
by
Bradley M. Small
Browse files
Options
Downloads
Patches
Plain Diff
removing MouseListener in favor of MouseAdaptor
parent
74fd75f1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/com/small/tictactoe/GameBoardPanel.java
+37
-42
37 additions, 42 deletions
src/main/java/com/small/tictactoe/GameBoardPanel.java
with
37 additions
and
42 deletions
src/main/java/com/small/tictactoe/GameBoardPanel.java
+
37
−
42
View file @
b60acf7e
...
@@ -8,19 +8,28 @@ package com.small.tictactoe;
...
@@ -8,19 +8,28 @@ package com.small.tictactoe;
import
javax.swing.*
;
import
javax.swing.*
;
import
java.awt.*
;
import
java.awt.*
;
import
java.awt.event.MouseAdapter
;
import
java.awt.event.MouseEvent
;
import
java.awt.event.MouseEvent
;
import
java.awt.event.MouseListener
;
public
class
GameBoardPanel
extends
JPanel
implements
MouseListener
{
/**
*
*/
public
class
GameBoardPanel
extends
JPanel
{
private
final
GameTile
[][]
gameTiles
=
new
GameTile
[
3
][
3
];
private
final
GameTile
[][]
gameTiles
=
new
GameTile
[
3
][
3
];
private
final
transient
TicTacToeGamePlayer
player
;
private
final
transient
TicTacToeGamePlayer
player
;
/**
* @param player interface for handling game logic
*/
GameBoardPanel
(
TicTacToeGamePlayer
player
)
{
GameBoardPanel
(
TicTacToeGamePlayer
player
)
{
this
.
player
=
player
;
this
.
player
=
player
;
initGUI
();
initGUI
();
newGame
();
newGame
();
}
}
/**
*
*/
public
void
newGame
()
{
public
void
newGame
()
{
player
.
newGame
();
player
.
newGame
();
clearTable
();
clearTable
();
...
@@ -54,7 +63,7 @@ public class GameBoardPanel extends JPanel implements MouseListener {
...
@@ -54,7 +63,7 @@ public class GameBoardPanel extends JPanel implements MouseListener {
case
0
->
g
.
fillRect
(
20
,
getHeight
()
/
6
-
10
,
getWidth
()
-
40
,
20
);
case
0
->
g
.
fillRect
(
20
,
getHeight
()
/
6
-
10
,
getWidth
()
-
40
,
20
);
case
1
->
g
.
fillRect
(
20
,
(
getHeight
()
/
6
)
*
3
-
10
,
getWidth
()
-
40
,
20
);
case
1
->
g
.
fillRect
(
20
,
(
getHeight
()
/
6
)
*
3
-
10
,
getWidth
()
-
40
,
20
);
case
2
->
g
.
fillRect
(
20
,
getHeight
()
/
6
*
5
-
10
,
getWidth
()
-
40
,
20
);
case
2
->
g
.
fillRect
(
20
,
getHeight
()
/
6
*
5
-
10
,
getWidth
()
-
40
,
20
);
default
->
throw
new
IllegalStateException
(
"Unexpected value: "
+
winRow
);
default
->
throw
new
IllegalStateException
(
"Unexpected
row
value: "
+
winRow
);
}
}
}
}
if
(
winDirection
==
'c'
)
{
if
(
winDirection
==
'c'
)
{
...
@@ -62,7 +71,7 @@ public class GameBoardPanel extends JPanel implements MouseListener {
...
@@ -62,7 +71,7 @@ public class GameBoardPanel extends JPanel implements MouseListener {
case
0
->
g
.
fillRect
(
getWidth
()
/
6
-
10
,
20
,
20
,
getHeight
()
-
40
);
case
0
->
g
.
fillRect
(
getWidth
()
/
6
-
10
,
20
,
20
,
getHeight
()
-
40
);
case
1
->
g
.
fillRect
(
getWidth
()
/
6
*
3
-
10
,
20
,
20
,
getHeight
()
-
40
);
case
1
->
g
.
fillRect
(
getWidth
()
/
6
*
3
-
10
,
20
,
20
,
getHeight
()
-
40
);
case
2
->
g
.
fillRect
(
getWidth
()
/
6
*
5
-
10
,
20
,
20
,
getHeight
()
-
40
);
case
2
->
g
.
fillRect
(
getWidth
()
/
6
*
5
-
10
,
20
,
20
,
getHeight
()
-
40
);
default
->
throw
new
IllegalStateException
(
"Unexpected value: "
+
winColumn
);
default
->
throw
new
IllegalStateException
(
"Unexpected
column
value: "
+
winColumn
);
}
}
}
}
if
(
winDirection
==
'd'
)
{
if
(
winDirection
==
'd'
)
{
...
@@ -95,23 +104,43 @@ public class GameBoardPanel extends JPanel implements MouseListener {
...
@@ -95,23 +104,43 @@ public class GameBoardPanel extends JPanel implements MouseListener {
gridBagConstraints
.
fill
=
GridBagConstraints
.
BOTH
;
gridBagConstraints
.
fill
=
GridBagConstraints
.
BOTH
;
gridBagConstraints
.
insets
=
new
Insets
(
30
,
30
,
30
,
30
);
gridBagConstraints
.
insets
=
new
Insets
(
30
,
30
,
30
,
30
);
gridBagConstraints
.
gridx
=
0
;
gridBagConstraints
.
gridy
=
0
;
gridBagConstraints
.
gridwidth
=
1
;
gridBagConstraints
.
gridwidth
=
1
;
gridBagConstraints
.
gridheight
=
1
;
gridBagConstraints
.
gridheight
=
1
;
gridBagConstraints
.
weightx
=
gridBagConstraints
.
weighty
=
1.0
;
gridBagConstraints
.
weightx
=
gridBagConstraints
.
weighty
=
1.0
;
initializeTiles
(
gridBagConstraints
);
}
private
void
initializeTiles
(
GridBagConstraints
gridBagConstraints
)
{
for
(
int
column
=
0
;
column
<
3
;
++
column
)
{
for
(
int
column
=
0
;
column
<
3
;
++
column
)
{
for
(
int
row
=
0
;
row
<
3
;
++
row
)
{
for
(
int
row
=
0
;
row
<
3
;
++
row
)
{
gridBagConstraints
.
gridx
=
column
;
gridBagConstraints
.
gridx
=
column
;
gridBagConstraints
.
gridy
=
row
;
gridBagConstraints
.
gridy
=
row
;
gameTiles
[
row
][
column
]
=
new
GameTile
();
gameTiles
[
row
][
column
]
=
new
GameTile
();
gameTiles
[
row
][
column
].
addMouseListener
(
this
);
gameTiles
[
row
][
column
].
addMouseListener
(
new
MouseAdapter
()
{
@Override
public
void
mousePressed
(
MouseEvent
e
)
{
if
(
e
.
getButton
()
==
1
)
{
GameTile
tile
=
(
GameTile
)
e
.
getSource
();
playTilesSquare
(
tile
);
}
}
});
add
(
gameTiles
[
row
][
column
],
gridBagConstraints
);
add
(
gameTiles
[
row
][
column
],
gridBagConstraints
);
}
}
}
}
}
}
private
void
playTilesSquare
(
GameTile
tile
)
{
for
(
int
row
=
0
;
row
<
3
;
++
row
)
{
for
(
int
column
=
0
;
column
<
3
;
++
column
)
{
if
(
gameTiles
[
row
][
column
]
==
tile
)
{
playSquare
(
row
,
column
);
}
}
}
}
private
void
playSquare
(
int
row
,
int
column
)
{
private
void
playSquare
(
int
row
,
int
column
)
{
Character
xOrO
=
player
.
playSquare
(
row
,
column
);
Character
xOrO
=
player
.
playSquare
(
row
,
column
);
if
(
xOrO
==
null
)
{
if
(
xOrO
==
null
)
{
...
@@ -136,38 +165,4 @@ public class GameBoardPanel extends JPanel implements MouseListener {
...
@@ -136,38 +165,4 @@ public class GameBoardPanel extends JPanel implements MouseListener {
}
}
}
}
}
}
@Override
public
void
mouseClicked
(
MouseEvent
e
)
{
if
(
e
.
getButton
()
==
1
)
{
GameTile
tile
=
(
GameTile
)
e
.
getSource
();
for
(
int
row
=
0
;
row
<
3
;
++
row
)
{
for
(
int
column
=
0
;
column
<
3
;
++
column
)
{
if
(
gameTiles
[
row
][
column
]
==
tile
)
{
playSquare
(
row
,
column
);
}
}
}
}
}
@Override
public
void
mousePressed
(
MouseEvent
e
)
{
// ignoring this action
}
@Override
public
void
mouseReleased
(
MouseEvent
e
)
{
// ignoring this action
}
@Override
public
void
mouseEntered
(
MouseEvent
e
)
{
// ignoring this action
}
@Override
public
void
mouseExited
(
MouseEvent
e
)
{
// ignoring this action
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment