0

I'm making a game which has pieces which I can drag onto a board, right now I have made each of the individual pieces a JComponent, and I'm writing the class which allows me to drag the pieces(PieceComponent) onto the Board. This is my paintComponent method for my board:

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    int imageSize = pieceComponents.get(0).getHeight();
    int boardBuffer = (getWidth()-3*imageSize)/2;
    for(int i=0; i<puzzle.getCols(); i++) {
        for(int j=0; j<puzzle.getRows(); j++) {
            g2d.drawRect(boardBuffer+i*imageSize, j*imageSize, imageSize, imageSize);
        }
    }
}

The rectangles are my board, but I'm not sure how to draw the Pieces onto the screen, I can't have them in a different panel because I can't get pieces to be dragged across panels.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
Mike
  • 87
  • 10
  • Don't, use an appropriate layout manager instead – MadProgrammer Apr 03 '16 at 04:21
  • Drag'n'drop is a question of do you want to move the component or the data the components represents – MadProgrammer Apr 03 '16 at 04:26
  • @MadProgrammer I want to move both the component and the data the component represents, this is a puzzle, I need both parts for this to work. I tried to look at what you suggested and had no luck. What layout manager should do you suggest? My design is have the board on top and the pieces in a line on the bottom. – Mike Apr 03 '16 at 04:36
  • `GridLayout` or `GridBagLayout` – MadProgrammer Apr 03 '16 at 04:37
  • Drag and drop becomes a little complex, for [example](http://stackoverflow.com/questions/11201734/java-how-to-drag-and-drop-jpanel-with-its-components/11443501#11443501) and this [example](http://stackoverflow.com/questions/28844574/drag-and-drop-from-jbutton-to-jcomponent-in-java/28844969#28844969) shows how you could transfer data between the components – MadProgrammer Apr 03 '16 at 04:41

0 Answers0