0

I am creating a chess game, and I have now populated my graphic chessboard with all the pieces, now I need to use Mouselistner to move the pieces around. Before implementing the graphic version I created a 2D console version, that took in "player moves", so I do have all those methods, but I need to now use Mouselistener, I read up about the methods, however, do I need to implement mouselistener in each class?

I have 1 abstract Piece class as well as 7 subclasses (incl Dummy piece), and a ChessBoard class that populates all the pieces and provides methods for moving (from the console version..) so where do I put the mouselistener? In the Jcomponent extension, JFrame or ChessBoard class that contains the methods to populate the chessboard and moves?

user2809437
  • 500
  • 1
  • 6
  • 21
  • What are the pieces? Are they components or do you render them yourself? The logical location would be to place the mouse listener on the board, as it becomes more self contained... – MadProgrammer Nov 06 '13 at 01:28
  • MouseListener is an interface so you can implement it anywhere you want. Probably your most "outside" class. Whatever object can see all of the components and has access to all of the things you will be doing in the events. Probably what you should not do is put listeners inside the components themselves which will likely get messy. Where to put the listener(s) is really up to whatever makes the most sense in your context. – Radiodef Nov 06 '13 at 01:36
  • I'd look to store [images of the pieces](http://stackoverflow.com/a/19209651/418556), and display them in an [undecorated button](http://stackoverflow.com/a/10862262/418556). – Andrew Thompson Nov 06 '13 at 01:36
  • See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Nov 06 '13 at 03:05

2 Answers2

0

Sorry for such a simple answer, but all you need to do is add the mouselistener to your ChessBoard class. From there I'm assuming you can access the Piece subclass objects you've instantiated and call methods on them (i.e. mouseClicked, piece.pickUp()). If you code is arranged in such a way that you need to implement a mouse listener in many of your classes, consider the following:

addMouseListener( new MouseAdapter() {
    @Override
    public void mouseClicked( MouseEvent e ) {
          // Do something
    }
} );

http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseAdapter.html

Also, if it were me, I would transfer the methods for moving your Pieces to your Piece class, preferably at a higher level and then you don't have to rewrite the same code twice. Then in your game, whenever mouseReleased is invoked, call some method like attemptToMove(BoardPoint p) that will check if your piece's current position and the new position, p are within your piece's means of travel. A BoardPoint could be something you set up with x, y coordinates for your own board in an 8 X 8 style, like a 2-dimensional integer array.

michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
0

It depends somewhat on how you have your pieces implemented. If they are GUI objects themselves, such as buttons or panels, then putting the mouseListener on them will allow the Swing framework to figure out which one has been clicked on. If the pieces all extend a Piece class, then you can put a handler in that as long as the logic it needs to execute (such as moving a piece around) can be made the same for all pieces.

If, on the other hand, you are drawing graphic images on the board in your code, so there is no GUI component for Swing to detect being clicked, then it makes more sense to implement the mouseListener on the board. In this case, your code is going to have to figure out which square was clicked on, and whether it has a piece on it; after that the handling will be much like the previous case.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • I am drawing graphic images on a board. I implemented Mouselistener with the pressed & released methods. the press will get a sourceR=event.getX() and a sourceC=event.getY(), then the released will do the same, but I called these variables destinationRow and destinationCol, instance variables.I implemented a move(), that does the following piece[destinationRow][destinationCol]=pieces[sourceR][sourceCol]; then piece[sourceR][sourceCol]=new Dummy();<--this leaves a blank space...however when the program runs, and I press then release, it deletes the piece objects rather than moving them. – user2809437 Nov 07 '13 at 01:33
  • It is difficult to tell exactly what you're doing without the code, and it's tricky stuff. It is not enough, of course, to put the piece into the array -- you have to draw it onto that square. Assuming you have the code to draw the pieces in your paint() method, I *think* what you do is call repaint on the component (probably the frame, in your case) to tell the component to repaint itself. Whether you do it this way or another way, you need to cause the framework to redraw after your piece is in its new place. – arcy Nov 07 '13 at 03:26