Adding support for complex borders (added decorations next to the corners).

This commit is contained in:
Stian Grenborgen 2024-01-01 19:53:53 +01:00
parent 788d7dfa47
commit 0c8f29975b
1 changed files with 217 additions and 44 deletions

View File

@ -49,6 +49,7 @@ public class FreeColImageBorder extends AbstractBorder {
public static final FreeColImageBorder buttonBorder = new FreeColImageBorder("image.border.button");
public static final FreeColImageBorder simpleButtonBorder = new FreeColImageBorder("image.border.button.simple");
public static final FreeColImageBorder menuBarBorder = new FreeColImageBorder("image.border.menu");
private static float scaleFactor = 1;
@ -69,40 +70,80 @@ public class FreeColImageBorder extends AbstractBorder {
*/
private BufferedImage topLeftCornerImage;
/**
* NNW
*/
private BufferedImage topStartImage;
/**
* N-border
*/
private BufferedImage topImage;
/**
* NNE
*/
private BufferedImage topEndImage;
/**
* NE-corner
*/
private BufferedImage topRightCornerImage;
/**
* ENE
*/
private BufferedImage rightStartImage;
/**
* E-border
*/
private BufferedImage rightImage;
/**
* ESE
*/
private BufferedImage rightEndImage;
/**
* SE-corner
*/
private BufferedImage bottomRightCornerImage;
/**
* SSE
*/
private BufferedImage bottomEndImage;
/**
* S-border
*/
private BufferedImage bottomImage;
/**
* SSW
*/
private BufferedImage bottomStartImage;
/**
* SW-corner
*/
private BufferedImage bottomLeftCornerImage;
/**
* WSW
*/
private BufferedImage leftEndImage;
/**
* W-border
*/
private BufferedImage leftImage;
/**
* WNW
*/
private BufferedImage leftStartImage;
private FreeColImageBorder(String baseKey) {
@ -127,13 +168,21 @@ public class FreeColImageBorder extends AbstractBorder {
private void loadImages() {
loadImages(getImage(baseKey + ".nw"),
getImage(baseKey + ".nnw"),
getImage(baseKey + ".n"),
getImage(baseKey + ".nne"),
getImage(baseKey + ".ne"),
getImage(baseKey + ".ene"),
getImage(baseKey + ".e"),
getImage(baseKey + ".ese"),
getImage(baseKey + ".se"),
getImage(baseKey + ".sse"),
getImage(baseKey + ".s"),
getImage(baseKey + ".ssw"),
getImage(baseKey + ".sw"),
getImage(baseKey + ".w"));
getImage(baseKey + ".wsw"),
getImage(baseKey + ".w"),
getImage(baseKey + ".wnw"));
initialized = true;
}
@ -150,21 +199,37 @@ public class FreeColImageBorder extends AbstractBorder {
* @param leftImage W-border
*/
private void loadImages(BufferedImage topLeftCornerImage,
BufferedImage topImage,
BufferedImage topRightCornerImage,
BufferedImage rightImage,
BufferedImage bottomRightCornerImage,
BufferedImage bottomImage,
BufferedImage bottomLeftCornerImage,
BufferedImage leftImage) {
BufferedImage topStartImage,
BufferedImage topImage,
BufferedImage topEndImage,
BufferedImage topRightCornerImage,
BufferedImage rightStartImage,
BufferedImage rightImage,
BufferedImage rightEndImage,
BufferedImage bottomRightCornerImage,
BufferedImage bottomEndImage,
BufferedImage bottomImage,
BufferedImage bottomStartImage,
BufferedImage bottomLeftCornerImage,
BufferedImage leftEndImage,
BufferedImage leftImage,
BufferedImage leftStartImage) {
this.topLeftCornerImage = topLeftCornerImage;
this.topStartImage = topStartImage;
this.topImage = topImage;
this.topEndImage = topEndImage;
this.topRightCornerImage = topRightCornerImage;
this.rightStartImage = rightStartImage;
this.rightImage = rightImage;
this.rightEndImage = rightEndImage;
this.bottomRightCornerImage = bottomRightCornerImage;
this.bottomEndImage = bottomEndImage;
this.bottomImage = bottomImage;
this.bottomStartImage = bottomStartImage;
this.bottomLeftCornerImage = bottomLeftCornerImage;
this.leftImage = leftImage;
this.leftEndImage = leftEndImage;
this.leftImage = leftImage;
this.leftStartImage = leftStartImage;
}
public static void setScaleFactor(float scaleFactor) {
@ -216,18 +281,34 @@ public class FreeColImageBorder extends AbstractBorder {
@Override
public Insets getBorderInsets(Component c, Insets insets) {
ensureInitialized();
int top = Math.max(Math.max(getHeight(topImage),
getHeight(topLeftCornerImage)),
getHeight(topRightCornerImage));
int left = Math.max(Math.max(getWidth(leftImage),
getWidth(topLeftCornerImage)),
getWidth(bottomLeftCornerImage));
int bottom = Math.max(Math.max(getHeight(bottomImage),
getHeight(bottomLeftCornerImage)),
getHeight(bottomRightCornerImage));
int right = Math.max(Math.max(getWidth(rightImage),
getWidth(topRightCornerImage)),
getWidth(bottomRightCornerImage));
int top = max(
getHeight(topImage),
getHeight(topStartImage),
getHeight(topEndImage),
getHeight(topLeftCornerImage),
getHeight(topRightCornerImage)
);
int left = max(
getWidth(leftImage),
getWidth(leftStartImage),
getWidth(leftEndImage),
getWidth(topLeftCornerImage),
getWidth(bottomLeftCornerImage)
);
int bottom = max(
getHeight(bottomImage),
getHeight(bottomStartImage),
getHeight(bottomEndImage),
getHeight(bottomLeftCornerImage),
getHeight(bottomRightCornerImage)
);
int right = max(
getWidth(rightImage),
getWidth(rightStartImage),
getWidth(rightEndImage),
getWidth(topRightCornerImage),
getWidth(bottomRightCornerImage)
);
if (topImage == null) {
top = 0;
@ -252,6 +333,16 @@ public class FreeColImageBorder extends AbstractBorder {
return insets;
}
}
private int max(int... numbers) {
int highest = Integer.MIN_VALUE;
for (int i : numbers) {
if (i > highest) {
highest = i;
}
}
return highest;
}
/**
* Paints the border on the given component.
@ -267,51 +358,133 @@ public class FreeColImageBorder extends AbstractBorder {
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
ensureInitialized();
Insets insets = getBorderInsets(c);
Graphics2D g2 = (Graphics2D) g;
final Insets insets = getBorderInsets(c);
final Graphics2D g2 = (Graphics2D) g;
// Get width and height of the images
int topHeight = getHeight(topImage);
int leftWidth = getWidth(leftImage);
int bottomHeight = getHeight(bottomImage);
int rightWidth = getWidth(rightImage);
int topLeftCornerWidth = getWidth(topLeftCornerImage);
int topLeftCornerHeight = getHeight(topLeftCornerImage);
int topRightCornerWidth = getWidth(topRightCornerImage);
int topRightCornerHeight = getHeight(topRightCornerImage);
int bottomLeftCornerWidth = getWidth(bottomLeftCornerImage);
int bottomLeftCornerHeight = getHeight(bottomLeftCornerImage);
int bottomRightCornerWidth = getWidth(bottomRightCornerImage);
int bottomRightCornerHeight = getHeight(bottomRightCornerImage);
final int topHeight = getHeight(topImage);
final int leftWidth = getWidth(leftImage);
final int bottomHeight = getHeight(bottomImage);
final int rightWidth = getWidth(rightImage);
final int topStartWidth = getWidth(topStartImage);
final int topStartHeight = getHeight(topStartImage);
final int topEndWidth = getWidth(topEndImage);
final int topEndHeight = getHeight(topEndImage);
final int leftStartWidth = getWidth(leftStartImage);
final int leftStartHeight = getHeight(leftStartImage);
final int leftEndWidth = getWidth(leftEndImage);
final int leftEndHeight = getHeight(leftEndImage);
final int bottomStartWidth = getWidth(bottomStartImage);
final int bottomStartHeight = getHeight(bottomStartImage);
final int bottomEndWidth = getWidth(bottomEndImage);
final int bottomEndHeight = getHeight(bottomEndImage);
final int rightStartWidth = getWidth(rightStartImage);
final int rightStartHeight = getHeight(rightStartImage);
final int rightEndWidth = getWidth(rightEndImage);
final int rightEndHeight = getHeight(rightEndImage);
final int topLeftCornerWidth = getWidth(topLeftCornerImage);
final int topLeftCornerHeight = getHeight(topLeftCornerImage);
final int topRightCornerWidth = getWidth(topRightCornerImage);
final int topRightCornerHeight = getHeight(topRightCornerImage);
final int bottomLeftCornerWidth = getWidth(bottomLeftCornerImage);
final int bottomLeftCornerHeight = getHeight(bottomLeftCornerImage);
final int bottomRightCornerWidth = getWidth(bottomRightCornerImage);
final int bottomRightCornerHeight = getHeight(bottomRightCornerImage);
// Add the image border
if (topStartImage != null) {
final int w = Math.min(topStartWidth, width - topLeftCornerWidth - topRightCornerWidth);
fillTexture(g2, topStartImage,
x + topLeftCornerWidth,
y + insets.top - topStartHeight,
w,
topStartHeight);
}
if (topImage != null) {
fillTexture(g2, topImage,
x + topLeftCornerWidth,
x + topLeftCornerWidth + topStartWidth,
y + insets.top - topHeight,
width - topLeftCornerWidth - topRightCornerWidth,
width - topLeftCornerWidth - topRightCornerWidth - topStartWidth - topEndWidth,
topHeight);
}
if(leftImage != null) {
if (topEndImage != null) {
final int w = Math.min(topEndWidth, width - topLeftCornerWidth - topRightCornerWidth - topStartWidth);
fillTexture(g2, topEndImage,
x + width - Math.max(insets.right, topRightCornerWidth) - w,
y + insets.top - topEndHeight,
w,
topEndHeight);
}
if (leftStartImage != null) {
final int h = Math.min(leftStartHeight, height - Math.max(insets.top, topLeftCornerHeight) - bottomLeftCornerHeight);
fillTexture(g2, leftStartImage,
x + insets.left - leftStartWidth,
y + Math.max(insets.top, topLeftCornerHeight),
leftStartWidth,
h);
}
if (leftImage != null) {
fillTexture(g2, leftImage,
x + insets.left - leftWidth,
y + topLeftCornerHeight,
y + Math.max(insets.top, topLeftCornerHeight) + leftStartHeight,
leftWidth,
height - topLeftCornerHeight - bottomLeftCornerHeight);
height - Math.max(insets.top, topLeftCornerHeight) - Math.max(insets.bottom, bottomLeftCornerHeight) - leftStartHeight - leftEndHeight);
}
if (leftEndImage != null) {
final int h = Math.min(leftEndHeight, height - topLeftCornerHeight - bottomLeftCornerHeight - leftStartHeight);
fillTexture(g2, leftEndImage,
x + insets.left - leftEndWidth,
y + height - Math.max(insets.bottom, bottomLeftCornerHeight) - h,
leftEndWidth,
h);
}
if (bottomStartImage != null) {
final int w = Math.min(bottomStartWidth, width - bottomLeftCornerWidth - bottomRightCornerWidth);
fillTexture(g2, bottomStartImage,
x + bottomLeftCornerWidth,
y + height - insets.bottom,
w,
bottomStartHeight);
}
if (bottomImage != null) {
fillTexture(g2, bottomImage,
x + bottomLeftCornerWidth,
x + bottomLeftCornerWidth + bottomStartWidth,
y + height - insets.bottom,
width - bottomLeftCornerWidth - bottomRightCornerWidth,
width - bottomLeftCornerWidth - bottomRightCornerWidth - bottomStartWidth - bottomEndWidth,
bottomHeight);
}
if (bottomEndImage != null) {
final int w = Math.min(bottomEndWidth, width - bottomLeftCornerWidth - bottomRightCornerWidth - bottomStartWidth);
fillTexture(g2, bottomEndImage,
x + width - Math.max(insets.right, bottomRightCornerWidth) - w,
y + height - insets.bottom,
w,
bottomEndHeight);
}
if (rightStartImage != null) {
final int h = Math.min(rightStartHeight, height - Math.max(insets.top, topRightCornerHeight) - bottomRightCornerHeight);
fillTexture(g2, rightStartImage,
x + width - insets.right,
y + Math.max(insets.top, topRightCornerHeight),
rightStartWidth,
h);
}
if (rightImage != null) {
fillTexture(g2, rightImage,
x + width - insets.right,
y + topRightCornerHeight,
y + Math.max(insets.top, topRightCornerHeight) + rightStartHeight,
rightWidth,
height - topRightCornerHeight - bottomRightCornerHeight);
height - Math.max(insets.top, topRightCornerHeight) - bottomRightCornerHeight - rightStartHeight - rightEndHeight);
}
if (rightEndImage != null) {
final int h = Math.min(rightEndHeight, height - topRightCornerHeight - bottomRightCornerHeight - rightStartHeight);
fillTexture(g2, rightEndImage,
x + width - insets.right,
y + height - Math.max(insets.bottom, bottomRightCornerHeight) - h,
rightEndWidth,
h);
}
if (topLeftCornerImage != null) {
fillTexture(g2, topLeftCornerImage,