不使用模拟器的Java GUI实现
有一个机器人,它的起始位置是0,0,结束位置是5,3。我指出了从起始位置到目标位置的方向 我想根据这幅图在java中实现一个GUI。这意味着每当我点击运行按钮时,就会出现一个窗口,显示机器人正在网格内移动 我试图用java实现一个代码来实现这个移动。但这只是一个原始代码。未能实现GUI。我的代码是这样的。我有一个机器人类和一个在变量网格中定义的网格不使用模拟器的Java GUI实现,java,robotics,Java,Robotics,有一个机器人,它的起始位置是0,0,结束位置是5,3。我指出了从起始位置到目标位置的方向 我想根据这幅图在java中实现一个GUI。这意味着每当我点击运行按钮时,就会出现一个窗口,显示机器人正在网格内移动 我试图用java实现一个代码来实现这个移动。但这只是一个原始代码。未能实现GUI。我的代码是这样的。我有一个机器人类和一个在变量网格中定义的网格 Public class Robot{ int[][] grid = new int[][]{{0, 0, 0, 0, 0, 0}, {0
Public class Robot{
int[][] grid = new int[][]{{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}};
int North=0;int East=1;int South=2;int west=3;
public void forward() {
switch (orientation) {
case "north":
if (grid.isValid(position.x, position.y+1)) {
position.y += 1;
} else {
System.out.println("Can't go there!");
}
break;
}
}
像那样。。。。现在谁能帮我提出一个用Java显示GUI的建议。我不想使用内置模拟器。您可以使用Swigs在Java中实现这一点。检查以下给出预期结果的代码
在moveRobot方法中添加自定义实现以确定移动路径
公共类机器人演示{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GridFrame().setVisible(true);
}
});
}
}
类GridFrame扩展了JFrame{
public GridFrame() {
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Set the width,height,number of rows and columns
final GridPanel panel = new GridPanel(300, 300, 10, 10);
add(panel);
JButton button = new JButton("Start");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Set the location of target grid
panel.moveTo(5, 3);
}
});
add(button, BorderLayout.SOUTH);
pack();
}
}
类GridPanel扩展了JPanel{
int width, height, rows, columns;
int gridWidth, gridHeight;
int targetX, targetY, x, y = 0;
boolean isRunning = true;
public GridPanel(int width, int height, int rows, int columns) {
this.width = width;
this.height = height;
this.rows = rows;
this.columns = columns;
gridHeight = height / rows;
gridWidth = width / columns;
setPreferredSize(new Dimension(width, height));
}
@Override
public void paint(Graphics g) {
g.clearRect(0, 0, width, height);
// Draw grid
g.setColor(Color.GRAY);
for (int i = 1; i <= rows; i++) {
g.drawLine(0, i * gridHeight, width, i * gridHeight);
}
for (int j = 1; j <= columns; j++) {
g.drawLine(j * gridWidth, 0, j * gridWidth, height);
}
// Draw green block for movement
g.setColor(Color.GREEN);
g.fillRect(x, y, gridWidth, gridHeight);
}
public void moveTo(int x, int y) {
targetX = x * gridWidth;
targetY = y * gridHeight;
isRunning = true;
// Start your animation thread
new Thread(new Runnable() {
@Override
public void run() {
while (isRunning) {
moveRobot();
try {
Thread.sleep(700); // Customize your refresh time
} catch (InterruptedException e) {
}
}
}
}).start();
}
private void moveRobot() {
// Add all your path movement related code here
int newX = x + gridWidth;
int newY = y + gridHeight;
if (newX >= width || newX >= targetX) {
y = newY;
} else {
x = newX;
}
if (newX >= targetX && newY >= targetY) {
// Reached target grid. Stop moving
isRunning = false;
return;
}
// Repaint the screen
repaint();
}
}-我看不到任何GUI代码的尝试。你有画网格的代码吗?至少,当您更改position.y的值时,您不认为您需要调用一些GUI更新方法吗?这是我的问题。我如何尝试实现GUI?@SoniSori这个问题非常广泛,需要介绍一些主题,包括您想要使用的GUI框架;在所述框架中处理并发;所述框架中图形元素的表示,等等。你需要退后一步,看看你的可用选项,选择一个你认为适合你需要的框架,并开始研究如何实现目标——使用现有组件;使用定制绘画;并发和动画在框架中是如何工作的?如果您有一个特定的问题,那么您可以寻求帮助,或者您自己编写这个小型GUI,或者您可以找到一个提供游戏板的库,您可以将图形的位置输入其中。如果你想写你自己的GUI,你需要一个学习的途径。根据您的个性,您可以访问课程、阅读书籍、从在线教程中学习或参加在线计算机课程。我用于实现网格的Java AWT画布。也可以使用框架。我能够创建我向图片显示的网格。但我只是好奇,这也是由JavaFx实现的吗@MadProgrammer谢谢。如果有障碍,那么我认为我们需要像*一样实现算法。如何创建障碍网格?