added backonestep method to retract steps from user and repaint panel

gui
beratkocak 2024-12-20 14:04:03 +01:00
parent 6a505ef34d
commit 8dcb29d701
1 changed files with 57 additions and 0 deletions

View File

@ -108,6 +108,63 @@ public class GameGUI {
madeMoves.push(logEntrance);
}
public static void backOneStep(Stack<String> movesMade, JButton[][] buttons, String[][] colors, JPanel grid){
try {
String move = movesMade.pop();
String[] line = move.split("\\.");
String y = line[0];
String x = line[1];
String color = line[2];
if(color.equals("W")){
int i = Integer.parseInt(y);
int j = Integer.parseInt(x);
JButton b0 = buttons[i][j];
b0.setOpaque(true);
b0.setForeground(Color.WHITE);
b0.setContentAreaFilled(true);
b0.setBorderPainted(false);
b0.setFocusPainted(false);
b0.setBackground(Color.BLACK);
buttons[i][j] = b0;
String str = colors[i][j];
String str0 = str.substring(0, str.length() - 1);
colors[i][j] = str0;
grid.repaint();
gridUpdate(grid, buttons);
}else if(color.equals("G")){
int i = Integer.parseInt(y);
int j = Integer.parseInt(x);
JButton b0 = buttons[i][j];
b0.setOpaque(true);
b0.setForeground(Color.BLACK);
b0.setContentAreaFilled(true);
b0.setBorderPainted(false);
b0.setFocusPainted(false);
b0.setBackground(Color.WHITE);
buttons[i][j] = b0;
String str = colors[i][j];
String str0 = str.substring(0, str.length() - 1);
colors[i][j] = str0;
gridUpdate(grid, buttons);
} else if(color.equals("B")){
int i = Integer.parseInt(y);
int j = Integer.parseInt(x);
JButton b0 = buttons[i][j];
b0.setOpaque(true);
b0.setForeground(Color.BLACK);
b0.setContentAreaFilled(true);
b0.setBorderPainted(false);
b0.setFocusPainted(false);
b0.setBackground(Color.lightGray);
buttons[i][j] = b0;
String str = colors[i][j];
String str0 = str.substring(0, str.length() - 1);
colors[i][j] = str0;
gridUpdate(grid, buttons);
}
} catch(EmptyStackException e) {}
}
public static void gridUpdate(JPanel grid, JButton[][] buttons){
grid.removeAll();
grid.repaint();