Repository navigation
Expand file tree
/
Copy pathTicTacToeButton.java
More file actions
116 lines (107 loc) · 2.9 KB
/
Copy pathTicTacToeButton.java
File metadata and controls
116 lines (107 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* A button to hold the user interaction parts
* for the TicTacToe board.
*
* @summary X or O button
* @author Ainsley Pyle
*
* Created at : 2023-10-16 19:30:52
* Last modified : 2023-10-16 19:30:52
*/
//package TicTacToeLab;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.*;
import java.io.File;
import java.io.IOException;
/**
* the button that the user will interact with.
* it will have an x or o when the button is pressed.
*/
public class TicTacToeButton extends JButton {
private ImageIcon m_image;
private TicTacToePanel m_panel;
private boolean m_state = false; //this tells if we are set or not?
private int m_xoValue = 0; //-1 is a O 1 is X 0 is not set.
public int btnIDx;
public int btnIDy;
private File fx = new File("C:\\Users\\ainsl\\OneDrive\\Desktop\\JavaBoot\\Lab6\\X.png");
private File fo = new File("C:\\Users\\ainsl\\OneDrive\\Desktop\\JavaBoot\\Lab6\\O.png");
TicTacToeButton (TicTacToePanel parent, int x, int y) {
m_panel = parent;
btnIDx = x;
btnIDy = y;
addActionListener(new TicTacToeButtonListener());
}
/**
* set the icon on the button.
* @param icn x to put an x icon on the button o to put an o icon on.
*/
void setIconImage (char icn) {
Image ii;
try{
if(icn == 'x'){
ii = ImageIO.read(fx);
m_xoValue = 1;
}
else{
ii = ImageIO.read(fo);
m_xoValue = -1;
}
m_image = new ImageIcon(ii);
setIcon(m_image);
}catch(IOException ioe) {
System.out.println("unable to read icon image");
}
}
/**
* get the panel that holds the button
*/
public TicTacToePanel getParent() {
return m_panel;
}
/**
* get the state (has this button been pressed.)
* @return true if the button has been pressed.
*/
boolean getState () {
return m_state;
}
/**
* set the state of the button
* @param true to set that the button has been pressed.
*/
void setState(boolean state){
m_state = state;
}
/**
* set the x or o value -1 or 1
* @param x
*/
void setXOValue (int x) {
m_xoValue = x;
}
/**
* gets the x or o value -1 or 1
* @return 1 for x -1 for o
*/
int getXOValue () {
return m_xoValue;
}
/**
* check if the value of this button is the same as another button.
*/
boolean equals(TicTacToeButton tb){
return m_xoValue == tb.getXOValue();
}
/**
* clear the button.
*/
void reset () {
m_state = false;
m_xoValue = 0;
setIcon(null);
setEnabled(true);
}
}