gui: cover scale style option for background images (#14049)

This commit is contained in:
Tuomas-Matti Soikkeli 2025-11-08 21:59:45 +02:00 committed by GitHub
parent 7e34363954
commit 1327fe2b99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 2 deletions

View file

@ -82,7 +82,7 @@ public class ThemePluginImpl implements ThemePlugin {
}
if (ui.containsKey("gamePanel") && ui.containsKey("jLayeredPane")) {
ImagePanel bgPanel = new ImagePanel(backgroundImage, ImagePanelStyle.TILED);
ImagePanel bgPanel = new ImagePanel(backgroundImage, ImagePanelStyle.COVER);
// TODO: research - is all components used? And why it make transparent?
unsetOpaque(ui.get("splitChatAndLogs"));

View file

@ -78,6 +78,9 @@ public class ImagePanel extends JPanel {
case ACTUAL:
drawActual(g);
break;
case COVER:
drawCover(g);
break;
}
}
@ -99,4 +102,25 @@ public class ImagePanel extends JPanel {
float y = (d.height - image.getHeight(null)) * alignmentY;
g.drawImage(image, (int) x, (int) y, this);
}
private void drawCover(Graphics g) {
Dimension d = getSize();
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
// Calculate scale to cover the entire panel while maintaining aspect ratio
double scaleX = (double) d.width / imageWidth;
double scaleY = (double) d.height / imageHeight;
double scale = Math.max(scaleX, scaleY);
// Calculate the scaled dimensions
int scaledWidth = (int) (imageWidth * scale);
int scaledHeight = (int) (imageHeight * scale);
// Center the image
int x = (d.width - scaledWidth) / 2;
int y = (d.height - scaledHeight) / 2;
g.drawImage(image, x, y, scaledWidth, scaledHeight, null);
}
}

View file

@ -4,5 +4,5 @@ package mage.components;
* Created by IGOUDT on 7-3-2017.
*/
public enum ImagePanelStyle {
TILED, SCALED, ACTUAL
TILED, SCALED, ACTUAL, COVER
}