For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2006 > Basic Java games









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Basic Java games
Colin Hemmings

2006-02-17, 7:57 am

Hi there,
I'm looking at make several two player boards games (such as chess,
daughts etc) in Java. Ive done a lot of the underlying code and I'm now
looking at adding a GUI to the game.
The problem is I'm not sure the best way of going about creating
the board. I was thinking of having a grid of images or sprites, some
will show empty square and some will have counters on them. I was then
thinking of adding onclick handlers to the images to take in the users move.

Is this a good way of going about it? because I'm a fairly inexperience
Java programmer so I'm looking for some advice from you wise people.


Thanks for your help


Colin H
VisionSet

2006-02-17, 7:00 pm


"Colin Hemmings" <colin.hemmings1@ntlworld.com> wrote in message
news:UajJf.9235$gB4.2950@newsfe4-gui.ntli.net...
> Hi there,
> I'm looking at make several two player boards games (such as chess,
> daughts etc) in Java. Ive done a lot of the underlying code and I'm now
> looking at adding a GUI to the game.
> The problem is I'm not sure the best way of going about creating
> the board. I was thinking of having a grid of images or sprites, some
> will show empty square and some will have counters on them. I was then
> thinking of adding onclick handlers to the images to take in the users

move.
>
> Is this a good way of going about it? because I'm a fairly inexperience
> Java programmer so I'm looking for some advice from you wise people.
>


Create a component with a null layout for the board.
a JLabel is good for this (new MyJLabel(myBoardImageIcon))
use absolute positioning and paint on all your pieces etc with a inheritence
hierachy of renderers. eg PieceRenderer, HintRenderer etc pass each renderer
the Graphics object which you can forward from you JLabel subclass eg

paintComponent(Graphics g) {

renderer1.paint((Graphics2d)g);
renderer2.paint((Graphics2d)g);
renderer3.paint((Graphics2d)g);
}

This obviously gives control over z-order

Yes you can paint the images on in your renderers.
Your AbstractRenderer super class could hold all the coordinate/scaling data
eg for ignoring any board border, or compensating for gridline widths.

Good use of null layout, all other components next to the board will have
regular layout managers and all be contained by a layout managed component.

Don't use components for pieces, you'll find ultimately you have less
flexibility.

--
Mike W



Colin Hemmings

2006-02-17, 7:00 pm

Ok thankyou for that although I'm not sure I understand most of it, much
research is needed i think


VisionSet wrote:
> "Colin Hemmings" <colin.hemmings1@ntlworld.com> wrote in message
> news:UajJf.9235$gB4.2950@newsfe4-gui.ntli.net...
>
>
> move.
>
>
>
> Create a component with a null layout for the board.
> a JLabel is good for this (new MyJLabel(myBoardImageIcon))
> use absolute positioning and paint on all your pieces etc with a inheritence
> hierachy of renderers. eg PieceRenderer, HintRenderer etc pass each renderer
> the Graphics object which you can forward from you JLabel subclass eg
>
> paintComponent(Graphics g) {
>
> renderer1.paint((Graphics2d)g);
> renderer2.paint((Graphics2d)g);
> renderer3.paint((Graphics2d)g);
> }
>
> This obviously gives control over z-order
>
> Yes you can paint the images on in your renderers.
> Your AbstractRenderer super class could hold all the coordinate/scaling data
> eg for ignoring any board border, or compensating for gridline widths.
>
> Good use of null layout, all other components next to the board will have
> regular layout managers and all be contained by a layout managed component.
>
> Don't use components for pieces, you'll find ultimately you have less
> flexibility.
>
> --
> Mike W
>
>
>

zero

2006-02-17, 7:00 pm

Colin Hemmings <colin.hemmings1@ntlworld.com> wrote in
news:UajJf.9235$gB4.2950@newsfe4-gui.ntli.net:

> Hi there,
> I'm looking at make several two player boards games (such as
> chess,
> daughts etc) in Java. Ive done a lot of the underlying code and I'm
> now looking at adding a GUI to the game.
> The problem is I'm not sure the best way of going about creating
> the board. I was thinking of having a grid of images or sprites, some
> will show empty square and some will have counters on them. I was then
> thinking of adding onclick handlers to the images to take in the users
> move.
>
> Is this a good way of going about it? because I'm a fairly
> inexperience Java programmer so I'm looking for some advice from you
> wise people.
>
>
> Thanks for your help
>
>
> Colin H
>


A simple way - or at least doable for an inexperienced programmer -
would be to extend JPanel. Override its paintComponent method to paint
the board, and then paint each piece on top of that. Add a mouse
listener to the JPanel that translates click and drop points to board
coordinates.

Some things depend on how you represented the board, but here's an
outline that you could follow:

public class BoardPanel extends JPanel()
{
private BoardPosition thePosition;
private ImageIcon board;

public BoardPanel(BoardPosition pos, ImageIcon boardIcon)
{
thePosition = pos;
board = boardIcon;
}

public void paintComponent(Graphics g)
{
// paint the background
boardIcon.paintIcon(this, g, 0, 0);

// instead of using magic number 8, consider
// thePosition.getBoardWidth()
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
// if there is a piece, paint it
if(thePosition.getPiece(i, j) != null)
thePosition.getPiece(i, j).paintIcon(this, g,
SQUARESIZE*i, SQUARESIZE*j);
}
}
}

// override getPreferredSize and related methods
}
Roedy Green

2006-02-17, 7:00 pm

On Fri, 17 Feb 2006 12:19:00 GMT, Colin Hemmings
<colin.hemmings1@ntlworld.com> wrote, quoted or indirectly quoted
someone who said :

>Is this a good way of going about it? because I'm a fairly inexperience
>Java programmer so I'm looking for some advice from you wise people.


I think you will find this easiest with a Canvas or Painting with
paintComponent on a JPanel. See
http://mindprod.com/jgloss/canvas.html

You just draw your piece giving the x,y co-ordinates of the grid
square they live in. You field mouse clicks yourself.
you compute

col = x/colWidthInPixels;
row = y/rowWidthInPixels;

It will me much faster than arrays of components and much simpler to
control.
For speed, (hardly a concern for chess) you look at the clip region
passed to paint/paintComponent and convert that to a row/column range
to render with simple subtraction and division.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Thibaut

2006-02-20, 7:57 am


"Roedy Green" <my_email_is_posted_on_my_website@munged.invalid> a écrit dans
le message de news:a5qcv1h9fgclfgpikselfuk91a2e0es9j7@
4ax.com...
> On Fri, 17 Feb 2006 12:19:00 GMT, Colin Hemmings
> <colin.hemmings1@ntlworld.com> wrote, quoted or indirectly quoted
> someone who said :
>
>
> I think you will find this easiest with a Canvas or Painting with
> paintComponent on a JPanel. See
> http://mindprod.com/jgloss/canvas.html
>
> You just draw your piece giving the x,y co-ordinates of the grid
> square they live in. You field mouse clicks yourself.
> you compute
>
> col = x/colWidthInPixels;
> row = y/rowWidthInPixels;
>
> It will me much faster than arrays of components and much simpler to
> control.
> For speed, (hardly a concern for chess) you look at the clip region
> passed to paint/paintComponent and convert that to a row/column range
> to render with simple subtraction and division.
>
> --
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.


My question may sound stupid, but why nobody told about the JLayeredPane ?
I've already had to make a boardgame in Java and this is what i used. Can
you tell me why it was a bad idea please ?

Thanks

Thibaut


Roedy Green

2006-02-21, 7:03 pm

On Mon, 20 Feb 2006 09:25:13 +0100, "Thibaut"
<thibaut.desmarest@agenwithoutspamdize.com> wrote, quoted or
indirectly quoted someone who said :

>My question may sound stupid, but why nobody told about the JLayeredPane ?
>I've already had to make a boardgame in Java and this is what i used. Can
>you tell me why it was a bad idea please ?


IIRC it is a relatively new feature, reminiscent of the sprites of
old, to deal the problem of 3D layers without 3D rendering. Board
games such as Go and chess without anything moving or fancy
backgrounds, would not need its complications. It is for when you
have objects moving about over a background.

If you want something basic, that will run even on old Javas, you can
use a Canvas.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com