• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Chess?  RSS feed

 
Per Kristian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing a chess game and i need some help about how to put out/showing the piesces this is what the code is so far
 
Per Kristian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okey i found out how the pictures come up but i need to move them around the board


 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be more specific about how you want to move them? Do you want to use drag-and-drop or simple clicks? Writing a chess game is lots of fun, and people here will gladly jump in to help, but you need to provide some details on exactly what you need help doing.
 
Per Kristian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, thx for the reply... i would like to move them by clikcing on them once and then clicking on the place it shall go to. it was fun getting all the black and white on the board now its hard work , i dont need to follow all the ruls like in real chess.... but the main thing is that i can move them around... and btw are there places where i can get nice pics for my Pawn and queens :?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to try the Swing forum to catch the experts, but I can tell you that you can use the java.awt.event.MouseListener interface for your needs. You would create a class that implements it and responds to mouseClicked() events. I believe if you add the listener to the JPanel that contains your game board, it will pass on events from the individual components.

Others may be able to give you a simpler way to do this. Perhaps JPanel itself already has limited functionality for this, or you could use AbstractButton somehow and get ActionEvents instead.

Now would be a good time to go through a tutorial on Swing's event model.
 
Per Kristian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm okey that was a good answer.. now its up to me to understand it
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by skimming the JavaDocs for those classes. Again, I highly recommend going through Sun's various Swing tutorials. They're pretty short, and free so you can't really go wrong. I'm sure there are a few other free tutorials out there, too. Give IBM's DeveloperWorks a shot (I liked the NIO tutorial it had).

Briefly, the listener pattern in Swing (it's a common Software Pattern) works by registering event listeners with components. You attach an ActionListener to a JButton. When Swing detects that a button has been pressed and released in such a way that it should be activated, your listener's actionPerformed() method is called with an ActionEvent containing the relavent information (mouse modifiers, the event source, etc).

This way you don't extend JButton to have it take actions when pressed. Instead you simply register a listener (any class that implements ActionListener). The Swing forum should have plenty of examples of this, but definitely hit up some tutorials.
 
Per Kristian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again, yes i hit them up but all i managed was to rewrite my chessboard...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any luck figuring out how to use the MouseListener interface?
 
Per Kristian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still no all i know is that i have to use it in the Figur class where i use JButton....
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Per,

Years ago, in 1996, I wrote my 2nd Java program which was a chess position display applet which can be seen in action here or downloaded from here. This applet uses simple graphics primitives to display directly to the applet's rendering area. The problem with this code is that there is no separation of the model of chessboard position and its rendering . This makes it difficult to transform it into something interactive rather than a static rendering of the position.

Probably the first thing for you to consider is to separate out the model of the position from all the user interface code.

At any rate, by separating out the Position model from the ui model, you will be well on your way to making something interactive, modeling a chess Game that holds and updates a Position model based upon Moves that are selected.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!