-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
143 lines (118 loc) · 5.22 KB
/
Copy pathGrid.java
File metadata and controls
143 lines (118 loc) · 5.22 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
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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package suduko;
import java.awt.*;
import java.util.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Grid extends JPanel {
//instantiate grid of buttons.
JButton[][] gridButton;
ArrayList<JButton> testButtonList = new ArrayList<>();
//boolean to turn gridBurron on or off
boolean[][] oneTimeUse;
boolean[][] isEditable;
//adds responsive feel in ui
UI Ui;
int ROWS, COLUMNS;
public Grid(int ROWS, int COLUMNS, UI Ui, JPanel MENU_BAR_BOTTOM) {
//obejct is passed to link(IS_ERASABLE boolean) ie not create a new instance of ui class
this.Ui = Ui;
this.ROWS = ROWS;
this.COLUMNS = COLUMNS;
//gridLayout for suduko boxes entry
GridLayout gr = new GridLayout(ROWS, COLUMNS);
super(gr);
//length of gridButton and oneTimeUse
gridButton = new JButton[ROWS][COLUMNS];
oneTimeUse = new boolean[ROWS][COLUMNS];
isEditable = new boolean[ROWS][COLUMNS];
}
public void SetGrid() {
for (int i = 0; i <= ROWS - 1; i++) {
for (int j = 0; j <= COLUMNS - 1; j++) {
int _reffNo_i = i;
int _reffNo_j = j;
//instantiate the buttons.
gridButton[i][j] = new JButton("");
add(gridButton[i][j]);
testButtonList.add(gridButton[i][j]);
//input logic
ActionListener actionListener = (event) -> {
if (event.getSource() == gridButton[_reffNo_i][_reffNo_j]) {
//removes erase button at erase count 1
if (Ui.ERASER_COUNT == 1 && Ui.IS_ERASER == true) {
Ui.MENU_BAR_BOTTOM.remove(Ui.ERASER);
Ui.MENU_BAR_BOTTOM.revalidate();
Ui.MENU_BAR_BOTTOM.repaint();
}
//set string not equal to null
if (Ui.ERASER_COUNT <= 0) {
Ui.RemoveEraser(Ui.MENU_BAR_BOTTOM);
}
//combination of two bool to determine correct sequence of input
if (Ui.IS_ERASER == true && oneTimeUse[_reffNo_i][_reffNo_j] == Boolean.TRUE) {
if (!gridButton[_reffNo_i][_reffNo_j].getText().equals("") && isEditable[_reffNo_i][_reffNo_j]) {
Ui.ERASER_COUNT--;
Ui.ERASER.setText("ERASER *" + Ui.ERASER_COUNT);
}
oneTimeUse[_reffNo_i][_reffNo_j] = Boolean.TRUE;
SetText(_reffNo_i, _reffNo_j);
System.out.println(Ui.ERASER_COUNT);
} else if (Ui.IS_ERASER == true && oneTimeUse[_reffNo_i][_reffNo_j] == Boolean.FALSE) {
if (!gridButton[_reffNo_i][_reffNo_j].getText().equals("") && isEditable[_reffNo_i][_reffNo_j]) {
Ui.ERASER_COUNT--;
Ui.ERASER.setText("ERASER *" + Ui.ERASER_COUNT);
}
oneTimeUse[_reffNo_i][_reffNo_j] = Boolean.TRUE;
SetText(_reffNo_i, _reffNo_j);
} else if (Ui.IS_ERASER == false && oneTimeUse[_reffNo_i][_reffNo_j] == Boolean.TRUE) {
oneTimeUse[_reffNo_i][_reffNo_j] = Boolean.FALSE;
SetText(_reffNo_i, _reffNo_j);
} else {
return;
}
}
};
gridButton[i][j].addActionListener(actionListener);
}
}
}
//oneTimeUse 2Darray is set to be true by default here.
public void SET_ONE_TIME_USE(int ROWS, int COLUMNS) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
Arrays.fill(oneTimeUse[i], Boolean.TRUE);
Arrays.fill(oneTimeUse[j], Boolean.TRUE);
//System.out.println(i + "," + j + oneTimeUse[i][j]);
}
}
}
public void update() {
for (int i = 0; i <= ROWS - 1; i++) {
for (int j = 0; j <= COLUMNS - 1; j++) {
if (gridButton[i][j].getText().equals("")) {
isEditable[i][j] = true;
}
}
}
}
public void SetName(int index, String myName) {
testButtonList.get(index).setText(myName);
}
public String GetName(int index){
return testButtonList.get(index).getText();
}
//name of button is set here.
public void SetText(int i, int j) {
if (isEditable[i][j]) {
gridButton[i][j].setText(GetText());
}
}
//override it later
public String GetText() {
return "sexy";
}
}