Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

chess game : moving piece

 
mundva wala
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the full code of it....

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cross posted
http://forums.sun.com/thread.jspa?threadID=5444230
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mundva, BeForthrightWhenCrossPostingToOtherSites.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic