Found this code in the internet, it was posted years ago, so I just decided to ask here for some clarifications for some lines I don\'t quite understand.
In the
In the
mousePiece
method, what does he mean by:chessPiece = null
is he saying that if theJLabel chessPiece
has a image in it then it should be changed tonull
?
I assume you mean mousePressed
. By using chessPiece = null
, the author is de-referencing the variable, so what ever was assigned to it is no longer reachable through the is variable
Is
chessBoard.findComponentAt(e.getX(), e.getY())
returns theJPanel
square?
This depends. findComponentAt
can search the current container and it's any of it's child containers until it finds a component at the specified position. Technquial, the author is ignoring the component that triggered the event (which should layeredPane
) and is walking the chessBoard
instead. I suspect they are doing this because if they used layeredPane
it would return chessBoard
instead.
The method is capable of returning JPanel
, JLabel
and possibly even null
, but given the way that the components are laid out, it's a lower probability.
and lastly, when Component c gets its parent, who is the parent?
This depends. Based on my understanding of the code, I would say it's return a JPanel
underneth the JLabel
piece.
Have to say, there are easier ways to achieve the same result though...