I want to create this obstacle in red color in Java
class Grid extends JFrame{
public Grid(){
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(250,300);
// Set the width,height,number of rows and columns
final GridPanel panel = new GridPanel(300, 300, 10, 10);
add(panel);
}
}
class GridPanel22 extends JPanel{
int width, height, rows, columns;
int gridWidth, gridHeight;
public GridPanel22(int width, int height, int rows, int columns){
gridHeight = height / rows;
gridWidth = width / columns;
setPreferredSize(new Dimension(width, height));
}
public void paint(Graphics g){
g.setColor(Color.red);
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);
}
}
}
This code creates a grid but I'm unable to create an obstacles. In the picture those Red cells are the obstacle cell.To distinguish between normal grid and main grid I paint it with red. . I know that there is a function called drawRect()
in java but I'm not sure how it works.As per the picture there is a obstacle starting from grid (1,1) and ending it (5,4). Now to make those cell as a obstacle cell , I want to assign 1(one) to all those cells means (1,1)->1,then (2,1)->1 ......(5,4)->1 etc. So that when my Robot move from one cell to another it can avoid those cell and found it's own path to go to destination.