mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 02:30:08 -08:00
gui: cover scale style option for background images (#14049)
This commit is contained in:
parent
7e34363954
commit
1327fe2b99
3 changed files with 26 additions and 2 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ package mage.components;
|
|||
* Created by IGOUDT on 7-3-2017.
|
||||
*/
|
||||
public enum ImagePanelStyle {
|
||||
TILED, SCALED, ACTUAL
|
||||
TILED, SCALED, ACTUAL, COVER
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue