Skip to content

Commit

Permalink
Add dynamic background image resizing to fancy launcher (SKCraft#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgehog1029 authored Dec 12, 2021
2 parents 1ae2993 + 2ffdd4e commit 8c9f903
Showing 1 changed file with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,35 @@ public FancyBackgroundPanel() {
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);
}
}

}

0 comments on commit 8c9f903

Please sign in to comment.