aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes chess game : moving piece Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "chess game : moving piece" Watch "chess game : moving piece" New topic
Author

chess game : moving piece

mundva wala
Greenhorn

Joined: Jul 02, 2010
Posts: 6
i am try to make a simple 2player chess game...

problem:- when i did wrong move then it comes to its original position but then after i can't move it again...why this is happening??
also suggest some more efficient mechanism ....

]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I think I speak for many here by saying that I would much better understand your problem if I could see and play with compilable code. I recommend that you create and post the smallest program possible that is compilable, runnable, demonstrates your problem, and has no extraneous code not related to the problem, an SSCCE. Please see the link for details on how to create one of these because trust me, if your SSCCE is compliant with the specs, you'll likely get a very helpful answer from someone here quickly. For instance you would likely create a very simple board that has one piece on it, that relies on graphics that are available to all from the internet.

Much luck!
mundva wala
Greenhorn

Joined: Jul 02, 2010
Posts: 6
here is the full code of it....

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I don't know if there is one or a few best ways to do this, but I would have my jpanel squares grid held in a JLayeredPane as you're doing, but would add the chess piece to the square itself, not the layered pane. If the square uses GridBagLayout and you add the piece to it without constraints, it will be added to the center of the square. Then in the mouse listener, I'd obtain the square at the mouse pressed point, see if a piece was there, place it in the JLayeredPane's DRAG_LAYER, and move it with mouse dragged , and then on mouseRelease see if the mouse Point is over a square, check if it is a valid square and if so, add the piece again to the square. If not, I'd replace it on the original square.
mundva wala
Greenhorn

Joined: Jul 02, 2010
Posts: 6
thank you very much for giving reply
but i can't understand what you want 2 say....

if(valid)
then move
else
return to original position //can you write the code of this mechanism which is suitable for my coding???


if you can write the code for it then it will very helpful to me...

thank you
mudvawala
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I can show you my MouseAdapter code. It's not well written in that it is mixing GUI with logic, and its logic is only that of a black pawn but as a first approximation, it works for me:

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Cross posted
http://forums.sun.com/thread.jspa?threadID=5444230


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

mundva, BeForthrightWhenCrossPostingToOtherSites.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: chess game : moving piece