我想做一個基本的2D魔方3x3,但我遇到了方塊的顏色問題,問題出現在我開始混合導致顏色改變的位置時因此,我不知道如何解決它。我需要幫助來完成我的2D魔方
這是代碼。非常感謝您的幫助和時間。
@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);
}
}
}
}
200行代碼轉儲不是問題。 –
這個錯誤發生在哪裏?您也可以查看[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)以改進問題。歡迎來到SO! –
是的,抱歉,我編輯了這篇文章。當我宣佈廣場的顏色時,問題就出現了。僅此運動效果不錯,但混合後它會以不正確的方式改變顏色。謝謝你的鏈接。 –