Как реализовать перетаскивание и перемещение фигур на шахматной доске в Java (repaint() не работает)? ⇐ JAVA
Как реализовать перетаскивание и перемещение фигур на шахматной доске в Java (repaint() не работает)?
I'm working on a chess project in Java using Java Swing for the graphical interface. I have a Board class that extends JPanel, where I draw the chessboard and the pieces, and I've implemented mouse interaction to be able to move the pieces on the board.
My problem is that I'm having trouble implementing drag-and-drop and proper piece movement on the board. I've tried using MouseMotionListener and MouseListener to detect drag-and-drop events, but it doesn't seem to work correctly because i don't see any moves.The coordinates are updating, but the image is not.
Here's the code I've tried:
public class Board extends JPanel implements MouseMotionListener, MouseListener { private final PieceRender pieceRender = new PieceRender(); private final int cellSize = 600 / 8; private final int initialX = 45; private final int initialY = 55; private Point movingPiece; private int pieceLocationIndex; public Board() { addMouseListener(this); addMouseMotionListener(this); } @Override protected void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; drawBoard(g2); drawLabel(g2); pieceRender.pieceByDefault(g2,initialX,initialY,cellSize); } private void drawBoard(Graphics2D g2) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { drawSquare(g2,2 * j, 2 * i, true); drawSquare(g2,1 + 2 * j, 1 + 2 * i, true); drawSquare(g2,1 + 2 * j, 2 * i, false); drawSquare(g2,2 * j, 1+ 2 * i, false); } } } private void drawSquare(Graphics2D g, int x, int y, boolean color){ g.setColor(color ? Color.WHITE : Color.darkGray); g.fillRect(initialX + x * cellSize, initialY + y * cellSize, cellSize, cellSize); } private void drawLabel(Graphics2D g2){ char[] litere = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}; char[] cifre = {'1', '2', '3', '4', '5', '6', '7', '8'}; Font font = new Font("default", Font.BOLD, 20); g2.setFont(font); var pozitionX = initialX; var pozitionY = initialY; for(int i = 0; i < litere.length; i++){ g2.drawChars(litere, i, 1, pozitionX + 30, pozitionY - 10); pozitionX += 75; } for(int i = 0; i < cifre.length; i++){ g2.drawChars(cifre, i, 1, pozitionX - 625, pozitionY + 45); pozitionY += 75; } } @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { var actualX = (e.getPoint().x - initialX) / cellSize; var actualY = (e.getPoint().y - initialY) / cellSize; for (int i = 0; i < pieceRender.piecesBox.size(); i++) { if (pieceRender.piecesBox.get(i).getCoordinates().getPos_X() == actualX && pieceRender.piecesBox.get(i).getCoordinates().getPos_Y() == actualY){ pieceLocationIndex = i; }else System.out.println("Nui"); } System.out.println("Coord: " + actualX); System.out.println("Coord: " + actualY); movingPiece = e.getPoint(); repaint(); } @Override public void mouseReleased(MouseEvent e) { var actualX = (e.getPoint().x - initialX) / cellSize; var actualY = (e.getPoint().y - initialY) / cellSize; pieceRender.piecesBox.get(pieceLocationIndex).getCoordinates().setPos_X(actualX); pieceRender.piecesBox.get(pieceLocationIndex).getCoordinates().setPos_Y(actualY); movingPiece = null; System.out.println("Coord " + pieceRender.piecesBox.get(pieceLocationIndex).getCoordinates().getPos_X()); System.out.println("Coord " + pieceRender.piecesBox.get(pieceLocationIndex).getCoordinates().getPos_Y()); repaint(); } @Override public void mouseDragged(MouseEvent e) { movingPiece = e.getPoint(); repaint(); } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseMoved(MouseEvent e) { } public static void main(String[] args) { Thread t1 = new Thread(() -> { Board board = new Board(); board.addMouseListener(board); board.addMouseMotionListener(board); JFrame frame = new JFrame("Chess Game"); frame.add(board); frame.setSize(800, 800); frame.setVisible(true); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }); t1.start(); } } And draw them with this method:
public void pieceByDefault (Graphics2D g2, int initialX, int initialY, int cellSize) { PieceFactory pieceFactory = new PieceFactory(); char[][] defaultPositions = { {'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'} }; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { char pieceType = defaultPositions[y][x]; if (pieceType != ' ') { ChessPiece piece = pieceFactory.create(pieceType, new Coordinates(x, y)); piecesBox.add(piece); drawPiece(g2, piece.getIndex(), piece .getCoordinates().getPos_X(), piece.getCoordinates() .getPos_Y(),initialX, initialY, cellSize); } } } } public void drawPiece(Graphics2D g2, int index, int initialX, int initialY,int primeX, int primeY, int cellSize){ drawImage(g2, index, initialX, initialY,primeX, primeY, cellSize); }
Источник: https://stackoverflow.com/questions/780 ... -java-repa
I'm working on a chess project in Java using Java Swing for the graphical interface. I have a Board class that extends JPanel, where I draw the chessboard and the pieces, and I've implemented mouse interaction to be able to move the pieces on the board.
My problem is that I'm having trouble implementing drag-and-drop and proper piece movement on the board. I've tried using MouseMotionListener and MouseListener to detect drag-and-drop events, but it doesn't seem to work correctly because i don't see any moves.The coordinates are updating, but the image is not.
Here's the code I've tried:
public class Board extends JPanel implements MouseMotionListener, MouseListener { private final PieceRender pieceRender = new PieceRender(); private final int cellSize = 600 / 8; private final int initialX = 45; private final int initialY = 55; private Point movingPiece; private int pieceLocationIndex; public Board() { addMouseListener(this); addMouseMotionListener(this); } @Override protected void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; drawBoard(g2); drawLabel(g2); pieceRender.pieceByDefault(g2,initialX,initialY,cellSize); } private void drawBoard(Graphics2D g2) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { drawSquare(g2,2 * j, 2 * i, true); drawSquare(g2,1 + 2 * j, 1 + 2 * i, true); drawSquare(g2,1 + 2 * j, 2 * i, false); drawSquare(g2,2 * j, 1+ 2 * i, false); } } } private void drawSquare(Graphics2D g, int x, int y, boolean color){ g.setColor(color ? Color.WHITE : Color.darkGray); g.fillRect(initialX + x * cellSize, initialY + y * cellSize, cellSize, cellSize); } private void drawLabel(Graphics2D g2){ char[] litere = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}; char[] cifre = {'1', '2', '3', '4', '5', '6', '7', '8'}; Font font = new Font("default", Font.BOLD, 20); g2.setFont(font); var pozitionX = initialX; var pozitionY = initialY; for(int i = 0; i < litere.length; i++){ g2.drawChars(litere, i, 1, pozitionX + 30, pozitionY - 10); pozitionX += 75; } for(int i = 0; i < cifre.length; i++){ g2.drawChars(cifre, i, 1, pozitionX - 625, pozitionY + 45); pozitionY += 75; } } @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { var actualX = (e.getPoint().x - initialX) / cellSize; var actualY = (e.getPoint().y - initialY) / cellSize; for (int i = 0; i < pieceRender.piecesBox.size(); i++) { if (pieceRender.piecesBox.get(i).getCoordinates().getPos_X() == actualX && pieceRender.piecesBox.get(i).getCoordinates().getPos_Y() == actualY){ pieceLocationIndex = i; }else System.out.println("Nui"); } System.out.println("Coord: " + actualX); System.out.println("Coord: " + actualY); movingPiece = e.getPoint(); repaint(); } @Override public void mouseReleased(MouseEvent e) { var actualX = (e.getPoint().x - initialX) / cellSize; var actualY = (e.getPoint().y - initialY) / cellSize; pieceRender.piecesBox.get(pieceLocationIndex).getCoordinates().setPos_X(actualX); pieceRender.piecesBox.get(pieceLocationIndex).getCoordinates().setPos_Y(actualY); movingPiece = null; System.out.println("Coord " + pieceRender.piecesBox.get(pieceLocationIndex).getCoordinates().getPos_X()); System.out.println("Coord " + pieceRender.piecesBox.get(pieceLocationIndex).getCoordinates().getPos_Y()); repaint(); } @Override public void mouseDragged(MouseEvent e) { movingPiece = e.getPoint(); repaint(); } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseMoved(MouseEvent e) { } public static void main(String[] args) { Thread t1 = new Thread(() -> { Board board = new Board(); board.addMouseListener(board); board.addMouseMotionListener(board); JFrame frame = new JFrame("Chess Game"); frame.add(board); frame.setSize(800, 800); frame.setVisible(true); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }); t1.start(); } } And draw them with this method:
public void pieceByDefault (Graphics2D g2, int initialX, int initialY, int cellSize) { PieceFactory pieceFactory = new PieceFactory(); char[][] defaultPositions = { {'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'} }; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { char pieceType = defaultPositions[y][x]; if (pieceType != ' ') { ChessPiece piece = pieceFactory.create(pieceType, new Coordinates(x, y)); piecesBox.add(piece); drawPiece(g2, piece.getIndex(), piece .getCoordinates().getPos_X(), piece.getCoordinates() .getPos_Y(),initialX, initialY, cellSize); } } } } public void drawPiece(Graphics2D g2, int index, int initialX, int initialY,int primeX, int primeY, int cellSize){ drawImage(g2, index, initialX, initialY,primeX, primeY, cellSize); }
Источник: https://stackoverflow.com/questions/780 ... -java-repa
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение