As the title says, I’m having trouble with my Java code when it comes to finally drawing the counters on my abacus. the code compiles and runs but the counters are drawn starting from the first row instead of the top row. On top of this the counters do not stack in each column, one counter appears and moves up and down the column depending on which button is clicked which is correct but the counters should either be adding as I left click or subtracting as I right click.
I’ve spent a couple of hours on this and I’m sure it’s something silly but my brain has stopped working and I cant think of any solutions.
Anyway, here is my code.
AbacusPanel.java
public void paint(Graphics g)
{
g.setColor(Color.gray);
g.fillRect(0,0,getWidth(), getHeight());
g.setColor(Color.black);
Graphics2D g2 = (Graphics2D)g;
// we'll use Graphics2D for it's "draw" method -
// neater than the Graphics "drawRect" suppled
// (which you could also use)
for (int i = 0;i<numCols;i++)
{
for(int j = 0;j<numRows;j++)
{
g2.draw(getRect(i,j));
}
}
for(int thisCol= 0; thisCol < numCols; thisCol++)
{
for(int thisRow = 0; thisRow < numRows; thisRow++)
{
for(int counters=0; counters<=myAbacus.getNumCounters(thisCol); counters++)
{
Rectangle r2 = getRect(thisCol,myAbacus.getNumCounters(thisCol));
g2.setColor(Color.red);
g2.fillOval(r2.x, r2.y, r2.width, r2.height);
}
}
}
}
Hopefully someone out there can point me in the right direction and sorry if any of this isn’t formatted how you would like. This is my first question post and I have tried to make it easy on the eyes.
As per my comment:
When you draw the placeholders for stones, you iterate over columns and then rows. But when when draw the counters, you only iterate over columns and not rows. Why not rows also? And shouldn’t the result of getNumCounters() take both column & row as inputs, since you’re trying to get the number of counters per position on the mancala board, right?
In answer to your second question, I would have to believe that
getNumCounters()would have to take both the row and column as inputs. Since you’ve got two rows on the mancala board, and the number of stones is dependent upon both the column and whether it is in the first or second row, it wouldn’t make sense otherwise.Also, a third thing to think about. In your rectangle, when you’re drawing your counters, it seems would need to vary the position where they are — it appears to me that each of your counters is going to stack on top of each other, so 20 counters will look no different than 1.