Date: 2021jul26
Language: Java
Q. Java: Grow/Shrink AWT Rectangle by percentage
A. Use the grow() member
void growByPercent(Rectangle rect, final float percent) {
final float factor = percent / 100f / 2f;
// Divide the percent by 100 to get a factor between 0 and 1
// Divide by 2 because grow() applies to both sides
final int xGrow = Math.round(rect.width * factor);
final int yGrow = Math.round(rect.height * factor);
rect.grow(xGrow, yGrow);
}
void shrinkByPercent(Rectangle rect, final float percent) {
growByPercent(rect, -1 * percent);
}