Rectangle rect = new Rectangle(); int startIndex = children.length - 1; for (int i = 0; i < children.length; i++) { Rectangle b = children[i].getBounds(); if (children[i].isOpaque() && children[i].isVisible() && g.hitClip(b.x, b.y, b.width, b.height)) { if (rect.isEmpty()) rect.setBounds(b); else SwingUtilities.computeUnion(b.x, b.y, b.width, b.height, rect);  if (SwingUtilities.isRectangleContainingRectangle(rect, inner)) { startIndex = i; break; } } }
  private void paintChildrenOptimized(Graphics g)  {    Shape originalClip = g.getClip();    Rectangle inner = SwingUtilities.calculateInnerArea(this, rectCache);    g.clipRect(inner.x, inner.y, inner.width, inner.height);    Component[] children = getComponents();    // Find the bottommost component that needs to be painted. This is the    // component that - together with the rectangles of the components that    // are painted above it - covers the whole clip area.    Rectangle rect = new Rectangle();    int startIndex = children.length - 1;        for (int i = 0; i < children.length; i++)          {        Rectangle b = children[i].getBounds();        if (children[i].isOpaque() && children[i].isVisible()            && g.hitClip(b.x, b.y, b.width, b.height))          {            if (rect.isEmpty())              rect.setBounds(b);            else              SwingUtilities.computeUnion(b.x, b.y, b.width, b.height, rect);            if (SwingUtilities.isRectangleContainingRectangle(rect, inner))              {                startIndex = i;                break;              }          }      }    // paintingTile becomes true just before we start painting the component's    // children.    paintingTile = true;    for (int i = startIndex; i >= 0; i--) //children.length; i++)      {        // paintingTile must be set to false before we begin to start painting        // the last tile.        if (i == children.length - 1)          paintingTile = false;        if (!children[i].isVisible())          continue;        Rectangle bounds = children[i].getBounds(rectCache);        Rectangle oldClip = g.getClipBounds();        if (oldClip == null)          oldClip = bounds;        if (!g.hitClip(bounds.x, bounds.y, bounds.width, bounds.height))          continue;        boolean translated = false;        try          {            g.clipRect(bounds.x, bounds.y, bounds.width, bounds.height);            g.translate(bounds.x, bounds.y);            translated = true;            children[i].paint(g);          }        finally          {            if (translated)              g.translate(-bounds.x, -bounds.y);            g.setClip(oldClip);          }      }    g.setClip(originalClip);  }