I am writing a code in which I am drawing two rectangles as obstacles and an oval as the robot. The robot moves while obstacles are stationary. The problem is as the robot's location is generated randomly in the beginning, it sometimes overlap the rectangles. I am trying continuously to fix this, but I am not able to find a good solution.
I tried checking the bounds of the rectangle with the x,y coordinate of circle before drawing it. But, it only checks for that single point and sometimes some other point of circle overlaps the rectangle.
x=randomInteger(25,655);//so that it stays inside the content_Walls panel
y=randomInteger(25,425); //so that it stays inside the content_Walls panel
x=x-(robot_radius/2);
y=y-(robot_radius/2);
robot_orientation=randomFloat(0,360);
if((x<251&&x>149)&&(y<266&&y>224))
{
x=0;
y=0;
positionRobot(x,y);
}
else if((x<=541&&x>499)&&(y<401&&y>299))
{
x=0;
y=0;
positionRobot(x,y);
}
I am using the following code to draw the shapes:
private void createObstacle(Graphics g, int x, int y, int width, int height)
{
g.setColor(Color.YELLOW);
g.fillRect(x, y, width, height);
}
private void drawRobot(Graphics g, int x, int y, int radius)
{
g.setColor(Color.GREEN);
g.fillOval(x, y, radius, radius);
}
I guess i cannot use the shape.intersects then? Can I?
Is there any way to solve this issue. Is there way to check the background color at that coordinate and accordingly do some action?