gpt4 book ai didi

Java - 为什么 mouseClicked 方法在执行完成后不会永久重置变量?

转载 作者:行者123 更新时间:2023-12-02 10:00:46 33 4
gpt4 key购买 nike

我有一个单元格对象的二维数组,其中包含其“tileCode”的字符串变量,当我使用键盘设置 Activity 图 block 代码,然后单击二维数组中所需的单元格时,它应该更改该单元格的tileCode到当前 Activity 图 block 。但是,当我单击该图 block 后,它立即切换回之前的图 block 代码和图像。

默认情况下,每个图 block 都设置为一个地板。

public static JFrame frame = new JFrame("Tool");
public static Panel panel;

public Image wall, floor;

public int rows = 50;
public int cols = 50;
public int xCoor = 20;
public int yCoor = 20;

String activeKey = "f";
Image activeImage = floor;

//Arrays
Rectangle[][] recs = new Rectangle[rows][cols];
Cell[][] cells = new Cell[rows][cols];
public Set <Integer> keysDown = new HashSet<>();

public LevelDesignTool() {
loadImages();
panel = new Panel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1400, 1100);
frame.setFocusable(true);
frame.requestFocus();
frame.setVisible(true);
frame.getContentPane().add(panel);

KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(this);
addMouseListener(this);

myThread thread = new myThread();
thread.start();
}

class Panel extends JPanel {

public void paintComponent(Graphics g) {

Graphics2D g2D = (Graphics2D)g;

//Loading array of Tile Types
ArrayList<String> TileTypes = new ArrayList<>();
try {
Scanner TileFileScanner = new Scanner(new File("TileTypes"));

while(TileFileScanner.hasNext()) {
TileTypes.add(TileFileScanner.nextLine());
}

}
catch(FileNotFoundException e) {
System.err.println("Tile Type file not found.");
}


g.setFont(new Font("Italic",Font.PLAIN, 25));
g.drawString("Press Enter to save to file.", 1050, 50);

//Tile Type key on the rigt side of the screen
int y = 550;
for(int i = 0; i < TileTypes.size(); i++) {
g.drawString(TileTypes.get(i), 1050, y);
y += 30;
}

//Creating 2D array of Rectangles
xCoor = 20;
for(int r = 0; r < rows; r++) {
yCoor = 20;
for(int c = 0; c < cols; c++) {
recs[r][c] = new Rectangle(xCoor, yCoor, 18, 18);
yCoor += 19;
}
xCoor += 19;
}
//Creating 2D array of cells
for(int r = 0; r < rows; r++) {

for(int c = 0; c < cols; c++) {

cells[r][c] = new Cell(recs[r][c], "f", activeImage);
xCoor += 21;
}
xCoor = 0;
yCoor += 21;
}
//Drawing cells
for(int r = 0; r < rows; r++) {

for(int c = 0; c < cols; c++) {

g.fillRect((int)cells[r][c].r.getX(), (int)cells[r][c].r.getY(), (int)cells[r][c].r.getWidth(), (int)cells[r][c].r.getHeight());
g.setColor(Color.BLACK);
}
}
//Drawing the images stored in each cell object
for(int r = 0; r < rows; r++) {

for(int c = 0; c < cols; c++) {

cells[r][c].image = floor;
g.drawImage(cells[r][c].getImage(), cells[r][c].getX(), cells[r][c].getY(), this);
}
}


addMouseListener(new MouseAdapter() {

@Override
public void mouseReleased(MouseEvent e) {

for(int r = 0; r < rows; r++) {

for(int c = 0; c < cols; c++) {

if (cells[r][c].getRect().contains(e.getX(), e.getY())) {
cells[r][c].tileCode = activeKey;
cells[r][c].image = activeImage;
//System.out.println("click registered");
}
}
}
}
});
}
}

//Must have these implemeted because they are from an interface
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}


public void update() throws Exception {

//System.out.println(cells[0][0].getTileCode());
//System.out.println(activeKey);
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

if(event.getID() == KeyEvent.KEY_PRESSED){
keysDown.add(event.getKeyCode());
if(event.getKeyCode() == KeyEvent.VK_F) {
activeKey = "f";
activeImage = floor;
}
if(event.getKeyCode() == KeyEvent.VK_E) {
activeKey = "e";
}
if(event.getKeyCode() == KeyEvent.VK_W) {
activeKey = "w";
activeImage = wall;
}
}
if(event.getID() == KeyEvent.KEY_RELEASED)
keysDown.remove(event.getKeyCode());

return false;
}

class myThread extends Thread {
public void run() {
while(true) {
try {
Thread.sleep(15);
frame.repaint();
update(); //update any game state changes for the next frame
}
catch(Exception e) {
System.out.println(e);
}
}
}
}

private void loadImages() {
floor = new ImageIcon("images/Floor.png").getImage();
wall = new ImageIcon("images/Wall.png").getImage();
}

//Runs code
public static void main(String[] args) {
new LevelDesignTool();
}

}

单击某个单元格后,其图像应更改为 Activity 图像,并且图 block 代码应更改为 Activity 图 block 代码。

最佳答案

问题是您在每次调用paintComponent时都初始化数据(并添加新的mouseListener)。每次重新绘制时,都会将所有数据重置为“floor”,这会覆盖您在鼠标监听器中所做的任何更改。另外,请注意,在“绘制单元格”循环中,您在绘制图像之前将图像重置为地板 (cells[r][c].image = Floor;)。这将再次重置您在鼠标监听器中设置的数据。

解决方案是将 mouseListener 和初始化代码移至 Panel 的构造函数中,如下所示:

class Panel extends JPanel {
ArrayList<String> TileTypes = new ArrayList<String>();
public Panel() {
//Loading array of Tile Types
try {
Scanner TileFileScanner = new Scanner(new File("TileTypes"));
while(TileFileScanner.hasNext()) {
TileTypes.add(TileFileScanner.nextLine());
}
} catch(FileNotFoundException e) {
System.err.println("Tile Type file not found.");
}

//Creating 2D array of Rectangles
xCoor = 20;
for(int r = 0; r < rows; r++) {
yCoor = 20;
for(int c = 0; c < cols; c++) {
recs[r][c] = new Rectangle(xCoor, yCoor, 18, 18);
yCoor += 19;
}
xCoor += 19;
}

//Creating 2D array of cells
for(int r = 0; r < rows; r++) {
for(int c = 0; c < cols; c++) {
cells[r][c] = new Cell(recs[r][c], "f", activeImage);
cells[r][c].image = floor;
xCoor += 21;
}
xCoor = 0;
yCoor += 21;
}

addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
for(int r = 0; r < rows; r++) {
for(int c = 0; c < cols; c++) {
if (cells[r][c].getRect().contains(e.getX(), e.getY())) {
cells[r][c].tileCode = activeKey;
cells[r][c].image = activeImage;
//System.out.println("click registered");
}
}
}
}
});
}

public void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D)g;
g.setFont(new Font("Italic",Font.PLAIN, 25));
g.drawString("Press Enter to save to file.", 1050, 50);

//Tile Type key on the rigt side of the screen
int y = 550;
for(int i = 0; i < TileTypes.size(); i++) {
g.drawString(TileTypes.get(i), 1050, y);
y += 30;
}

//Drawing cells
for(int r = 0; r < rows; r++) {
for(int c = 0; c < cols; c++) {
g.fillRect((int)cells[r][c].r.getX(), (int)cells[r][c].r.getY(),
(int)cells[r][c].r.getWidth(), (int)cells[r][c].r.getHeight());
g.setColor(Color.BLACK);
}
}
//Drawing the images stored in each cell object
for(int r = 0; r < rows; r++) {
for(int c = 0; c < cols; c++) {
g.drawImage(cells[r][c].getImage(), cells[r][c].getX(), cells[r][c].getY(), this);
}
}
}
}

另外,这不是您问题的一部分,但您正在丢弃线程中的中断异常。改为这样做:

public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(15);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

关于Java - 为什么 mouseClicked 方法在执行完成后不会永久重置变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55655837/

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