Skip to content

Commit 0b25dad

Browse files
committed
Fixed Display of Misc Award Images; Upgraded Award Image Scaling
- Fixed misc image count to return misc images and not medals. - Added logging to warn if image files (ribbons, medals, misc) are missing. - Adjusted the maximum number of ribbon awards per row from 4 to 3 for better display with the new scaling tech. - Introduced `scaleImage` method in `ImageUtilities`. This is essentially a duplicate of `scaleImageIcon`, with reused components separated into a shared method. - Replaced award image scaling in `PersonViewPanel` with the new `scaleImage` method from `ImageUtilities`.
1 parent 7193b9a commit 0b25dad

File tree

2 files changed

+70
-34
lines changed

2 files changed

+70
-34
lines changed

MekHQ/src/mekhq/gui/view/PersonViewPanel.java

+17-31
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static mekhq.campaign.personnel.Person.getLoyaltyName;
3535
import static mekhq.campaign.personnel.turnoverAndRetention.Fatigue.getEffectiveFatigue;
3636
import static mekhq.utilities.ImageUtilities.addTintToImageIcon;
37+
import static mekhq.utilities.ImageUtilities.scaleImage;
3738
import static org.jfree.chart.ChartColor.DARK_BLUE;
3839
import static org.jfree.chart.ChartColor.DARK_RED;
3940

@@ -94,8 +95,6 @@
9495
import mekhq.gui.utilities.MarkdownRenderer;
9596
import mekhq.gui.utilities.WrapLayout;
9697

97-
import static megamek.client.ui.WrapLayout.wordWrap;
98-
9998
/**
10099
* A custom panel that gets filled in with goodies from a Person record
101100
*
@@ -104,7 +103,7 @@
104103
public class PersonViewPanel extends JScrollablePanel {
105104
private static final MMLogger logger = MMLogger.create(PersonViewPanel.class);
106105

107-
private static final int MAX_NUMBER_OF_RIBBON_AWARDS_PER_ROW = 4;
106+
private static final int MAX_NUMBER_OF_RIBBON_AWARDS_PER_ROW = 3;
108107

109108
private final CampaignGUI gui;
110109

@@ -408,13 +407,16 @@ private Box drawRibbons() {
408407
int awardTierCount = getAwardTierCount(award, maximumTiers);
409408

410409
String ribbonFileName = award.getRibbonFileName(awardTierCount);
410+
String directory = award.getSet() + "/ribbons/";
411411

412-
ribbon = (Image) MHQStaticDirectoryManager.getAwardIcons()
413-
.getItem(award.getSet() + "/ribbons/", ribbonFileName);
412+
ribbon = (Image) MHQStaticDirectoryManager.getAwardIcons().getItem(directory, ribbonFileName);
414413
if (ribbon == null) {
414+
logger.warn("No ribbon icon found for award: {}", directory + ribbonFileName);
415415
continue;
416416
}
417-
ribbon = ribbon.getScaledInstance(25, 8, Image.SCALE_DEFAULT);
417+
418+
ribbon = scaleImage(ribbon, 8, false);
419+
418420
ribbonLabel.setIcon(new ImageIcon(ribbon));
419421
ribbonLabel.setToolTipText(award.getTooltip(campaign.getCampaignOptions(), person));
420422
rowRibbonsBox.add(ribbonLabel, 0);
@@ -481,23 +483,15 @@ private JPanel drawMedals() {
481483
int awardTierCount = getAwardTierCount(award, maximumTiers);
482484

483485
String medalFileName = award.getMedalFileName(awardTierCount);
486+
String directory = award.getSet() + "/medals/";
484487

485-
medal = (Image) MHQStaticDirectoryManager.getAwardIcons()
486-
.getItem(award.getSet() + "/medals/", medalFileName);
488+
medal = (Image) MHQStaticDirectoryManager.getAwardIcons().getItem(directory, medalFileName);
487489
if (medal == null) {
490+
logger.warn("No medal icon found for award: {}", directory + medalFileName);
488491
continue;
489492
}
490493

491-
int width = medal.getWidth(null);
492-
int height = medal.getHeight(null);
493-
494-
if (width == height) {
495-
medal = medal.getScaledInstance(40, 40, Image.SCALE_FAST);
496-
} else if (width < height) {
497-
medal = medal.getScaledInstance(20, 40, Image.SCALE_FAST);
498-
} else {
499-
medal = medal.getScaledInstance(40, 20, Image.SCALE_FAST);
500-
}
494+
medal = scaleImage(medal, 40, false);
501495

502496
medalLabel.setIcon(new ImageIcon(medal));
503497
medalLabel.setToolTipText(award.getTooltip(campaign.getCampaignOptions(), person));
@@ -529,27 +523,19 @@ private JPanel drawMiscAwards() {
529523

530524
Image misc;
531525
try {
532-
int maximumTiers = award.getNumberOfMedalFiles();
526+
int maximumTiers = award.getNumberOfMiscFiles();
533527
int awardTierCount = getAwardTierCount(award, maximumTiers);
534528

535529
String miscFileName = award.getMiscFileName(awardTierCount);
530+
String directory = award.getSet() + "/misc/";
536531

537-
misc = (Image) MHQStaticDirectoryManager.getAwardIcons()
538-
.getItem(award.getSet() + "/misc/", miscFileName);
532+
misc = (Image) MHQStaticDirectoryManager.getAwardIcons().getItem(directory, miscFileName);
539533
if (misc == null) {
534+
logger.warn("No misc icon found for award: {}", directory + miscFileName);
540535
continue;
541536
}
542537

543-
int width = misc.getWidth(null);
544-
int height = misc.getHeight(null);
545-
546-
if (width == height) {
547-
misc = misc.getScaledInstance(40, 40, Image.SCALE_FAST);
548-
} else if (width < height) {
549-
misc = misc.getScaledInstance(20, 40, Image.SCALE_FAST);
550-
} else {
551-
misc = misc.getScaledInstance(40, 20, Image.SCALE_FAST);
552-
}
538+
misc = scaleImage(misc, 40, false);
553539

554540
miscLabel.setIcon(new ImageIcon(misc));
555541
miscLabel.setToolTipText(award.getTooltip(campaign.getCampaignOptions(), person));

MekHQ/src/mekhq/utilities/ImageUtilities.java

+53-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,57 @@ public static ImageIcon scaleImageIcon(ImageIcon icon, int size, boolean scaleBy
9393
width = (int) Math.ceil((double) height * icon.getIconWidth() / icon.getIconHeight());
9494
}
9595

96+
// Create a new BufferedImage with the desired dimensions
97+
BufferedImage scaledImage = getBufferedImage(icon.getImage(), width, height);
98+
99+
return new ImageIcon(scaledImage);
100+
}
101+
102+
/**
103+
* Scales an {@link Image} proportionally based on either the specified width or height.
104+
*
105+
* <p>This method preserves the aspect ratio of the original image while resizing. The size to scale
106+
* is determined by the {@code size} parameter, and whether scaling is based on width or height is controlled by the
107+
* {@code scaleByWidth} flag.</p>
108+
*
109+
* <p>If the provided {@link Image} is {@code null}, an empty {@link Image} will be returned, and
110+
* an error will be logged.</p>
111+
*
112+
* @param image The {@link Image} to be scaled. If {@code null}, an empty {@link Image} is returned.
113+
* @param size The target size to scale to, either width or height depending on the {@code scaleByWidth}
114+
* flag. This value will be scaled for the GUI using {@link UIUtil#scaleForGUI(int)}.
115+
* @param scaleByWidth A {@code boolean} flag to determine the scaling mode:
116+
* <ul>
117+
* <li>If {@code true}, scales the image by the given width, and calculates the height
118+
* proportionally.</li>
119+
* <li>If {@code false}, scales the image by the given height, and calculates the width
120+
* proportionally.</li>
121+
* </ul>
122+
*
123+
* @return A scaled {@link Image}, resized to the specified target dimension while maintaining the aspect ratio. If
124+
* the provided {@link Image} is {@code null}, returns an empty {@link Image}.
125+
*/
126+
public static Image scaleImage(Image image, int size, boolean scaleByWidth) {
127+
if (image == null) {
128+
logger.error(new NullPointerException(),
129+
"Image is null in scaleImage(Image, int, boolean). Returning a placeholder image.");
130+
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
131+
}
132+
133+
int width, height;
134+
135+
if (scaleByWidth) {
136+
width = Math.max(1, UIUtil.scaleForGUI(size));
137+
height = (int) Math.ceil((double) width * image.getHeight(null) / image.getWidth(null));
138+
} else {
139+
height = Math.max(1, UIUtil.scaleForGUI(size));
140+
width = (int) Math.ceil((double) height * image.getWidth(null) / image.getHeight(null));
141+
}
142+
143+
return getBufferedImage(image, width, height);
144+
}
145+
146+
private static BufferedImage getBufferedImage(Image image, int width, int height) {
96147
// Create a new BufferedImage with the desired dimensions
97148
BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
98149

@@ -103,10 +154,9 @@ public static ImageIcon scaleImageIcon(ImageIcon icon, int size, boolean scaleBy
103154
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
104155

105156
// Draw the scaled image with high-quality rendering
106-
g2d.drawImage(icon.getImage(), 0, 0, width, height, null);
157+
g2d.drawImage(image, 0, 0, width, height, null);
107158
g2d.dispose();
108-
109-
return new ImageIcon(scaledImage);
159+
return scaledImage;
110160
}
111161

112162
/**

0 commit comments

Comments
 (0)