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

javax.swing.JPanel[,300,0,75x75,invalid,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

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

Recommended Answers

All 3 Replies

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.

commented: Ive never Extended something before. Well ive tried and failed. How would I go about it? +0

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.