gpt4 book ai didi

java - 在 do-while 循环中更新定时器上的 JButton

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:07 26 4
gpt4 key购买 nike

我在让 JButton 在 do-while 循环中重复更新(与计时器一起使用)时遇到了一些麻烦。我正在开发一个简单的游戏,在 10 * 10 的图 block 对象网格上玩,这些对象对应于具有 100 个按钮的 JButton arrayList。

程序的这一部分处理简单的寻路(即,如果我点击角色,然后点击一个空方 block ,角色将在到达目的地的途中穿过每个方 block )。每一步之间都有延迟,因此用户可以看到角色的进度。

在当前状态下,移动是正确的,但 JButton 仅在角色到达目的地时更新,而不是在中间步骤上更新。

public void move(int terrainTile)
{
int currentPosition = actorList.get(selectedActor).getPosition();
int movementValue = 0;
int destination = terrainTile;
int destinationX = destination / 10;
int destinationY = destination % 10;

do
{
currentPosition = actorList.get(selectedActor).getPosition(); // Gets PC's current position (before move)
System.out.println("Old position is " + currentPosition);
int currentX = currentPosition / 10;
int currentY = currentPosition % 10;


if(actorList.get(selectedActor).getCurrentAP() > 0)
{
movementValue = 0;

if(destinationX > currentX)
{
movementValue += 10;
}

if(destinationX < currentX)
{
movementValue -= 10;
}

if(destinationY > currentY)
{
movementValue += 1;
}

if(destinationY < currentY)
{
movementValue -= 1;
}

int nextStep = currentPosition + movementValue;

myGame.setActorIdInTile(currentPosition, -1); //Changes ActorId in PC current tile back to -1
scrubTiles(currentPosition);

actorList.get(selectedActor).setPosition(nextStep); // Sets new position in actor object
System.out.println("Actor " + selectedActor + " " + actorList.get(selectedActor).getName() + " position has been updated to " + nextStep);

myGame.setActorIdInTile(nextStep, selectedActor); // Sets ActorId in moved to Tile
System.out.println("Tile " + nextStep + " actorId has been updated to " + selectedActor);

buttons.get(nextStep).setIcon(new ImageIcon(actorList.get(selectedActor).getImageName()));

// If orthagonal move AP-4
if(movementValue == 10 || movementValue == -10 || movementValue == 1 || movementValue == -1)
{
actorList.get(selectedActor).reduceAP(4);
}
// If diagonal move AP-6
else
{
actorList.get(selectedActor).reduceAP(6);
}

System.out.println(actorList.get(selectedActor).getName() + " has " + actorList.get(selectedActor).getCurrentAP() + " AP remaining");

try
{
Thread.sleep(500); // one second
}
catch (Exception e){}

buttons.get(nextStep).repaint();

}

else
{
System.out.println(actorList.get(selectedActor).getName() + " has insufficient AP to move");
break;
}

}while(destination != (currentPosition + movementValue));

我尝试过的:

  1. buttons.get(nextStep).repaint(); (尝试在设置 imageIcon 后放置一个重新绘制按钮的命令。没有变化。

  2. buttons.get(nextStep).revalidate(); (没有 100% 确定这是做什么的 - 它作为一个潜在的解决方案出现,但不起作用。

  3. 第 1 步和第 2 步相结合

  4. 探讨了 Swing 定时器类 - 每次 Action 时都不会发生移动,只有选择字符,目标瓷砖是空的)

如有任何帮助,我们将不胜感激!

最佳答案

我真的不知道你在评论中到底想知道什么,尽管对上面的答案 +1,在我看来这才是真正的原因。看看这个示例程序,只需将您的调用添加到 timerAction 中的 move(...) 方法,看起来这对您有用。在这里试试这个代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GridExample
{
private static final int SIZE = 36;
private JButton[] buttons;
private int presentPos;
private int desiredPos;
private Timer timer;
private Icon infoIcon =
UIManager.getIcon("OptionPane.informationIcon");

private ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
buttons[presentPos].setIcon(null);
if (desiredPos < presentPos)
{
presentPos--;
buttons[presentPos].setIcon(infoIcon);
}
else if (desiredPos > presentPos)
{
presentPos++;
buttons[presentPos].setIcon(infoIcon);
}
else if (desiredPos == presentPos)
{
timer.stop();
buttons[presentPos].setIcon(infoIcon);
}
}
};

public GridExample()
{
buttons = new JButton[SIZE];
presentPos = 0;
desiredPos = 0;
}

private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Grid Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel contentPane = new JPanel();
contentPane.setLayout(new GridLayout(6, 6, 5, 5));
for (int i = 0; i < SIZE; i++)
{
final int counter = i;
buttons[i] = new JButton();
buttons[i].setActionCommand("" + i);
buttons[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
desiredPos = Integer.parseInt(
(String) buttons[counter].getActionCommand());
timer.start();
}
});
contentPane.add(buttons[i]);
}
buttons[presentPos].setIcon(infoIcon);

frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(1000, timerAction);
}

public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new GridExample().createAndDisplayGUI();
}
});
}
}

关于java - 在 do-while 循环中更新定时器上的 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10835322/

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