Dear friends,
I currently am working on programming a chess game. Nothing difficult, just a board with pieces that can be moved around. I got the first part working (draw board with pieces), but I am having problems with writing the methods for the pieces to be moved (MousePressed, mouseDragged, mouseReleased). Could any of you please help me? I am still a beginner, so lots of help would be appreciated. Thank you in advance.
P.S. I know this sounds horrible, but if the answer could contain a finished version of this code that would be heavens... but maybe thats really me asking too much already (i just learn better that way)
import javax.swing.*; import java.awt.*; import java.awt.Color; import java.awt.Font; import java.awt.event.*; import javax.swing.JLabel; import javax.swing.SwingConstants; public class Chess2 extends JFrame implements MouseListener, MouseMotionListener { JFrame frame; JLayeredPane chessPane; JLabel chessPiece; JPanel chessBoard; JPanel squares[][] = new JPanel[8][8]; int xCoordinate; int yCoordinate; public Chess2() { frame = new JFrame("Chess2"); frame.setSize(600, 600); frame.setLayout(new GridLayout(8, 8)); chessPane = new JLayeredPane(); getContentPane().add(chessPane); chessPane.addMouseListener(this); chessPane.addMouseMotionListener(this); //drawing the board for (int a = 0; a < 8; a++) { for (int b = 0; b < 8; b++) { squares[a][b] = new JPanel(); if ((a + b) % 2 == 0) { squares[a][b].setBackground(new Color(238, 221, 187)); } else { squares[a][b].setBackground(new Color(204,136,68)); } frame.add(squares[a][b]); } } //first row top squares[0][0].add(new JLabel(new ImageIcon("Rooka8.png"))); squares[0][1].add(new JLabel(new ImageIcon("Knightb8.png"))); squares[0][2].add(new JLabel(new ImageIcon("Bishopc8.png"))); squares[0][3].add(new JLabel(new ImageIcon("Queend8.png"))); squares[0][4].add(new JLabel(new ImageIcon("Kinge8.png"))); squares[0][5].add(new JLabel(new ImageIcon("Bishopf8.png"))); squares[0][6].add(new JLabel(new ImageIcon("Knightg8.png"))); squares[0][7].add(new JLabel(new ImageIcon("Rookh8.png"))); //second row top a for (int x = 0; x < 8; x++) { squares[1][x].add(new JLabel(new ImageIcon("Pawna7.png"))); } //third row bottom for (int x = 0; x < 8; x++) { squares[6][x].add(new JLabel(new ImageIcon("Pawna2.png"))); } //fourth row bottom squares[7][0].add(new JLabel(new ImageIcon("Rooka1.png"))); squares[7][1].add(new JLabel(new ImageIcon("Knightb1.png"))); squares[7][2].add(new JLabel(new ImageIcon("Bishopc1.png"))); squares[7][3].add(new JLabel(new ImageIcon("Queend1.png"))); squares[7][4].add(new JLabel(new ImageIcon("Kinge1.png"))); squares[7][5].add(new JLabel(new ImageIcon("Bishopf1.png"))); squares[7][6].add(new JLabel(new ImageIcon("Knightg1.png"))); squares[7][7].add(new JLabel(new ImageIcon("Rookh1.png"))); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void mousePressed(MouseEvent event) { Component component = frame.findComponentAt(event.getX(), event.getY()); if (component instanceof JPanel) return; Point parentLocation = component.getParent().getLocation(); xCoordinate = parentLocation.x - event.getX(); yCoordinate = parentLocation.y - event.getY(); chessPiece = (JLabel)component; chessPiece.setLocation(event.getX() + xCoordinate, event.getY() + yCoordinate); chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight()); chessPane.add(chessPiece, JLayeredPane.DRAG_LAYER); } //move pieces public void mouseDragged(MouseEvent me) { if (chessPiece == null) return; chessPiece.setLocation(me.getX() + xCoordinate, me.getY() + yCoordinate); } //drop a piece when mouse is released public void mouseReleased(MouseEvent e) { if(chessPiece == null) return; chessPiece.setVisible(false); Component c = chessBoard.findComponentAt(e.getX(), e.getY()); if (c instanceof JLabel) { Container parent = c.getParent(); parent.remove(0); parent.add( chessPiece ); } else { Container parent = (Container)c; parent.add( chessPiece ); } chessPiece.setVisible(true); } public void mouseClicked(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public static void main(String[] args) { JFrame frame = new Chess2(); frame.setResizable(true); frame.setLocationRelativeTo(null); } }