Rectangle clip = g.getClipBounds(); for (int i = 0; i < children.length; i++) { Rectangle childBounds = children[i].getBounds(); if (children[i].isOpaque() && SwingUtilities.isRectangleContainingRectangle(childBounds, g.getClipBounds())) { startIndex = i; break; } }
if (isOptimizedDrawingEnabled()) paintChildrenOptimized(g); else paintChildrenWithOverlap(g);
  protected void paintChildren(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 a    // component that completely covers the current clip and is opaque. In    // this case we don't need to paint the components below it.    int startIndex = children.length - 1;    // No need to check for overlapping components when this component is    // optimizedDrawingEnabled (== it tiles its children).    if (! isOptimizedDrawingEnabled())      {        Rectangle clip = g.getClipBounds();        for (int i = 0; i < children.length; i++)          {            Rectangle childBounds = children[i].getBounds();            if (children[i].isOpaque()                && SwingUtilities.isRectangleContainingRectangle(childBounds,                                                            g.getClipBounds()))              {                startIndex = i;                break;              }          }      }    // paintingTile becomes true just before we start painting the component's    // children.    paintingTile = true;    for (int i = startIndex; i >= 0; --i)      {        // paintingTile must be set to false before we begin to start painting        // the last tile.        if (i == 0)          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);  }