Use the same JLabel Logo on all Swing windows

This commit is contained in:
Grische
2022-03-14 11:29:28 +00:00
committed by harlekin
parent 1005b4afc4
commit 7b0a8448de
3 changed files with 57 additions and 40 deletions

View File

@@ -32,10 +32,12 @@ import com.sheepit.client.standalone.swing.activity.Settings;
import com.sheepit.client.standalone.swing.activity.Working; import com.sheepit.client.standalone.swing.activity.Working;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@@ -44,10 +46,14 @@ import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import java.awt.AWTException; import java.awt.AWTException;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.Image; import java.awt.Image;
import java.awt.MenuItem; import java.awt.MenuItem;
import java.awt.PopupMenu; import java.awt.PopupMenu;
import java.awt.RenderingHints;
import java.awt.SystemTray; import java.awt.SystemTray;
import java.awt.TrayIcon; import java.awt.TrayIcon;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@@ -58,11 +64,58 @@ import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.net.URL; import java.net.URL;
import java.util.Objects;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
public class GuiSwing extends JFrame implements Gui { public class GuiSwing extends JFrame implements Gui {
public static final String type = "swing"; public static final String type = "swing";
private static final String logoPath = "/sheepit-logo.png";
public static void drawVersionStringOnImage(final BufferedImage image, String versionString) {
var watermarkWidth = image.getWidth();
var watermarkHeight = image.getHeight();
Graphics2D gph = (Graphics2D) image.getGraphics();
// use anti aliasing to smooth version string
gph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
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();
}
@NotNull public static JLabel createLogoWithWatermark() {
final URL logoURL = GuiSwing.class.getResource(logoPath);
// If logo cannot be found, return dummy image
if (logoURL == null) {
System.err.println("Error: Unable to find logo " + logoPath);
return new JLabel();
}
JLabel labelImage;
try {
// Include the version of the app as a watermark in the SheepIt logo.
final BufferedImage watermark = ImageIO.read(logoURL);
String versionString = "v" + Configuration.jarVersion;
drawVersionStringOnImage(watermark, versionString);
labelImage = new JLabel(new ImageIcon(watermark));
}
catch (Exception e) {
// If something fails, we just show the SheepIt logo (without any watermark)
ImageIcon image = new ImageIcon(logoURL);
labelImage = new JLabel(image);
}
return labelImage;
}
public enum ActivityType { public enum ActivityType {
WORKING, SETTINGS WORKING, SETTINGS

View File

@@ -19,8 +19,6 @@
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.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.GridLayout; import java.awt.GridLayout;
@@ -28,19 +26,16 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
import javax.swing.ButtonGroup; import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
@@ -135,20 +130,7 @@ public class Settings implements Activity {
GridBagConstraints constraints = new GridBagConstraints(); GridBagConstraints constraints = new GridBagConstraints();
int currentRow = 0; int currentRow = 0;
JLabel labelImage; JLabel labelImage = GuiSwing.createLogoWithWatermark();
try {
// Include the version of the app as a watermark in the SheepIt logo.
final BufferedImage watermark = ImageIO.read(getClass().getResource("/sheepit-logo.png"));
String versionString = "v" + Configuration.jarVersion;
drawVersionStringOnImage(watermark, versionString);
labelImage = new JLabel(new ImageIcon(watermark));
}
catch (Exception e) {
// If something fails, we just show the SheepIt logo (without any watermark)
ImageIcon image = new ImageIcon(getClass().getResource("/sheepit-logo.png"));
labelImage = new JLabel(image);
}
constraints.fill = GridBagConstraints.CENTER; constraints.fill = GridBagConstraints.CENTER;
@@ -558,23 +540,6 @@ public class Settings implements Activity {
} }
} }
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() {

View File

@@ -231,8 +231,7 @@ public class Working implements Activity {
last_frame_panel.add(lastRenderTime); last_frame_panel.add(lastRenderTime);
last_frame_panel.add(lastRender); last_frame_panel.add(lastRender);
ImageIcon image = new ImageIcon(getClass().getResource("/sheepit-logo.png")); JLabel labelImage = GuiSwing.createLogoWithWatermark();
JLabel labelImage = new JLabel(image);
labelImage.setAlignmentX(Component.CENTER_ALIGNMENT); labelImage.setAlignmentX(Component.CENTER_ALIGNMENT);
parent.getContentPanel().add(labelImage); parent.getContentPanel().add(labelImage);