Java中的GUI实现,无需使用模拟器

时间:2018-02-22 20:07:07

标签: java robotics

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

我想根据java中的这张图片实现一个GUI。这意味着每当我点击运行按钮时,会出现一个窗口,显示机器人正在网格内移动。

enter image description here

我尝试在java中实现代码来实现移动。但这只是一个原始代码。无法实现GUI。我的代码就是这样。我有一个机器人类和一个我在变量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的建议。我不想在构建模拟器中使用。

1 个答案:

答案 0 :(得分:0)

您可以使用Swings在Java中实现此功能。请检查以下代码,以获得预期结果。

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

公共类RobotDemo {

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

}