-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTicTacToeWindow.java
83 lines (67 loc) · 1.86 KB
/
TicTacToeWindow.java
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
package TicTacToe;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TicTacToeWindow extends JFrame {
private JButton[][] gridButtons= new JButton[3][3];
private ImageIcon xImage;
private ImageIcon oImage;
private ImageIcon blankImage;
public enum icon {X,O,BLANK};
public void setImagePosition (int x, int y, icon a){
switch(a){
case X:
gridButtons[x][y].setIcon(xImage);
break;
case O:
gridButtons[x][y].setIcon(oImage);
break;
case BLANK:
gridButtons[x][y].setIcon(blankImage);
break;
}
}
public icon getImagePosition (int x, int y){
JButton b= gridButtons[x][y];
if(b.getIcon()==xImage){
return icon.X;
}else if(b.getIcon()==xImage){
return icon.O;
}else{
return icon.BLANK;
}
}
public static void main(String[] args) {
TicTacToeWindow w= new TicTacToeWindow();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setResizable(false);
w.setSize(600,600);
w.setTitle("Tic Tac Toe");
w.setVisible(true);
}
public void setEnabledGrid(int x, int y, boolean e){
gridButtons[x][y].setEnabled(e);
}
public void addActionGrid(int x, int y, ActionListener a){
gridButtons[x][y].addActionListener(a);
}
public TicTacToeWindow(){
super();
xImage= new ImageIcon("TicTacToeX.png");
oImage= new ImageIcon("TicTacToeO.png");
blankImage= new ImageIcon("BlankSpace.png");
this.setLayout(new GridLayout(3,3));
for (int i=0; i<3;i++){
for (int j=0; j<3;j++){
gridButtons[i][j]= new JButton();
gridButtons[i][j].setIcon(blankImage);
this.add(gridButtons[i][j],i,j);
}
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(600,600);
this.setTitle("Tic Tac Toe");
this.setVisible(true);
}
}