Draw version string correctly on the watermark

This commit is contained in:
Grische
2022-02-20 16:11:50 +01:00
parent aef9ccc034
commit 941ec22f8a

View File

@@ -19,6 +19,7 @@
package com.sheepit.client.standalone.swing.activity; package com.sheepit.client.standalone.swing.activity;
import java.awt.FontMetrics;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
@@ -138,12 +139,9 @@ public class Settings implements Activity {
try { try {
// Include the version of the app as a watermark in the SheepIt logo. // Include the version of the app as a watermark in the SheepIt logo.
final BufferedImage watermark = ImageIO.read(getClass().getResource("/sheepit-logo.png")); final BufferedImage watermark = ImageIO.read(getClass().getResource("/sheepit-logo.png"));
String versionString = "v" + Configuration.jarVersion;
Graphics gph = watermark.getGraphics(); drawVersionStringOnImage(watermark, versionString);
gph.setFont(gph.getFont().deriveFont(12f));
gph.drawString("v" + Configuration.jarVersion, 335, 120);
gph.dispose();
labelImage = new JLabel(new ImageIcon(watermark)); labelImage = new JLabel(new ImageIcon(watermark));
} }
catch (Exception e) { catch (Exception e) {
@@ -559,7 +557,24 @@ public class Settings implements Activity {
new SaveAction().actionPerformed(null); new SaveAction().actionPerformed(null);
} }
} }
private void drawVersionStringOnImage(final BufferedImage image, String versionString) {
var watermarkWidth = image.getWidth();
var watermarkHeight = image.getHeight();
Graphics gph = image.getGraphics();
gph.setFont(gph.getFont().deriveFont(12f));
FontMetrics fontMetrics = gph.getFontMetrics();
// move text to the very right of the image respecting its length
int x = watermarkWidth - fontMetrics.stringWidth(versionString);
// the bottom of the image, but give enough headroom for descending letters like g
int y = watermarkHeight - fontMetrics.getMaxDescent();
gph.drawString(versionString, x, y);
gph.dispose();
}
public void resizeWindow() {} public void resizeWindow() {}
public boolean checkDisplaySaveButton() { public boolean checkDisplaySaveButton() {