Frage

Ok, so I have an if statement checking whether the JPanel selected is a certain color, line 2 prints false, line 4 and 5 print out the color of the panel selected and the color i'm checking against - which are exactly the same - red, line 14 gets printed, line 15 and 17 don't. The strange thing is I know the code works, this bug only happens when I include a connection using objectoutputStreams which have an instance of this class. The objectoutputstream includes an array of colors that are then displayed, maybe the colors are affected by serialisation ? but they are printed out the same in lines 4 and 5.

public void mouseClicked(MouseEvent e){ 
        System.out.println(squareSelected);             line2
        JPanel currentPanel = (JPanel) e.getSource();
        System.out.println(whosTurn[0]);                  line 4
        System.out.println(currentPanel.getBackground());  line 5
        if(squareSelected){
            for(int i = 0; i < 64; i++){
                if(squares[i] == currentPanel){
                    currentSquarePosition = i;
                    i = 63;
                }
            }
        }
        if(!squareSelected)System.out.println("y");
        if(currentPanel.getBackground() == Color.RED)System.out.println("d");   
        if(!squareSelected && (currentPanel.getBackground() == whosTurn[0] ||  line 16 currentPanel.getBackground() == whosTurn[1])){     // line 16
            System.out.println("1");
War es hilfreich?

Lösung

Try the equals method.

if(currentPanel.getBackground().equals(Color.RED))System.out.println("d");   
if(!squareSelected && (currentPanel.getBackground().equals(whosTurn[0]) ||  currentPanel.getBackground().equals(whosTurn[1]))){     // line 16
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top