Im creating a Chess game. Im stuck at getting the pieces to move within the rules Aka bishop can only move diagonally.
I Create Panels - 64 of them within a lager panel then add JLabels - The pieces - into those panels

JLabel bb = new JLabel(new ImageIcon("bb.gif")); panel = (JPanel)chessBoard.getComponent(2); panel.add(bb);

Thats how I add the piece into the panel on the board but when I try this on Mouse event e :
Component squareNumber = chessBoard.findComponentAt(e.getX(), e.getY());

and then i Print squareNumber i get


How can I get a number 1-64 to print out?(aka how can i figure out which component that piece is in)

5 Years
Discussion Span
Last Post by NormR1

how can i figure out which component that piece is in

One way is to extend the component and add methods and class variables that can be used when the component is clicked on.

Edited by NormR1

Votes + Comments
Ive never Extended something before. Well ive tried and failed. How would I go about it?

How would I go About doing that:? Ive never done that before.


What about the JFrame class? That is often extended.
To extend a class:

class YourClass extends <THECOMPONENTCLASS> {
   <member variables>
   // methods
} // end class

In a listener, cast the event source to YourClass and you can call its methods

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.