March 4th, 2008, 10:36 AM
Creating a Checkers game using NetBeans
I'm currently studying Netbeans for an assignment, i have been given the task to create a multiplayer game that can be played across two mobiles. We have been given three options of different games we can create, i have chosen the easiest option of creating a 'Checkers' game
Unfortunately my Java skills are not to a high standard and for the past three days i've become stuck on the same problem.
I've created a board and have managed to create two different coloured checker pieces using 'sprites', my main concern at the moment is how to move on from having just one checker each to having three rows of checkers for each player. At the moment i can only think of creating multiple amounts of the checker pieces by having 24 sprites (bad practice?) and then moving on by coding the movement of the pieces. Is there another way of generating 24 pieces onto the board without having 24sprite images?
Many thanks for any feedback, if i'm in the wrong place i apologize.
March 4th, 2008, 02:52 PM
The good thing about Java, is that objects in Java behave a little like pointers in C++. For example:
This makes obj2 and obj1 handles to the same object (in other words, changing one will change the other).
SO what this means for you, is you can pass the handle to a single "sprite" on to multiple checkers.
For example, you could first create a Checker class:
Then, create a Checker Manager class, which loads the sprite once, then creates the individual Checkers and passes them a handle to the loaded sprite:
public class Checker
private BufferedImage mySprite;
// Constructor: gets passed a handle to the sprite:
public Checker( BufferedImage s )
// Point "mySprite" to the given handle:
mySprite = s;
Hope this helps!
public class CheckerManager
private BufferedImage redCheckerSprite;
private BufferedImage blackCheckerSprite;
private Checker redCheckers;
private Checker blackCheckers;
// Constructor: load the sprites, and create the checkers
String redCheckerFilename = "redchecker.jpg";
String blackCheckerFilename = "blackchecker.jpg";
// Use whatever way you want to load your sprites:
redCheckerSprite = ImageIO.read( getClass().getClassLoader().getResourceAsStream( redCheckerFilename ) );
blackCheckerSprite = ImageIO.read( getClass().getClassLoader().getResourceAsStream( blackCheckerFilename ) );
catch( IOException e )
System.err.println( "Error loading a sprite" );
// Create 12 of each type of checkers:
redCheckers = new Checker;
blackCheckers = new Checker;
for( int c = 0; c < 12; c++ )
// Create the individual checkers, each gets a handle to one of the two loaded sprites:
redCheckers[c] = new Checker( redCheckerSprite );
blackCheckers[c] = new Checker( blackCheckerSprite );