3

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!

zeta
  • 1,113
  • 3
  • 15
  • 24

1 Answers1

2

I'd suggest not using an image but instead creating a few BufferedImages at the beginning of your program that creates images for your chess pieces. Then add them to ImageIcons (which can be used more than once), and add these to JLabels (which can't). So for instance the white side will have 8 JLabels for the 8 pawns, but each of these JLabels will use the same white pawn ImageIcon. Then simply add the JLabels JPanels that represents each cell on the chessboard. I'd give the cell JPanels a GridBagLayout so that the JLabels will be added to their center without any fuss.

For an example of this, please check out: does-adding-a-jlabel-to-a-jpanel-hide-the-jpanel

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I don't think you can drag-and-drop a JPanel. – toto2 Nov 06 '11 at 14:50
  • @toto: I agree, it's not available out of the box, but it can be added if extended, but you don't need to actually. Just use a JLayeredPane, and on click move the JLabel from the JPanel up into the drag layer of the JLayeredPane. See my link above for an example of this. – Hovercraft Full Of Eels Nov 06 '11 at 14:53
  • I ran your example. It's quite complete; you could have made the AI too while you were at it. – toto2 Nov 06 '11 at 15:11
  • absolutely amazing! I can't believe I didn't find that example before when looking for some chess examples in java 2D – zeta Nov 06 '11 at 15:16