Graphics g = graphics.create();
int borderWidth = getBorderWidth(component);
// *** Clipping for top and left borders ***
Polygon p = new Polygon();
p.addPoint(x, y);
p.addPoint(x + width, y);
p.addPoint(x + width - borderWidth, y + borderWidth);
p.addPoint(x + borderWidth, y + borderWidth);
p.addPoint(x + borderWidth, y + height - borderWidth);
p.addPoint(x, y + height);
g.setClip(p);
g.clipRect(oldClip.x, oldClip.y, oldClip.width, oldClip.height);
// top border
for (int i = x; i < x + width; i += imageWidth) {
g.drawImage(topLeftImage, i, y, null);
}
// left border
for (int i = y; i < y + height; i += imageHeight) {
g.drawImage(topLeftImage, x, i, null);
}
// *** Clipping for bottom and right borders ***
// We have the same number of vertices as before, so it's efficient to
// reuse the polygon
p.reset();
p.addPoint(x + width, y);
p.addPoint(x + width, y + height);
p.addPoint(x, y + height);
p.addPoint(x + borderWidth, y + height - borderWidth);
p.addPoint(x + width - borderWidth, y + height - borderWidth);
p.addPoint(x + width - borderWidth, y + borderWidth);
g.setClip(p);
g.clipRect(oldClip.x, oldClip.y, oldClip.width, oldClip.height);
// Bottom border. More than one y coordinate may be needed in case the
// tile border coincides to be inside the bottom border.