gpt4 book ai didi

java - 在java.awt.AWTEventMulticaster.mouseExited(AWTEventMulticaster.java :309)

转载 作者:行者123 更新时间:2023-11-30 06:24:16 26 4
gpt4 key购买 nike

我已经发布了我的代码。我无法理解问题的根本原因在哪里。这是我一直在尝试制作的 java chess Engine 的 UI 代码。如果我幸运或速度快的话,该应用程序运行大约 4-5 次移动和 8-9 次移动。最终,当我移动鼠标时,我遇到了一系列无限的错误。我认为我的代码在某个地方正在创建 mouseListeners 的多个实例,我认为这是不应该发生的。请指导我该怎么做。

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class UserInterface extends JPanel implements MouseListener, MouseMotionListener {
static int mouseX, mouseY, newMouseX, newMouseY;
static int squareSize = 32;

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(new Color(120, 100, 45));
this.addMouseListener(this);
this.addMouseMotionListener(this);
for (int i = 0; i < 64; i += 2) {
g.setColor(new Color(255, 200, 100));
g.fillRect((i % 8 + (i / 8) % 2) * squareSize, (i / 8) * squareSize, squareSize, squareSize);
g.setColor(new Color(150, 50, 30));
g.fillRect(((i + 1) % 8 - ((i + 1) / 8) % 2) * squareSize, ((i + 1) / 8) * squareSize, squareSize, squareSize);
}
Image chessPiecesImage;
chessPiecesImage = new ImageIcon("ChessPieces.png").getImage();

for (int i = 0; i < 64; i++) {
int j = -1, k = -1;
switch (alphaBetaChess.chessBoard[i / 8][i % 8]) {
case "P":
j = 5;
k = 0;
break;
case "p":
j = 5;
k = 1;
break;
case "R":
j = 2;
k = 0;
break;
case "r":
j = 2;
k = 1;
break;
case "K":
j = 4;
k = 0;
break;
case "k":
j = 4;
k = 1;
break;
case "B":
j = 3;
k = 0;
break;
case "b":
j = 3;
k = 1;
break;
case "Q":
j = 1;
k = 0;
break;
case "q":
j = 1;
k = 1;
break;
case "A":
j = 0;
k = 0;
break;
case "a":
j = 0;
k = 1;
break;
}
if (j != -1 && k != -1) {
g.drawImage(chessPiecesImage, (i % 8) * squareSize, (i / 8) * squareSize, (i % 8 + 1) * squareSize, (i / 8 + 1) * squareSize, j * 64, k * 64, (j + 1) * 64, (k + 1) * 64, this);
}
}
repaint();
// g.setColor(Color.black);
// g.fillRect(x, y,500/8,500/8);
}

@Override
public void mouseMoved(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
if (e.getX() < 8 * squareSize && e.getY() < 8 * squareSize) {
//if inside the board
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
}

@Override
public void mouseReleased(MouseEvent e) {
if (e.getX() < 8 * squareSize && e.getY() < 8 * squareSize) {
//if inside the board
newMouseX = e.getX();
newMouseY = e.getY();
if (e.getButton() == MouseEvent.BUTTON1) {
String dragMove;
if (newMouseY / squareSize == 0 && mouseY / squareSize == 1 && "P".equals(alphaBetaChess.chessBoard[mouseY / squareSize][mouseX / squareSize])) {
//pawn promotion
dragMove = "" + mouseX / squareSize + newMouseX / squareSize + alphaBetaChess.chessBoard[newMouseY / squareSize][newMouseX / squareSize] + "QP";
} else {
//regular move
dragMove = "" + mouseY / squareSize + mouseX / squareSize + newMouseY / squareSize + newMouseX / squareSize + alphaBetaChess.chessBoard[newMouseY / squareSize][newMouseX / squareSize];
}
String userPosibilities = alphaBetaChess.possibleMoves();
if (userPosibilities.replaceAll(dragMove, "").length() < userPosibilities.length()) {
//if valid move
alphaBetaChess.makeMove(dragMove);
alphaBetaChess.flipBoard();
alphaBetaChess.makeMove(alphaBetaChess.alphaBeta(alphaBetaChess.globalDepth, 1000000, -1000000, "", 0));
alphaBetaChess.flipBoard();
repaint();
}
}
}
}

@Override
public void mouseClicked(MouseEvent e) {
}

@Override
public void mouseDragged(MouseEvent e) {
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}
}

最佳答案

添加鼠标监听器,并在构建面板的地方添加运动监听器。这样就不会多次创建运动监听器。

关于java - 在java.awt.AWTEventMulticaster.mouseExited(AWTEventMulticaster.java :309),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47515390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com