/
GUILogi5Board.java
161 lines (127 loc) · 5.35 KB
/
GUILogi5Board.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.awt.*;
import javax.swing.*;
import java.awt.event.* ;
import javax.swing.JFrame;
import java.util.ArrayList;
public class GUILogi5Board extends JPanel {
private Game game;
private JButton[][] spaces;
private GUILogi5Space space;
private int dim;
private int spaceSize = 50;
private Color color;
private Color[] possibleColors;
private Group[] groups;
public GUILogi5Board(Game game){
this.game = game;
dim = game.getDimensions()[0];
setLayout(new GridLayout(dim,dim));
spaces = new JButton[dim][dim];
for (int i = 0; i < spaces.length; i++){
for (int j = 0; j < spaces[i].length; j++){
Space otherSpace = game.getSpaceAt(i,j);
GUILogi5Space spacey = new GUILogi5Space(i,j,spaceSize);
int[] loc = spacey.getDims();
spacey.setSpace(game.getSpaceAt(loc[0],loc[1]));
spaces[i][j] = spacey;
add(spacey);
}
}
setBorder(BorderFactory.createLineBorder(Color.black));
setPreferredSize(new Dimension(spaceSize*spaces.length,spaceSize*spaces[0].length));
setMaximumSize(new Dimension(spaceSize*spaces.length,spaceSize*spaces[0].length));
int num = 255;
Color[] colors = new Color[6];
for (int i = 0; i < colors.length; i++){
colors[i] = new Color(num,num,num,220);
num -= 30;
}
possibleColors = colors;
}
public void setUpGroups(){
this.groups = game.getGroups();
Color[] groupToColor = new Color[groups.length];
for (int i = 0; i < groupToColor.length; i++){
groupToColor[i] = possibleColors[i%possibleColors.length];
}
for (int i = 0; i < groupToColor.length; i++){
Group[] conflictingGroups;
conflictingGroups = conflicting(i, groupToColor);
int noOfTimes = 0;
while (conflictingGroups.length != 0){
int offset;
if (noOfTimes > 5)
offset = 3;
else
offset = 2;
groupToColor[conflictingGroups[0].groupIDX] = possibleColors[(conflictingGroups[0].groupIDX+offset)%possibleColors.length];
conflictingGroups = conflicting(i, groupToColor);
noOfTimes++;
}
}
for (int i = 0; i < groups.length; i++){
Space[] groupSpaces = groups[i].getSpaces();
for (int j = 0; j < groupSpaces.length; j++){
Space current = groupSpaces[j];
GUILogi5Space currentSpace = (GUILogi5Space)(spaces[current.getX()][current.getY()]);
currentSpace.setColor(groupToColor[i]);
}
}
}
private Group[] conflicting(int ish, Color[] groupToColor){
ArrayList<Group> touchers = new ArrayList<Group>();
Group g = groups[ish];
Space[] gSpaces = g.getSpaces();
for (int i = 0; i < gSpaces.length; i++){
Space current = gSpaces[i];
for (int k = -1; k < 2; k++){
int curSX = current.getX();
int curSY = current.getY();
curSX += k;
curSY += k;
if (curSX < 0){
curSX = 0;
}
if (curSX > spaces.length-1){
curSX = spaces.length-1;
}
if (curSY < 0){
curSY = 0;
}
if (curSY > spaces.length-1){
curSY = spaces.length-1;
}
Space[] comparison = new Space[2];
comparison[0] = game.getSpaceAt(curSX, current.getY());
comparison[1] = game.getSpaceAt(current.getX(), curSY);
int mod = groupToColor.length;
for (int j = 0; j < comparison.length; j++){
if ((comparison[j].getGroup().groupIDX != g.groupIDX) &&
groupToColor[comparison[j].getGroup().groupIDX%mod] == groupToColor[current.getGroup().groupIDX%mod] &&
!touchers.contains(comparison[j].getGroup())){
touchers.add(comparison[j].getGroup());
}
}
}
}
return touchers.toArray(new Group[touchers.size()]);
}
public void removeListeners(){
for (JButton[] panel : spaces){
for (int i = 0; i < panel.length; i++){
Component[] camps = panel[i].getComponents();
Component comp = null;
for (int j = 0; j < camps.length; j++){
comp = camps[j];
if (comp instanceof JButton){
JButton jb = (JButton)(camps[j]);
ActionListener[] lastnrs = jb.getActionListeners();
for (int k = 0; k < lastnrs.length; k++){
jb.removeActionListener(lastnrs[k]);
}
}
}
}
}
}
}