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.