simple boardgames

I was thinking about doing some simple boardgames. Unfortunately, most of the tutorials and books out there handle only arcade/applets … not my type :slight_smile:
(if anyone know of resources that handle these kind of games I’ll be glad to read them).

Anyway… there’s a lot to think about. Should my board be intelligent for example, or should I simply dram some thing and catch the XY coordinates? I was thinking about getting oput there as simple as possible… so I wrote a board like this:

public ChessBoard()
{
super(“Schack Arne”);
setBounds(0,0,400,400);

         setLayout(new GridLayout(8,8));

          for (int i=0; i<64; i++)
          {
              JPanel square = new JPanel();

              if ((i+i/8)%2 == 0)
                  square.setBackground(Color.black);
              else
                  square.setBackground(Color.white);
            add(square);

and thought about doing the gamepieces like draggable JLabels… Do you have any thoughts about that, or any good resources for me to read? I’m really not into GUI programming at all, trying to add a simple JLabel with a picture made my Chessboard go whacky (the getcontentpane().add(Label) stuff I suppose…)

Happy New year to all of you

The reason why your board screws up when you add pieces is that you’re adding a 65th item to a 8x8 (64 item) grid. To accomplish what you’re trying to do, you’d need another panel over top of the chessboard panel.

Personally, I don’t think that using Swing components is the way to approach this issue. A much better design is to develop a custom Swing component that paints the board, finds pieces, and handles dragging. For example:


public interface Piece
{
  private static final int BLACK = 0;
  private static final int WHITE = 1;

  public Image getImage();
  public String getName();
  public int getColor();
}

public class Chess extends JComponent
{
  private Piece[][] board = new Piece[8][8];

  public Chess()
  {
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e)
      {
        int width = getWidth() / 8;
        int height = getHeight() / 8;

        Piece piece = board[e.getY()/height][e.getX()/width];

        if(piece != null)
        {
          //handle setting up the drag and drop. Check
          //the chess rules on dropping of the piece.
        }
      }
    });
  }

  public void paintComponent(Graphics g)
  {
    int width = getWidth() / 8;
    int height = getHeight() / 8;
    boolean white = true;

    for(int y=0; y<8; y++)
    {
      for(int x=0; x<8; x++)
      {
        g.setColor((white ? Color.WHITE : Color.BLACK));
        g.fillRect(x*width, y*height, width, height);
        white = !white;

        if(board[y][x] != null) 
        {
          g.drawImage(board[y][x].getImage(), x*width, y*height, null);
        }
      }
    }
  }
}

That’s about as far as I’m willing to go without a compiler, but that should give you a good start. :slight_smile:

Well that seems to be a nice start indeed, thank you very much! :smiley:

I’ll try that out, and… probably get back later. I feel I would need to work some on the UML part, cause there’s a lot to think about indeed.

But the only way to get there is … code, code, code!!

Regards

Although I am a strong advocate of customized components in Java games myself, I think in this case it is worthwile to consider the advantages of using Swing components.

If it’s a board game without the need of high frequency updates (e.g. more like 1fps than 60fps), then Swing’s inherent speed disadvantage would not weigh in. Also, Swing’s rather limited layout capabilities wouldn’t matter much, as the layout of a board game is usually quite simple and fits well into the ‘gridular’ thinking of Swing.

On the other hand, by using Swing one would be able to leverage the complete Swing functionality, plus standardization, plus easy customization. Also, Swing is quite structured (although the particular approach of Swing can of course be debated), and there is a lot of peer help available for Swing interfaces.

So for a beginner, Swing might be easier to handle than a completely custom-painted component, where the developer has to handle / sort out all object interaction, object representation, etc.

I think Jbanes’ code example is a good and natural approach, but if you have some experience with Swing already, using JPanels should work just as well. Just make sure that you do not add additional components to the board component, but to the individual tile JPanels.

Hi Digitprop,

I have to disagree with you on the use of Swing components. It’s not that I’m against Swing itself (you’ll note that I suggested a custom Swing component without a main loop), but rather that Swing’s UI model is not designed to handle the type of thing elwiz is trying to accomplish.

If he takes the approach of using standard Swing components, he’s going to run into layout manager problems, issues getting the events to the right component, z-ordering issues (the object being dragged should always be on top), and a host of other minor issues that are going to detract from the point of writing a chess game. Which is easier, writing a tiny bit of boilerplate code or debugging why Swing is acting like a GUI framework instead of a gaming framework?

I don’t want to enter a Swing vs. custom-made GUI discussion here, and your point is well taken. As you yourself, I would almost always do a game GUI on my own, rather than using Swing.

However, I think it is worthwile to consider available GUI frameworks such as Swing for some very specific games. Board games often require GUIs which are more similar to standard application GUIs than to ‘regular’ gaming GUIs. They often involve a predefined set of statically laid out components, and interaction is similar to standard GUI interaction (mouse clicks, drag and drop).

[quote]Which is easier, writing a tiny bit of boilerplate code or debugging why Swing is acting like a GUI framework instead of a gaming framework?
[/quote]
The first is certainly easier. My point, however, was that in some instances a game required a GUI framework, not a gaming framework in the first place.

Don’t get me wrong, I agree that standard Swing components make a lot of sense in certain circumstances. For example, I might not bother with a custom component if I was writing a “whack-a-mole” or “Monopoly” type of game. The problem here, however, is that the interaction with the board is probably beyond the point where the standard components make sense.

If he was just doing a “click on square to move, then click on square to land”, then he would probably be fine with standard Swing. But drag and drop (and other similar effects) add a whole new dimension to the UI that Swing is simply not designed to handle. :slight_smile:

Hello all!

This is a very interesting thread indeed.

I’ll admit, I’ve started with Swing components, I found a thread somewhere with a nice example. Me too, thought that using existing components would be an easier approach.

Well, I’ll see if it will, cause I’m not there yet. I’ve built my board and I can drag my pieces around but that’s it.
I do plan to try both approaches, as soon as I’m there with my Swing version I’ll try the painting approach.

Regards

[quote]I found a thread somewhere with a nice example.
[/quote]
Mind posting the link. I would like to give it a read. Thanks

It was somewhere on the Sun Forums, do a google on chessboard and jlayeredpane. Got this one for example which looks a little like mine.
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=518707