I made a Java application for my Honours year project that used a lot of Java2D.
I used a BufferedImage to give me a canvas upon which I drew a grid. In the underlying class which used the BufferedImage I used formulae and fixed width/height constants to calucalte the x,y positions of a particular cell.
For example;
Say you had a 7x6 grid of squares, each with sides of 10 pixels (Just think squares but we'll draw circles inside them), then I would calculate the x/y positions as follows;
private static final int CELL_DIMENSION = 10;
// Get the top left position of the cell
// from a given (column, row) position,
// where 0 is 1st row/column
private int[] getCellPosition( int column, int row ) {
int xPosition = column * CELL_DIMENSION;
int yPosition = row * CELL_DIMENSION;
return new int[] { xPosition, yPosition};
}
So when the user drops the counter thing into a column, you store which rows in the column are already taken, then from that figure out which cell is to be filled in.
Call the above method using the caluclated column and row positions and it will return the pixel positions to draw from on the BufferedImage. Then just get the graphics context of the BufferedImage - myBufferedImage.getGraphics(); - and fill an oval at the returned (x,y) positions and using the fixed dimension constant.
Bare in mind the above example means all the circles will be touching so adjust the forumale in the method to suit your needs. And also store in a data structure that the current cell is now occupied.
Then just output the BufferedImage graphics context to your JPanel in the GUI
Hope this helps
=)