-1

Game.java calls a function getLabel() present in board.java. When I am running Game.java, I am getting error "cannot find symbol method getLabel()" in the terminal. I am not able to correct it.

Game.java

import java.awt.*;
import javax.swing.*;
import javax.swing.JLabel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Game {

    private board b;
    private bishop bis1;
    private JLabel q;

    public static void main(String[] args) {
        Game f = new Game();
        f.start();
    }

    public void start() {
        b = new board();
        bis1 = new bishop();
        bis1.setLocation(0, 0);
        ImageIcon m = bis1.getImage();
        q = b.getLabel();

        q.addMouseListener(new Mouselist());
        b.squares[0][0].add(q);
    }

    class Mouselist implements MouseListener {

        public void mouseClicked(MouseEvent e) {
            //k.setIcon(null);
            b.squares[1][1].add(new JLabel(new ImageIcon("rook.png")));
        }

        // mouse entered the JLabel increment count and display it
        public void mouseEntered(MouseEvent e) {
            b.squares[1][1].add(new JLabel(new ImageIcon("rook.png")));
        }

        public void mouseExited(MouseEvent e) {
            b.squares[1][2].add(new JLabel(new ImageIcon("rook.png")));
        }

        // mouse was presssed (cliked and released)
        // increment counter and display it
        public void mousePressed(MouseEvent e) {
            b.squares[1][1].add(new JLabel(new ImageIcon("rook.png")));

        }

        public void mouseReleased(MouseEvent e) {
            b.squares[1][1].add(new JLabel(new ImageIcon("rook.png")));
        }
    }
}

board.java

import javax.swing.*;
import java.awt.*;
import javax.swing.JLabel;

public class board {

    public JFrame frame;
    public JPanel squares[][] = new JPanel[3][3];
    private JLabel sqk = new JLabel(new ImageIcon("knight.png"));

    public board() {
        frame = new JFrame("Simplified Chess");
        frame.setSize(1200, 800);
        frame.setLayout(new GridLayout(2, 3));

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                squares[i][j] = new JPanel();

                if ((i + j) % 2 == 0) {
                    squares[i][j].setBackground(Color.black);
                } else {
                    squares[i][j].setBackground(Color.white);
                }
                frame.add(squares[i][j]);
            }
        }

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

    public JLabel getLabel() {
        return sqk;
    }
}

What is possibly wrong here ?

rid
  • 61,078
  • 31
  • 152
  • 193
John Watson
  • 869
  • 3
  • 16
  • 32
  • 6
    Java coding conventions say that classes should start with an uppercase letter, so it should be `Board` instead of `board`. Probably will not help you with your problem, but as a general note. – nwinkler Mar 09 '12 at 11:34
  • 5
    Have you recompiled board.java? =) – mcfinnigan Mar 09 '12 at 11:35
  • there no such method with name `call` – mKorbel Mar 09 '12 at 11:35
  • See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Mar 09 '12 at 11:38
  • The code you've posted compiles (when the bits about bishop are commented out). – Jon Skeet Mar 09 '12 at 11:40
  • @mcfinnigan, you are correct. i deleted board.class and did javac Game.java. but does javac not complile all the classes ? – John Watson Mar 09 '12 at 11:48
  • no. Javac compiles only the source file specified on the command line, it does not compile dependant source files. This is why we use things like ant, maven or IDEs - they automate this kind of thing. – mcfinnigan Mar 09 '12 at 11:58

2 Answers2

3

Most probably you have just added the getLabel() method and haven't yet recompiled board.java

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
0

I took your code for both board.java and Game.java and loaded it into my IDE. I did not get an error on line 24 (q = b.getLabel()). (I did get some compile errors because you did not provide bishop.java, but that is irrellevant to your problem.)

Is line 24 where you are getting your error? If so, can you email me the 3 actual .java files.

By the way, although again it is irrelevant to your issue, as noted above, capitalizing class names (e.g. Board and Bishop) is very standard practice and I highly recommend you do so. It will make it easier for other Java programmers to read your code.

Howard Schutzman
  • 2,115
  • 1
  • 15
  • 8