EOS 2  1.1.0
Einfache Objektbasierte Sprache
GuiToolkit.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.base.util;
2 
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.Font;
6 import java.awt.Image;
7 import java.awt.Toolkit;
8 import java.awt.event.ActionListener;
9 import java.awt.image.BufferedImage;
10 
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;
26 
27 import de.lathanda.eos.base.ResourceLoader;
28 
34 public class GuiToolkit {
41  private static int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
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();
49 
56  public static int mm2pixel(double mm) {
57  return (int) (dpi / 25.4d * mm);
58  }
59 
66  public static double pixel2mm(int pixel) {
67  return pixel * 25.4d / dpi;
68  }
69 
70  static {
71  UIManager.put("ToolTip.font", new FontUIResource(GuiToolkit.createFont(Font.SANS_SERIF, Font.PLAIN, 10)));
72  }
73  public static int getScreenResolution() {
74  return dpi;
75  }
76  public static void setScreenResolution(int dpi) {
77  if (dpi == 0) {
78  dpi = Toolkit.getDefaultToolkit().getScreenResolution();
79  } else {
80  GuiToolkit.dpi = dpi;
81  }
82  }
83  public static double getUnit() {
84  return unit;
85  }
86  public static void setUnit(double unit) {
87  GuiToolkit.unit = unit;
88  }
89  public static Font createFont(String name, int style, int size) {
90  return new Font(name, style, getFontSize(size));
91  }
92 
93  public static int getFontSize(int size) {
94  return dpi * size / 72;
95  }
96 
97  public static int scaledSize(int size) {
98  return dpi * size / 72;
99  }
100  public static Dimension scaledDimension(int width, int height) {
101  return new Dimension(dpi * width / 72, dpi * height / 72);
102  }
103 
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));
108 
109  btn.setIcon(icon);
110  btn.setToolTipText(tooltip);
111  btn.setFocusable(false);
112  btn.addActionListener(action);
113  return btn;
114  }
115  public static JMenu createMenue(String text) {
116  JMenu menu = new JMenu(text);
117  menu.setFont(MENU_FONT);
118  return menu;
119  }
120 
121  public static JMenuItem createMenuItem(String text, String tooltip, ActionListener action) {
122  JMenuItem mit = new JMenuItem();
123  mit.setText(text);
124  if (tooltip != null) {
125  mit.setToolTipText(tooltip);
126  }
127  mit.addActionListener(action);
128  mit.setFont(MENU_ITEM_FONT);
129  return mit;
130  }
131 
132  public static JMenuItem createMenuItem(String text, String tooltip, ActionListener action, int mem) {
133  JMenuItem mit = createMenuItem(text, tooltip, action);
134  mit.setAccelerator(KeyStroke.getKeyStroke(mem, CMD_MASK));
135  return mit;
136  }
137 
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);
144  return slider;
145  }
146  public static TitledBorder createTitledBorder(String title) {
147  return BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), title, TitledBorder.CENTER,
148  TitledBorder.TOP, TITLE_BORDER_FONT);
149  }
150 
151  public static JLabel createLabel(String text) {
152  JLabel label = new JLabel(text);
153  label.setFont(LABEL_FONT);
154  return label;
155  }
156 
157  public static JCheckBox createCheckBox(String label) {
158  JCheckBox checkbox = new JCheckBox(label, false);
159  checkbox.setFont(LABEL_FONT);
160  return checkbox;
161  }
162 
163  public static JTextField createTextField() {
164  JTextField textfield = new JTextField();
165  textfield.setFont(LABEL_FONT);
166  return textfield;
167  }
168  public static <T> JComboBox<T> createComboBox() {
169  JComboBox<T> jcb = new JComboBox<T>();
170  jcb.setFont(LABEL_FONT);
171  return jcb;
172 
173  }
174 
175  public static ImageIcon createSmallIcon(String image) {
176  ImageIcon icon = null;
177  Image i = ResourceLoader.loadResourceImage(image);
178  icon = new ImageIcon(i.getScaledInstance(dpi * 16 / 96, dpi * 16 / 96, Image.SCALE_SMOOTH));
179  return icon;
180  }
181 
182  public static ImageIcon createSmallIcon(BufferedImage image) {
183  ImageIcon icon = new ImageIcon(image.getScaledInstance(dpi * 16 / 96, dpi * 16 / 96, Image.SCALE_SMOOTH));
184  return icon;
185  }
186 }
static BufferedImage loadResourceImage(String name)
static int getFontSize(int size)
Definition: GuiToolkit.java:93
static JLabel createLabel(String text)
static void setUnit(double unit)
Definition: GuiToolkit.java:86
static Dimension scaledDimension(int width, int height)
static double pixel2mm(int pixel)
Definition: GuiToolkit.java:66
static JMenuItem createMenuItem(String text, String tooltip, ActionListener action, int mem)
static JMenu createMenue(String text)
static void setScreenResolution(int dpi)
Definition: GuiToolkit.java:76
static JButton createButton(Image image, String tooltip, ActionListener action)
static ImageIcon createSmallIcon(BufferedImage image)
static TitledBorder createTitledBorder(String title)
static ImageIcon createSmallIcon(String image)
static Font createFont(String name, int style, int size)
Definition: GuiToolkit.java:89
static< T > JComboBox< T > createComboBox()
static JSlider createSlider(String text, ChangeListener change)
static JCheckBox createCheckBox(String label)
static int mm2pixel(double mm)
Definition: GuiToolkit.java:56
static JMenuItem createMenuItem(String text, String tooltip, ActionListener action)
Impressum