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