for a school project, I am making a chessboard in java.
there were certain limitations, being that we can't use images for any of the pawns. We needed to make a pawn out of multiple shapes.
For example I have a pawn made out of a circle and a rounded square. here are some pieces of the code. This is the board defined as a set of chars, each represent a check on the board
private char[][] board = new char[][] { { 'T', 'H', 'B', 'Q', 'K', 'B', 'H', 'T' },
{ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' },
{ 'T', 'H', 'B', 'Q', 'K', 'B', 'H', 'T' } };
and here is the content of the method that creates a normal pawn
for(int i=0; i<8; i++) {
for(int j=0; j<8;j++) {
if(board[j][i] == 'P') {
Ellipse2D.Double ellipse = new Ellipse2D.Double(i * getWidth() / 8 + 20,
j * getHeight() / 8 + 20,
getWidth()/8 - 40,getHeight()/8 - 40);
g2d.setPaint(new GradientPaint(i * getWidth() / 8 , j * getHeight() / 8 + 20, Color.orange, i * getWidth() / 8, j * getHeight() / 8 + 60,
Color.pink, false) );
g2d.fill(ellipse);
RoundRectangle2D.Double roundRect = new RoundRectangle2D.Double(i*getWidth() / 8 + 20,
j*getHeight() / 8 + 10,
getWidth()/8 - 40, getHeight()/8-70,5,5);
g2d.setPaint(new GradientPaint(i * getWidth() / 8 , j * getHeight() / 8 + 20, new Color(20,20,150), i * getWidth() / 8, j * getHeight() / 8 + 60,
new Color(20, 20, 100), false) );
g2d.fill(roundRect);
}
}
}
probably not the cleanest code to do it, if there are any suggestions to do that better, please do suggest!
Now, the real problem, and my question is though, we must be able to drag and drop these multiple shapes at once onto another place on the board, and I honestly have no idea at all how to go over that.
Any help you guys can give me will be very much appreciated!
Thanks in advance!