I'm trying to make a basic 2D Rubik Cube 3x3 but I'm having problems with the square's colors, the problem comes when I start mixing the positions causing the colors to change not accordingly and I dont know how to fix it.
Here is the code. Thank you very much in advance for your help and time.
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == upLeft){
Collections.swap(squaresList, 0, 27);
Collections.swap(squaresList, 3, 30);
Collections.swap(squaresList, 6, 33);
Collections.swap(squaresList, 27, 18);
Collections.swap(squaresList, 30, 21);
Collections.swap(squaresList, 33, 24);
Collections.swap(squaresList, 18, 9);
Collections.swap(squaresList, 21, 12);
Collections.swap(squaresList, 24, 15);
// HERE IS WHERE I THINK THE PROBLEM IS...BUT I CANT SEE PAST THE PROBLEM
// I TRIED USING HASHMAP SO EACH SQUARE HAS A UNIQUE CODE FOR ITS COLOR BUT DIDNT WORK OUT...
for(int i = 0; i < squaresList.size(); i++){
if(i <= 8){
squaresList.get(i).setBackground(Color.WHITE);
}else if(i >= 9 && i <= 17){
squaresList.get(i).setBackground(Color.YELLOW);
}else if(i > 17 && i <= 26){
squaresList.get(i).setBackground(Color.BLUE);
}else if(i > 26 && i <= 35){
squaresList.get(i).setBackground(Color.RED);
}else if(i > 35 && i <= 44){
squaresList.get(i).setBackground(Color.GREEN);
}else if(i > 44 && i <= 53){
squaresList.get(i).setBackground(Color.ORANGE);
}
}
}
if(e.getSource() == upLeftRight){
Collections.swap(squaresList, 0, 45);
Collections.swap(squaresList, 1, 46);
Collections.swap(squaresList, 2, 47);
Collections.swap(squaresList, 45, 18);
Collections.swap(squaresList, 46, 19);
Collections.swap(squaresList, 47, 20);
Collections.swap(squaresList, 18, 36);
Collections.swap(squaresList, 19, 37);
Collections.swap(squaresList, 20, 38);
// HERE IS WHERE I THINK THE PROBLEM IS...BUT I CANT SEE PAST THE PROBLEM
// I TRIED USING HASHMAP SO EACH SQUARE HAS A UNIQUE CODE FOR ITS COLOR BUT DIDNT WORK OUT...
for(int i = 0; i < squaresList.size(); i++){
if(i <= 8){
squaresList.get(i).setBackground(Color.WHITE);
}else if(i >= 9 && i <= 17){
squaresList.get(i).setBackground(Color.YELLOW);
}else if(i > 17 && i <= 26){
squaresList.get(i).setBackground(Color.BLUE);
}else if(i > 26 && i <= 35){
squaresList.get(i).setBackground(Color.RED);
}else if(i > 35 && i <= 44){
squaresList.get(i).setBackground(Color.GREEN);
}else if(i > 44 && i <= 53){
squaresList.get(i).setBackground(Color.ORANGE);
}
}
}
}