Java 中的 GUI 实现而不使用模拟器

GUI implementation in Java without using simulator

有一个机器人的起始位置 (0,0) 和结束位置 (5,3)。我展示了它从开始到目标位置的方向。

我想根据java中的这张图实现一个GUI。这意味着每当我单击 运行 按钮时,都会出现 window,表明机器人正在网格内移动。

我尝试在 java 中实现一个代码来实现 movement.But 这只是一个原始的 code.Failed 来实现 GUI。我的代码就是这样。我有一个机器人 class 和一个我在变量 Grid.

中定义的网格
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 的建议。我不想在构建模拟器中使用。

您可以使用 Swings 在 Java 中实现这一点。检查以下给出预期结果的代码。

在 moveRobot() 方法中添加自定义实现以确定行进路径。

public class RobotDemo {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new GridFrame().setVisible(true);
        }
    });
}

}

class 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();
}

}

class 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();
}

}