Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  simple boardgames  (Read 1626 times)
0 Members and 1 Guest are viewing this topic.
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Posted 2004-12-28 17:44:50 »

I was thinking about doing some simple boardgames. Unfortunately, most of the tutorials and books out there handle only arcade/applets .. not my type :)
(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
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2004-12-28 18:28:48 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
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. :-)

Java Game Console Project
Last Journal Entry: 12/17/04
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #2 - Posted 2004-12-29 03:19:28 »

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

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline digitprop

Junior Member





« Reply #3 - Posted 2005-01-03 09:18:00 »

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.

M. Fischer . www.digitprop.com
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #4 - Posted 2005-01-03 10:43:08 »

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?

Java Game Console Project
Last Journal Entry: 12/17/04
Offline digitprop

Junior Member





« Reply #5 - Posted 2005-01-03 13:06:13 »

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?


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.

M. Fischer . www.digitprop.com
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #6 - Posted 2005-01-03 13:19:51 »

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. :-)

Java Game Console Project
Last Journal Entry: 12/17/04
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #7 - Posted 2005-01-04 08:45:28 »

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
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #8 - Posted 2005-01-05 20:11:31 »

Quote
I found a thread somewhere with a nice example.  


Mind posting the link.  I would like to give it a read. Thanks
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #9 - Posted 2005-01-06 02:12:00 »

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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!