Last active
October 20, 2021 02:55
-
-
Save peavers/79b11e1b82f86ff8e6af5f71d1c4d33e to your computer and use it in GitHub Desktop.
Standalone "About" dialog for apps that just run in the task tray.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package space.forloop.stardew.desktop.system; | |
import java.awt.*; | |
import javax.swing.*; | |
import javax.swing.border.EmptyBorder; | |
import lombok.experimental.UtilityClass; | |
import lombok.extern.slf4j.Slf4j; | |
import org.jetbrains.annotations.NotNull; | |
import space.forloop.stardew.desktop.utils.ImageUtils; | |
@Slf4j | |
@UtilityClass | |
public class AboutDialog { | |
private static final Font FONT = new JLabel().getFont(); | |
public void create(final String titleText, final String versionText, final String iconLocation) { | |
final JPanel panel = createPanel(); | |
final JFrame frame = createFrame(); | |
final GridBagConstraints gridBag = createGridBag(); | |
addIcon(getImageIcon(iconLocation), panel, gridBag); | |
addTitle(titleText, panel, gridBag); | |
addVersion(versionText, panel, gridBag); | |
frame.add(panel); | |
frame.pack(); | |
frame.setLocationRelativeTo(null); | |
frame.setAlwaysOnTop(true); | |
frame.setVisible(true); | |
} | |
private void addIcon( | |
final ImageIcon imageIcon, final JPanel panel, final GridBagConstraints gridBag) { | |
final JLabel label = new JLabel("", SwingConstants.CENTER); | |
label.setIcon(imageIcon); | |
gridBag.gridx = 0; | |
gridBag.gridy = 0; | |
panel.add(label, gridBag); | |
} | |
private void addTitle( | |
final String titleText, final JPanel panel, final GridBagConstraints gridBag) { | |
final JLabel label = new JLabel(titleText, SwingConstants.CENTER); | |
label.setFont(new Font(FONT.getName(), Font.PLAIN, 22)); | |
label.setBorder(new EmptyBorder(20, 0, 20, 0)); | |
gridBag.gridx = 0; | |
gridBag.gridy = 1; | |
panel.add(label, gridBag); | |
} | |
private void addVersion( | |
final String versionText, final JPanel panel, final GridBagConstraints gridBag) { | |
final JLabel label = new JLabel(versionText, SwingConstants.CENTER); | |
label.setFont(new Font(FONT.getName(), Font.PLAIN, 12)); | |
gridBag.gridx = 0; | |
gridBag.gridy = 2; | |
panel.add(label, gridBag); | |
} | |
@NotNull | |
private ImageIcon getImageIcon(final String iconLocation) { | |
final Image icon = ImageUtils.loadImageFromResource(iconLocation, "Icon"); | |
final Image scaledInstance = icon.getScaledInstance(124, 124, Image.SCALE_SMOOTH); | |
return new ImageIcon(scaledInstance); | |
} | |
@NotNull | |
private JPanel createPanel() { | |
final JPanel panel = new JPanel(); | |
panel.setLayout(new GridBagLayout()); | |
return panel; | |
} | |
@NotNull | |
private JFrame createFrame() { | |
final JFrame frame = new JFrame("About"); | |
frame.setPreferredSize(new Dimension(300, 300)); | |
frame.setResizable(false); | |
return frame; | |
} | |
@NotNull | |
private GridBagConstraints createGridBag() { | |
final GridBagConstraints gridBagConstraints = new GridBagConstraints(); | |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; | |
return gridBagConstraints; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package space.forloop.stardew.desktop.utils; | |
import java.awt.*; | |
import java.io.IOException; | |
import java.net.URL; | |
import javax.swing.*; | |
import lombok.experimental.UtilityClass; | |
import org.springframework.core.io.ClassPathResource; | |
@UtilityClass | |
public class ImageUtils { | |
public static Image loadImageFromResource(final String path, final String description) { | |
try { | |
final URL imageUrl = new ClassPathResource(path).getURL(); | |
return new ImageIcon(imageUrl, description).getImage(); | |
} catch (final IOException e) { | |
throw new RuntimeException("Unable to load image from: " + path); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment