Measurements mes = getMeasurements(c); Font oldFont = g.getFont(); Color oldColor = g.getColor();
Rectangle borderRect = new Rectangle(x + EDGE_SPACING, y + EDGE_SPACING, width - (EDGE_SPACING * 2), height - (EDGE_SPACING * 2)); Point textLoc = new Point();
  public void paintBorder(Component c, Graphics  g,                           int x, int y, int width, int height)  {    Measurements mes = getMeasurements(c);    Font oldFont = g.getFont();    Color oldColor = g.getColor();    /**     * A local helper class for painting the border without changing     * any pixels inside the rectangle of the title text.     */    class BorderPainter    {      private Component c;      private Border b;      private int x, y, width, height;      /**       * Constructs a BorderPainter.       *       * @param c the component whose border is being painted.       * @param b the border object.       * @param x the x coordinate of the rectangle delimiting the border.       * @param y the y coordinate of the rectangle delimiting the border.       * @param width the width of the rectangle delimiting the border.       * @param height the width of the rectangle delimiting the border.       */      public BorderPainter(Component c, Border b,                           int x, int y, int width, int height)      {        this.c = c;        this.b = b;        this.x = x;        this.y = y;        this.width = width;        this.height = height;      }      /**       * Paints the entire border.       */      public void paint(Graphics g)      {        if (b != null)          b.paintBorder(c, g, x, y, width, height);      }      /**       * Paints the border, clipping the drawing operation to a       * given rectangular area.       */      private void paint(Graphics g,                         int clipX, int clipY, int clipWidth, int clipHeight)      {        Shape oldClip = g.getClip();        try        {          g.clipRect(clipX, clipY, clipWidth, clipHeight);          paint(g);        }        finally        {          g.setClip(oldClip);        }      }      /**       * Paints the border without affecting a given rectangular area.       * This is used for painting the border without drawing anything       * underneath the title text.       *       * <p>Since we do not want to introduce unnecessary dependencies       * on Java 2D, we perform the clipping without constructive geometry       * (provided by java.awt.geom.Area). Instead, the border&#x2019;s       * bounding rectangle is split into smaller parts, which are then       * clipped and painted individually.:       *       * <p><pre>       *    +--------------------+          +--------------------+       *    |                    |          |        1           |       *    |   +--------+       |          +---+--------+-------+       *    |   | hole   |       |  |====>  | 2 | hole   |   3   |       *    |   +--------+       |          |---+--------+-------+       *    |                    |          |        4           |       *    +--------------------+          +--------------------+</pre>       *       */      public void paintExcept(Graphics g,                              int holeX, int holeY, int holeWidth, int holeHeight)      {        int stripeHeight;        stripeHeight = holeY - y;        if (stripeHeight > 0)          paint(g, x, y, width, stripeHeight);   // patch #1 in the image above        stripeHeight = holeHeight;        if (stripeHeight > 0)        {          paint(g, x, holeY, holeX - x, stripeHeight);  // patches #2 and #3          paint(g, holeX + holeWidth, holeY, x + width - (holeX + holeWidth), stripeHeight);        }        stripeHeight = height - (holeY - y + holeHeight);        if (stripeHeight > 0)          paint(g, x, y + height - stripeHeight, width, stripeHeight); // #4      }    };    BorderPainter bp;    int textX, textY, borderWidth, borderHeight;    borderWidth = width - (mes.outerSpacing.left + mes.outerSpacing.right);    borderHeight = height - (mes.outerSpacing.top + mes.outerSpacing.bottom);    bp = new BorderPainter(c, getBorder(),                           x + mes.outerSpacing.left, y + mes.outerSpacing.top,                           borderWidth, borderHeight);    switch (getRealTitleJustification(c))    {    case LEFT:      textX = x + EDGE_SPACING + TEXT_INSET_H;      break;    case CENTER:      textX = x + (borderWidth - mes.textWidth) / 2;      break;    case RIGHT:      textX = x + borderWidth - (mes.textWidth + TEXT_INSET_H);      break;    default:      throw new IllegalStateException();    }    switch (titlePosition)    {    case ABOVE_TOP:      textY = y + EDGE_SPACING;      break;    case TOP:    case DEFAULT_POSITION:    default:      textY = y + mes.outerSpacing.top + mes.borderInsets.top - mes.textAscent              + mes.lineHeight;      break;    case BELOW_TOP:      textY = y + mes.outerSpacing.top + mes.borderInsets.top + TEXT_SPACING;      break;    case ABOVE_BOTTOM:      textY = y + height - mes.outerSpacing.bottom - mes.borderInsets.bottom        - TEXT_SPACING - (mes.textAscent + mes.textDescent);      break;    case BOTTOM:    case BELOW_BOTTOM:      textY = y + height - (mes.textAscent + mes.textDescent);      break;    }    if (mes.trimmedText == null)      bp.paint(g);    else    {      try      {        g.setFont(mes.font);        g.setColor(getTitleColor());        g.drawString(mes.trimmedText, textX, textY + mes.textAscent);      }      finally      {        g.setFont(oldFont);        g.setColor(oldColor);      }      bp.paintExcept(g, textX, textY,                     mes.textWidth, mes.textAscent + mes.textDescent);    }  }