1 | package utils;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import java.awt.image.*;
|
---|
5 | import java.io.*;
|
---|
6 | import java.text.*;
|
---|
7 | import java.util.*;
|
---|
8 | import javax.imageio.*;
|
---|
9 |
|
---|
10 | public class Utils {
|
---|
11 | private static boolean RUNNING_FROM_JAR;
|
---|
12 | private static Utils classLoaderReference = null;
|
---|
13 | public static GraphicsConfiguration gc;
|
---|
14 |
|
---|
15 | //this constructor should never be called and is only used
|
---|
16 | //internally for getting a reference to classLoader
|
---|
17 | public Utils() {
|
---|
18 |
|
---|
19 | }
|
---|
20 |
|
---|
21 | public static void init(GraphicsConfiguration gc, boolean jar) {
|
---|
22 | Utils.gc = gc;
|
---|
23 | Utils.RUNNING_FROM_JAR = jar;
|
---|
24 | }
|
---|
25 |
|
---|
26 | public static String dateString() {
|
---|
27 | return new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date());
|
---|
28 | }
|
---|
29 |
|
---|
30 | private static DisplayMode getBestDisplayMode(GraphicsDevice device, int xRes, int yRes) {
|
---|
31 | DisplayMode[] BEST_DISPLAY_MODES = new DisplayMode[] {
|
---|
32 | new DisplayMode(xRes, yRes, 32, 0), new DisplayMode(xRes, yRes, 16, 0),
|
---|
33 | new DisplayMode(xRes, yRes, 8, 0) };
|
---|
34 |
|
---|
35 | for (int x = 0; x < BEST_DISPLAY_MODES.length; x++) {
|
---|
36 | DisplayMode[] modes = device.getDisplayModes();
|
---|
37 | for (int i = 0; i < modes.length; i++) {
|
---|
38 | if(modes[i].getWidth() == BEST_DISPLAY_MODES[x].getWidth() && modes[i].getHeight() == BEST_DISPLAY_MODES[x].getHeight() && modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth())
|
---|
39 | return BEST_DISPLAY_MODES[x];
|
---|
40 | }
|
---|
41 | }
|
---|
42 | return null;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public static void chooseBestDisplayMode(GraphicsDevice device, int xRes, int yRes) {
|
---|
46 | DisplayMode best = getBestDisplayMode(device, xRes, yRes);
|
---|
47 | if (best != null)
|
---|
48 | device.setDisplayMode(best);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public static BufferedReader loadTextFile(String fileName) throws IOException {
|
---|
52 | if(RUNNING_FROM_JAR) {
|
---|
53 | if(classLoaderReference == null)
|
---|
54 | classLoaderReference = new Utils();
|
---|
55 |
|
---|
56 | return new BufferedReader(new InputStreamReader(classLoaderReference.getClass().getResourceAsStream("/"+fileName)));
|
---|
57 | }else
|
---|
58 | return new BufferedReader(new FileReader(fileName));
|
---|
59 | }
|
---|
60 |
|
---|
61 | public static Font loadFont(String fileName) {
|
---|
62 | try {
|
---|
63 | if(RUNNING_FROM_JAR) {
|
---|
64 | if(classLoaderReference == null)
|
---|
65 | classLoaderReference = new Utils();
|
---|
66 |
|
---|
67 | return Font.createFont(Font.TRUETYPE_FONT, classLoaderReference.getClass().getResourceAsStream("/"+fileName));
|
---|
68 | }else
|
---|
69 | return Font.createFont(Font.TRUETYPE_FONT, new File(fileName));
|
---|
70 | }catch(FontFormatException ffe) {
|
---|
71 | ffe.printStackTrace();
|
---|
72 | }catch(IOException ioe) {
|
---|
73 | ioe.printStackTrace();
|
---|
74 | }
|
---|
75 |
|
---|
76 | return null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public static BufferedImage loadImg(String fileName) {
|
---|
80 | BufferedImage init;
|
---|
81 | BufferedImage accelerated;
|
---|
82 |
|
---|
83 | try {
|
---|
84 | if(RUNNING_FROM_JAR) {
|
---|
85 | if(classLoaderReference == null)
|
---|
86 | classLoaderReference = new Utils();
|
---|
87 |
|
---|
88 | init = ImageIO.read(classLoaderReference.getClass().getResource("/images/" + fileName));
|
---|
89 | }else
|
---|
90 | init = ImageIO.read(new File("images/" + fileName));
|
---|
91 |
|
---|
92 | accelerated = gc.createCompatibleImage(init.getWidth(), init.getHeight(), Transparency.TRANSLUCENT);
|
---|
93 | accelerated.getGraphics().drawImage(init, 0, 0, null);
|
---|
94 |
|
---|
95 | return accelerated;
|
---|
96 | }catch(IOException ioe) {
|
---|
97 | ioe.printStackTrace();
|
---|
98 | }
|
---|
99 |
|
---|
100 | return null;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public static BufferedImage loadTransImg(String fileName, Color bgColor, Color shadowColor) {
|
---|
104 | BufferedImage init;
|
---|
105 | BufferedImage accelerated;
|
---|
106 |
|
---|
107 | try {
|
---|
108 | if(RUNNING_FROM_JAR)
|
---|
109 | init = ImageIO.read(classLoaderReference.getClass().getResource("/images/" + fileName));
|
---|
110 | else
|
---|
111 | init = ImageIO.read(new File("images/" + fileName));
|
---|
112 |
|
---|
113 | init = makeColorTransparent(init, bgColor, 0x0);
|
---|
114 | init = makeColorTransparent(init, shadowColor, 0xA1000000);
|
---|
115 |
|
---|
116 | accelerated = gc.createCompatibleImage(init.getWidth(), init.getHeight(), Transparency.TRANSLUCENT);
|
---|
117 | accelerated.getGraphics().drawImage(init, 0, 0, null);
|
---|
118 |
|
---|
119 | return accelerated;
|
---|
120 | }catch(IOException ioe) {
|
---|
121 | ioe.printStackTrace();
|
---|
122 | }
|
---|
123 |
|
---|
124 | return null;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static BufferedImage makeColorTransparent(BufferedImage image, Color color, int newColor) {
|
---|
128 | BufferedImage dimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
---|
129 |
|
---|
130 | Graphics2D g = dimg.createGraphics();
|
---|
131 | g.setComposite(AlphaComposite.Src);
|
---|
132 | g.drawImage(image, null, 0, 0);
|
---|
133 | g.dispose();
|
---|
134 | for(int i = 0; i < dimg.getHeight(); i++) {
|
---|
135 | for(int j = 0; j < dimg.getWidth(); j++) {
|
---|
136 | if(dimg.getRGB(j, i) == color.getRGB()) {
|
---|
137 | dimg.setRGB(j, i, newColor);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return dimg;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public static BufferedImage brightenImage(BufferedImage image) {
|
---|
145 | BufferedImage dimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
---|
146 |
|
---|
147 | Graphics2D g = dimg.createGraphics();
|
---|
148 | g.setComposite(AlphaComposite.Src);
|
---|
149 | g.drawImage(image, null, 0, 0);
|
---|
150 | g.dispose();
|
---|
151 | Color c, newC;
|
---|
152 | for(int i = 0; i < dimg.getHeight(); i++) {
|
---|
153 | for(int j = 0; j < dimg.getWidth(); j++) {
|
---|
154 | c = new Color(dimg.getRGB(j, i), true);
|
---|
155 | newC = c.brighter().brighter();
|
---|
156 | dimg.setRGB(j, i, new Color(newC.getRed(), newC.getGreen(), newC.getBlue(), c.getAlpha()).getRGB());
|
---|
157 | }
|
---|
158 | }
|
---|
159 | return dimg;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public static int getRack(int loc) {
|
---|
163 | return (loc >> 24) & 0xFF;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public static int getSection(int loc) {
|
---|
167 | return (loc >> 16) & 0xFF;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public static int getColumn(int loc) {
|
---|
171 | return (loc >> 8) & 0xFF;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public static int getRow(int loc) {
|
---|
175 | return loc & 0xFF;
|
---|
176 | }
|
---|
177 |
|
---|
178 | public static int getLoc(int rack, int section, int column, int row) {
|
---|
179 | return (rack << 24) + (section << 16) + (column << 8) + row;
|
---|
180 | }
|
---|
181 | }
|
---|