title: How to determine the coordinates of the next rectangle on JPanel
body: <p>i have an arraylist <code>RecArray</code> of objects with each object containing two int values, one for the width and for the height of a rectangle. Each rectangle's height and width are a multiple of ten. the rectangles have to be passed on to the surface  as in the given order in <code>RecArray</code> from left to right and from top to bottom. my problem is i can not find the x,y coordinates of the next rectangle. what im trying to do is, starting at the coordinate (0,0) i generate the first rectangle, add it to an arraylist <code>RecList</code>. Then i set the x and y coordinates. x becomes <code>x = x+RecArray.get(0).getLength1() + 1</code>. if x is greater than the width of the jpanel surface then it becomes 0 and y becomes <code>y = y + 10</code> . starting from the second object in the RecArray i try to generate rectangles with the given coordinates and width&amp;height. Then i try to compare them with all the previous rectangles to see if there is any overlapping. if there is no overlapping, the rectangle will be drawn, if there is overlapping, the x coordinate of the rec becomes <code>x = RecList.get(j).width+1</code> and if that exceeds the width x becomes 0 and y is <code>y=y+10</code>. Then i regenate the current rectangle with the new coordinates and compare with the other rectangles in <code>RecList</code> again  till i find the right spot for the current rectangle.ive been dealing with that issue for the last 5 days and am really fed up now. i would greatly appreciate any tipps. and Please be patient with me. im still learning programming. </p>
  
  <pre><code>super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;     
  Rectangle rec = new Rectangle(x, y, RecArray.get(0).getWidth(),
                RecArray.get(0).getHeight());
  RecList.add(rec);
  recPaint(g2,RecArray.get(0)); 
  x = x + RecArray.get(0).getWidth() + 1;
  int i;
  for (i = 1; i &lt; RecArray.size(); i++) {
    if (x &gt;= this.getArea().getWidth()) {
          x = 0;
          y = y + 10;
    }     
    Rectangle rec1 = new Rectangle(x, y, RecArray.get(i)
                .getWidth(), RecArray.get(i).getheight());
    for (int j= 0; j &lt; RecList.size(); j++) {
        if (!recIntersect(rec1, RecList.get(j))) {
           RecList.add(rec1);
           recPaint(g2,RecArray.get(i));
           break;
        } 
        else {
             x = RecList.get(j).width;
             if (x &gt;= this.getFlaeche().getLength1()) {
                 x = 0;
                 y = y + 10;
             }
             rec1 = new Rectangle(x, y,RecArray.get(i).  .getWidth(),  
                   RecArray.get(i).getHeight());
        }
        x = x + RecArray.get(i).getWidth();
   } 
   //With this method using the given rec parameter a rectangle  will be drawn on the g2 and filled in blue colour
  private void recPaint (Graphics2D g2, RecType rec){
        g2.setColor(Color.BLUE);  
        g2.fillRect(x, y, rec.getWidth(),
        rec.getLength2());
        g2.setColor(Color.BLACK);
        g2.drawRect(x, y, rec.getHeight(),
        rec.getLength2());
  }
  // returns true, if two rectangles overlap
  private boolean recIntersect(Rectangle rec1, Rectangle rec2) {
     if( rec1.intersects(rec2)){
           return true;
      }
      return false;
  }
  </code></pre>
  
  <p>Edit: apparently i haven't stated clearly what my problem is. my problem is, that the way i generate (x,y) coordinates of the rectangles is obviously wrong.  the way my algorithm works doesnt get the results i want. i want my rectangles to be placed neatly next to/above/below each other WITHOUT overlapping, which is not the case.</p>