title: Collision detection between a circle and a rectangle without intersect method of awt
body: <p>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.</p>
  
  <p>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. I tried intersect method but I am not able to implement that in my code as I am using fillOval and fillRect.</p>
  
  <p><strong>I know this question is already asked in the community multiple times. But, all answers revolve around the intersect method in AWT. I am not using the Rectangle/Ellipse classes. I am using fillRect and fillOval to create my shapes. Is there any other way to prevent collision when in the initialize method these shapes are being first time drawn on the JPanel randomly.</strong></p>
  
  <p>Code:</p>
  
  <pre><code>public void initializePanel(Graphics g)
  {
      createObstacle(g,150,225,100,40);
      createObstacle(g,500,300,40,100);  
      drawNParticles(g);
      createRobot(g); 
  }
  
  private void createRobot(Graphics g)
  {
      ArrayList&lt;Integer&gt; robot_list= new ArrayList&lt;Integer&gt;();
      robot_list=positionRobot(robot_x,robot_y);
      robot_x=robot_list.get(0);
      robot_y=robot_list.get(1);
      System.out.println("Robot:"+robot_x+"--"+robot_y+"--"+robot_orientation);
      if((robot_x&gt;620)||(robot_y&gt;395)||(robot_x&lt;1)||(robot_y&lt;1))
      {
          robot_orientation=180;
      }
      drawRobot(g,robot_x,robot_y,robot_radius);
  }
  
  
  private ArrayList&lt;Integer&gt; positionRobot(int x, int y)
  {
      int robot_radius=50;  
      ArrayList&lt;Integer&gt; list= new ArrayList&lt;Integer&gt;();
      if(counter==0)
      {
          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&lt;251&amp;&amp;x&gt;149)&amp;&amp;(y&lt;266&amp;&amp;y&gt;224))
          {
              x=0;
              y=0;
              positionRobot(x,y);
          }
          else if((x&lt;=541&amp;&amp;x&gt;499)&amp;&amp;(y&lt;401&amp;&amp;y&gt;299))
          {
          x=0;
          y=0;
          positionRobot(x,y);
          }
      counter++;
      }
      else
      {
          setXPosition_robot(robot_x);
          setYPosition_robot(robot_y);
      }
          list.add(x);
          list.add(y);
          return list;                
  }
  
  private void drawNParticles(Graphics g)
  {   
      ArrayList&lt;Integer&gt; list;        
      list = new ArrayList&lt;Integer&gt;(Collections.nCopies(n, 0));
      for(int i=0;i&lt;list.size();i++)
      {
         generateParticle(g);
      }
  }
  
  private void generateParticle(Graphics g)
  {
      int radius = 4;
      ArrayList&lt;Integer&gt; list= new ArrayList&lt;Integer&gt;();
      list=positionParticles(particle_x,particle_y);
      g.setColor(Color.RED);
      g.fillOval(list.get(0),list.get(1), radius, radius);
  }
  
  private ArrayList&lt;Integer&gt; positionParticles(int x, int y)
  {
      int radius = 4;
      ArrayList&lt;Integer&gt; list= new ArrayList&lt;Integer&gt;();
  
          x=randomInteger(2,678); // bounds of x between which the particles should be generated
          y=randomInteger(2,448); // bounds of y between which the particles should be generated
          x=x-(radius/2);
          y=y-(radius/2);
          if((x&lt;251&amp;&amp;x&gt;149)&amp;&amp;(y&lt;266&amp;&amp;y&gt;224))
          {
              x=0;
              y=0;
              positionParticles(x,y);
          }
          if((x&lt;541&amp;&amp;x&gt;499)&amp;&amp;(y&lt;401&amp;&amp;y&gt;299))
          {
              x=0;
              y=0;
              positionParticles(x,y);
          }
      list.add(x);
      list.add(y);
      return list;
  }
  
  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);   
  }
  
  private static Random rand;
  
  private static int randomInteger(int min, int max)
  {
      if(rand==null)
          rand=new Random();
      int randomNum = rand.nextInt((max - min) + 1) + min;
      return randomNum;
  }
  
  private static Random randFloat;
  private static float randomFloat(float min, float max)
  {
      if(randFloat==null)
          randFloat=new Random();
      float randomNum = randFloat.nextFloat() *(max-min)+ min;
      return randomNum;
  }
  </code></pre>