-2

There is a Robot whose starting position(0,0) and end position(5,3).I showed the direction it take to go start to goal position.

I want to implement a GUI according to this picture in java. That means whenever I click in the run button a window appear which show the robot is moving within a grid.

enter image description here

I try to implement a code in java to implement the movement.But this is only a raw code.Failed to implement the GUI. My code is like that. I have a robot class and a grid that I define in the variable 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;
         }

}

Like that.... Now can anyone help me to give a suggestion to display the GUI in Java. I don't want to use in build simulator.

azro
  • 53,056
  • 7
  • 34
  • 70
Soni Sori
  • 123
  • 1
  • 1
  • 5
  • http://idownvotedbecau.se/noattempt/ --- I see no attempt at *any* GUI code. Do you even have code to draw the grid? At the very least, don't you think you need to call some GUI update method when you change the value of `position.y`? – Andreas Feb 22 '18 at 20:19
  • That is my question.How could I attempt to implement GUI? – Soni Sori Feb 22 '18 at 20:46
  • 1
    @SoniSori The question is to broad and requires in the introduction of a number of topics including, the GUI framework your want to use; handling concurrency in said framework; representation of the graphic elements in said framework, so on and so forth. You'll need to step back and take a look at your available options, pick a framework you think will suit your needs and begin investigating how you might achieve the goal - use existing components; use custom painting; how does concurrency and animation work in the framework? If you have a specific question, then you can ask for help – MadProgrammer Feb 22 '18 at 21:10
  • Either you write this small GUI on your own or you find a library providing a game board that you can feed the positions of your figure into. If you want do write your own GUI you need a path to learn it. Depending on your personality either you visit a course, read a book, learn from online tutorials or take an online computer course. – Blcknx Feb 22 '18 at 21:14
  • Java AWT Canvas that I used to implement the grid.Also use frame. I am able to create this grid that I shown to the picture. But I just curious is that also implement by JavaFx? @MadProgrammer – Soni Sori Feb 22 '18 at 22:02
  • @SoniSori Yes, you should get able to implement it in JavaFX, seen as it’s built around supporting animation out of the box, it might be a more feasible option – MadProgrammer Feb 22 '18 at 22:05
  • ok.wanna know JavaFx.Don't have any implementation idea. But thanks. @MadProgrammer – Soni Sori Feb 22 '18 at 22:17

1 Answers1

0

You can implement this in Java using Swings. Check the following code which gives the expected result.

Add your custom implementation in moveRobot() method to determine the path to travel.

public class RobotDemo {

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

}

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

}

vbbharath
  • 127
  • 4
  • Thank you.If there is a obstacle then I think we need to implement Algorithm like A*. How could I create obstacle grid? – Soni Sori Feb 23 '18 at 17:57