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);
}
}
}
}
From the codes you've provided, I assume that the squareList contains squares but not colour values. The problem with your approach is that you are moving around the squares values using Collection.swap()
but you are not assigning colour values based on the original values, which means that the colours will not be swapped when squares are swapped.
Consider the following example. Suppose number represents each of your square.
Original square list: {1(Black), 2(Blue), 3(Green), 4(Red)}
And you are assingning in order of Black-Blue-Green-Red to this list. The result of this will be the colour is in the following order:
Original colour list: {Black, Blue, Green, Red}
On the other hand, after you perform swap:
After swap, the square list: {3, 1, 2, 4}
But you are giving colours in the same order, which would be again:
After swap, colour list: {Black, Blue, Green, Red}
So your sq 3 has Black, 1 has Blue, etc, and it looks like colours are not chaning. I suggest two ways of dealing with this: one is to keep a separate colour list in your data and swap it together when you are swapping squares. And assign colours using the order from the colour list.
Another solution is to make a data structure for each Square, which would have colour value in it. The codes will look like this:
class Square
{
Color myColour;
// ...
}
With this structure, when you perform swap you are doing:
Original square list: {1(Black), 2(Blue), 3(Green), 4(Red)}
After swap, square list: {3(Green), 2(Blue), 1(Black), 4(Red)}
I hope this helps.