1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2025-01-06 19:18:27 +01:00

Dynamic background image resizing, preserving aspect ratio

This commit is contained in:
TheSwampire 2021-09-30 19:37:23 +02:00
parent cd063304fb
commit 2ffdd4e155

View File

@ -27,8 +27,35 @@ public class FancyBackgroundPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
g.drawImage(background, 0, 0, null);
double multi;
int w, h;
// Calculate Aspect Ratio Multiplier depending on window size
if (this.getHeight() <= this.getWidth()) {
multi = this.getWidth() / (float)background.getWidth(null);
}
else {
multi = this.getHeight() / (float)background.getHeight(null);
}
// Calculate new width and height
w = (int) Math.floor((float)background.getWidth(null) * multi);
h = (int) Math.floor((float)background.getHeight(null) * multi);
// Check if it needs to be switched (eg. in case of a square window)
if (h < this.getHeight() || w < this.getWidth()) {
if (h < this.getHeight()) {
multi = this.getHeight() / (float)background.getHeight(null);
}
else if (w < this.getWidth()) {
multi = this.getWidth() / (float) background.getWidth(null);
}
w = (int) Math.floor((float)background.getWidth(null) * multi);
h = (int) Math.floor((float)background.getHeight(null) * multi);
}
g.drawImage(background, 0, 0, w, h,null);
}
}
}