1

I am currently trying to make a scenario where every time I click a JPanel it changes a different color each time. For example I don't want to click a magenta square and get a magenta square again.

Color newColor = null;

Color[] colorArr = {Color.YELLOW, Color.MAGENTA, Color.BLACK,new Color (0x964B00),new Color (0xB57EDC)};
int ranNum = generator.nextInt(5);//generator is a .Random Object
switch (ranNum) {

    case 0:
    newColor = colorArr[0];
       break;

    case 1:
    newColor = colorArr[1];
       break;
    case 2:
    newColor = colorArr[2];
       break;
    case 3:
    newColor = colorArr[3];
       break;
    case 4:
    newColor = colorArr[4];
       break;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

you should define a global variable that holds the old color value.

int oldNum;
Color[] colorArr = {Color.YELLOW, Color.MAGENTA, Color.BLACK,new Color (0x964B00),new Color (0xB57EDC)};

    void changeColor(){
        Color newColor = null;
        int ranNum;
        do{
            ranNum = generator.nextInt(5);//generator is a .Random Object
        }while(oldNum == ranNum);
        oldNum = ranNum;
        newColor = color[ranNum];
    }
Emre Acar
  • 920
  • 9
  • 24