1 package de.lathanda.eos.base.util;
4 import java.awt.Dimension;
7 import java.awt.Toolkit;
8 import java.awt.event.ActionListener;
9 import java.awt.image.BufferedImage;
11 import javax.swing.BorderFactory;
12 import javax.swing.ImageIcon;
13 import javax.swing.JButton;
14 import javax.swing.JCheckBox;
15 import javax.swing.JComboBox;
16 import javax.swing.JLabel;
17 import javax.swing.JMenu;
18 import javax.swing.JMenuItem;
19 import javax.swing.JSlider;
20 import javax.swing.JTextField;
21 import javax.swing.KeyStroke;
22 import javax.swing.UIManager;
23 import javax.swing.border.TitledBorder;
24 import javax.swing.event.ChangeListener;
25 import javax.swing.plaf.FontUIResource;
27 import de.lathanda.eos.base.ResourceLoader;
42 private static double unit = 1;
43 private static final Font MENU_ITEM_FONT =
createFont(Font.SANS_SERIF, Font.PLAIN, 10);
44 private static final Font MENU_FONT =
createFont(Font.SANS_SERIF, Font.PLAIN, 10);
45 private static final Font LABEL_FONT =
createFont(Font.SANS_SERIF, Font.PLAIN, 10);
46 private static final Font SLIDER_FONT =
createFont(Font.SANS_SERIF, Font.PLAIN, 8);
47 private static final Font TITLE_BORDER_FONT =
createFont(Font.SANS_SERIF, Font.PLAIN, 8);
48 private static final int CMD_MASK = Toolkit.getDefaultToolkit(). getMenuShortcutKeyMaskEx();
57 return (
int) (dpi / 25.4d * mm);
67 return pixel * 25.4d / dpi;
71 UIManager.put(
"ToolTip.font",
new FontUIResource(
GuiToolkit.
createFont(Font.SANS_SERIF, Font.PLAIN, 10)));
78 dpi = Toolkit.getDefaultToolkit().getScreenResolution();
89 public static Font
createFont(String name,
int style,
int size) {
94 return dpi * size / 72;
98 return dpi * size / 72;
101 return new Dimension(dpi * width / 72, dpi * height / 72);
104 public static JButton
createButton(Image image, String tooltip, ActionListener action) {
105 JButton btn =
new JButton();
106 ImageIcon icon =
null;
107 icon =
new ImageIcon(image.getScaledInstance(dpi * 40 / 96, dpi * 40 / 96, Image.SCALE_SMOOTH));
110 btn.setToolTipText(tooltip);
111 btn.setFocusable(
false);
112 btn.addActionListener(action);
116 JMenu menu =
new JMenu(text);
117 menu.setFont(MENU_FONT);
121 public static JMenuItem
createMenuItem(String text, String tooltip, ActionListener action) {
122 JMenuItem mit =
new JMenuItem();
124 if (tooltip !=
null) {
125 mit.setToolTipText(tooltip);
127 mit.addActionListener(action);
128 mit.setFont(MENU_ITEM_FONT);
132 public static JMenuItem
createMenuItem(String text, String tooltip, ActionListener action,
int mem) {
134 mit.setAccelerator(KeyStroke.getKeyStroke(mem, CMD_MASK));
138 public static JSlider
createSlider(String text, ChangeListener change) {
139 JSlider slider =
new JSlider();
140 slider.setFocusable(
false);
141 slider.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), text, TitledBorder.CENTER,
142 TitledBorder.TOP, SLIDER_FONT));
143 slider.addChangeListener(change);
147 return BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), title, TitledBorder.CENTER,
148 TitledBorder.TOP, TITLE_BORDER_FONT);
152 JLabel label =
new JLabel(text);
153 label.setFont(LABEL_FONT);
158 JCheckBox checkbox =
new JCheckBox(label,
false);
159 checkbox.setFont(LABEL_FONT);
164 JTextField textfield =
new JTextField();
165 textfield.setFont(LABEL_FONT);
169 JComboBox<T> jcb =
new JComboBox<T>();
170 jcb.setFont(LABEL_FONT);
176 ImageIcon icon =
null;
178 icon =
new ImageIcon(i.getScaledInstance(dpi * 16 / 96, dpi * 16 / 96, Image.SCALE_SMOOTH));
183 ImageIcon icon =
new ImageIcon(image.getScaledInstance(dpi * 16 / 96, dpi * 16 / 96, Image.SCALE_SMOOTH));
static BufferedImage loadResourceImage(String name)