From 941ec22f8ae8e8feef77a31a72ff4b3c0600190c Mon Sep 17 00:00:00 2001 From: Grische Date: Sun, 20 Feb 2022 16:11:50 +0100 Subject: [PATCH] Draw version string correctly on the watermark --- .../standalone/swing/activity/Settings.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/com/sheepit/client/standalone/swing/activity/Settings.java b/src/com/sheepit/client/standalone/swing/activity/Settings.java index 12bdf31..8cadbdf 100644 --- a/src/com/sheepit/client/standalone/swing/activity/Settings.java +++ b/src/com/sheepit/client/standalone/swing/activity/Settings.java @@ -19,6 +19,7 @@ package com.sheepit.client.standalone.swing.activity; +import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; @@ -138,12 +139,9 @@ public class Settings implements Activity { try { // Include the version of the app as a watermark in the SheepIt logo. final BufferedImage watermark = ImageIO.read(getClass().getResource("/sheepit-logo.png")); - - Graphics gph = watermark.getGraphics(); - gph.setFont(gph.getFont().deriveFont(12f)); - gph.drawString("v" + Configuration.jarVersion, 335, 120); - gph.dispose(); - + String versionString = "v" + Configuration.jarVersion; + drawVersionStringOnImage(watermark, versionString); + labelImage = new JLabel(new ImageIcon(watermark)); } catch (Exception e) { @@ -559,7 +557,24 @@ public class Settings implements Activity { 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 boolean checkDisplaySaveButton() {