Search code examples
javaswinginstancescannot-find-symbol

Having difficulties in Java with class instances calling upon methods in different class instances


Pretty new to Java. Been studying/learning it for a few weeks now. One of our assignments is to make a Chess Board. At the moment I have a class called 'ChessBoard'. This is the GUI and has a method which creates the GUI and then also instantiates 64 instances of another class "ChessSquare" - the chess squares that make up the board. I also have a main method in a class called "ChessRun" which starts the whole thing off and instantiates the 'ChessBoard'.

My issues is that when one of the ChessSquare instances is clicked it needs to activate a method in ChessBoard. However, I keep getting 'cannot find symbol' when trying this. I understand it doesn't recognise the symbol because it was instantiated elsewhere, but how do I fix this? I have to keep this method inside ChessBoard.

Main Method:

    public class ChessRun{

      public static void main(String[] args){
        int i = 1;
        ChessBoard[] CB = new ChessBoard[2];
        CB[i] = new ChessBoard();
        CB[i].start();
      }

    }

ChessBoard Class:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;

    public class ChessBoard{

      int clickcount = 0;
      int firstsquare;
      int secondsquare;

      public void Chessboard(){
      }

      public void start(){
        int i = 0;
        ChessSquare[] cs = new ChessSquare[64]; 
        JFrame G = new JFrame();
        JPanel P = new JPanel();
        G.setContentPane(P);
        GridLayout grid = new GridLayout(8, 8);
        P.setLayout(grid);
        G.setTitle("Chess Board");
        G.setSize(360, 360);

        while (i < 64){
          cs[i] = new ChessSquare();
          cs[i].setsquarenumber(i);
          cs[i].setsize();
          cs[i].initialpiece(i);
          cs[i].setpiece();
          P.add(cs[i]);
          i++;
        }

        G.setVisible(true);
        G.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }

      public void beenPressed(int sq){
        if(clickcount == 0){
          clickcount++;
          firstsquare = sq;
        }
        else if(clickcount == 1){
          clickcount++;
          secondsquare = sq;
        } 
        else if(clickcount == 2){

      }

      }
    }

ChessSquare class (the error is in this class - the line that reads: CB1.beenPressed(squarenumber);)

    import javax.swing.*;
    import javax.swing.JButton;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;

    public class ChessSquare extends JButton implements ActionListener{

      private int squarenumber;
      private String piece = "emptysquare";

      ImageIcon emptysquare = new ImageIcon("EmptySquare.jpg");
      ImageIcon selectedsquare = new ImageIcon("SelectedSquare.jpg");
      ImageIcon pawn = new ImageIcon("Pawn.jpg");
      ImageIcon bishop = new ImageIcon("Bishop.jpg");
      ImageIcon rook = new ImageIcon("Rook.jpg");
      ImageIcon knight = new ImageIcon("Knight.jpg");
      ImageIcon king = new ImageIcon("King.jpg");
      ImageIcon queen = new ImageIcon("Queen.jpg");

      public ChessSquare(){
        addActionListener(this);
      }

      public void actionPerformed(ActionEvent e){
        System.out.println("Pressed");
        CB[1].beenPressed(squarenumber);
      }

      public void setsquarenumber(int i){
        this.squarenumber = i;
      }

      public void initialpiece(int i){
        if(47 < i && i < 56){
          piece = "pawn";
        }
        if(i == 56 || i == 63){
          piece = "rook";
        }
        if(i == 57 || i == 62){
          piece = "knight";
        }
        if(i == 58 || i == 61){
          piece = "bishop";
        }
        if(i == 60){
          piece = "king";
        }
        if(i == 59){
          piece = "queen";
        }
        }

      public void setsize(){
        this.setPreferredSize(new Dimension(44, 44));
      }

      public String getpiece(){
        return piece;
      }

      public void setpiece(String s){
          piece = s;
      }

      public void setpiece(){
        if(piece == "emptysquare"){
          this.setempty();
        }
        if(piece == "pawn"){
          this.setpawn();
        }
        if(piece == "rook"){
          this.setrook();
        }
        if(piece == "knight"){
          this.setknight();
        }
        if(piece == "bishop"){
          this.setbishop();
        }
        if(piece == "queen"){
          this.setqueen();
        }
        if(piece == "king"){
          this.setking();
        }
      }

      public void setempty(){
        this.setIcon(emptysquare);
      }
      public void setselected(){
        this.setIcon(selectedsquare);
      }
      public void setpawn(){
        this.setIcon(pawn);
      }
      public void setrook(){
        this.setIcon(rook);
      }
      public void setknight(){
        this.setIcon(knight);
      }
      public void setbishop(){
        this.setIcon(bishop);
      }
      public void setqueen(){
        this.setIcon(queen);
      }
      public void setking(){
        this.setIcon(king);
      }

    }

Solution

  • The CB array (which should be renamed chessBoards to comply with Java naming conventions) has not been declared within the ChessSquare class and so it is not visible there. You will want to pass a reference of the array into the other class so that it is visible.

    Also, don't compare Strings with == or !=. These check for reference equality -- if one object is the exact same as another reference object, and that's not what you're interested in. Use the equals(...) method instead.

    Myself, I'd have the chess board add the ActionListener to the square rather than adding it internal in the chess square class. That way the square wouldn't need the board's reference. I also would avoid extending JButton.