1 | package utils;
|
---|
2 |
|
---|
3 | import java.awt.Graphics2D;
|
---|
4 | import java.awt.image.BufferedImageOp;
|
---|
5 | import java.awt.Composite;
|
---|
6 | import java.awt.AlphaComposite;
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.image.ImageObserver;
|
---|
9 | import java.awt.Image;
|
---|
10 | import javax.imageio.ImageIO;
|
---|
11 | import java.awt.image.BufferedImage;
|
---|
12 | import java.awt.FontFormatException;
|
---|
13 | import java.io.File;
|
---|
14 | import java.awt.Font;
|
---|
15 | import java.io.IOException;
|
---|
16 | import java.io.FileReader;
|
---|
17 | import java.io.Reader;
|
---|
18 | import java.io.InputStreamReader;
|
---|
19 | import java.io.BufferedReader;
|
---|
20 | import java.awt.DisplayMode;
|
---|
21 | import java.awt.GraphicsDevice;
|
---|
22 | import java.util.Date;
|
---|
23 | import java.text.SimpleDateFormat;
|
---|
24 | import java.awt.GraphicsConfiguration;
|
---|
25 |
|
---|
26 | public class Utils {
|
---|
27 | private static boolean RUNNING_FROM_JAR;
|
---|
28 | private static Utils classLoaderReference;
|
---|
29 | public static GraphicsConfiguration gc;
|
---|
30 |
|
---|
31 | static {
|
---|
32 | Utils.classLoaderReference = null;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public static void init(final GraphicsConfiguration gc, final boolean jar) {
|
---|
36 | Utils.gc = gc;
|
---|
37 | Utils.RUNNING_FROM_JAR = jar;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static String dateString() {
|
---|
41 | return new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date());
|
---|
42 | }
|
---|
43 |
|
---|
44 | private static DisplayMode getBestDisplayMode(final GraphicsDevice device, final int xRes, final int yRes) {
|
---|
45 | final DisplayMode[] BEST_DISPLAY_MODES = { new DisplayMode(xRes, yRes, 32, 0), new DisplayMode(xRes, yRes, 16, 0), new DisplayMode(xRes, yRes, 8, 0) };
|
---|
46 | for (int x = 0; x < BEST_DISPLAY_MODES.length; ++x) {
|
---|
47 | final DisplayMode[] modes = device.getDisplayModes();
|
---|
48 | for (int i = 0; i < modes.length; ++i) {
|
---|
49 | 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()) {
|
---|
50 | return BEST_DISPLAY_MODES[x];
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return null;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static void chooseBestDisplayMode(final GraphicsDevice device, final int xRes, final int yRes) {
|
---|
58 | final DisplayMode best = getBestDisplayMode(device, xRes, yRes);
|
---|
59 | if (best != null) {
|
---|
60 | device.setDisplayMode(best);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static BufferedReader loadTextFile(final String fileName) throws IOException {
|
---|
65 | if (Utils.RUNNING_FROM_JAR) {
|
---|
66 | if (Utils.classLoaderReference == null) {
|
---|
67 | Utils.classLoaderReference = new Utils();
|
---|
68 | }
|
---|
69 | return new BufferedReader(new InputStreamReader(Utils.classLoaderReference.getClass().getResourceAsStream("/" + fileName)));
|
---|
70 | }
|
---|
71 | return new BufferedReader(new FileReader(fileName));
|
---|
72 | }
|
---|
73 |
|
---|
74 | public static Font loadFont(final String fileName) {
|
---|
75 | try {
|
---|
76 | if (Utils.RUNNING_FROM_JAR) {
|
---|
77 | if (Utils.classLoaderReference == null) {
|
---|
78 | Utils.classLoaderReference = new Utils();
|
---|
79 | }
|
---|
80 | return Font.createFont(0, Utils.classLoaderReference.getClass().getResourceAsStream("/" + fileName));
|
---|
81 | }
|
---|
82 | return Font.createFont(0, new File(fileName));
|
---|
83 | }
|
---|
84 | catch (FontFormatException ffe) {
|
---|
85 | ffe.printStackTrace();
|
---|
86 | }
|
---|
87 | catch (IOException ioe) {
|
---|
88 | ioe.printStackTrace();
|
---|
89 | }
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public static BufferedImage loadImg(final String fileName) {
|
---|
94 | try {
|
---|
95 | BufferedImage init;
|
---|
96 | if (Utils.RUNNING_FROM_JAR) {
|
---|
97 | if (Utils.classLoaderReference == null) {
|
---|
98 | Utils.classLoaderReference = new Utils();
|
---|
99 | }
|
---|
100 | init = ImageIO.read(Utils.classLoaderReference.getClass().getResource("/images/" + fileName));
|
---|
101 | }
|
---|
102 | else {
|
---|
103 | init = ImageIO.read(new File("images/" + fileName));
|
---|
104 | }
|
---|
105 | final BufferedImage accelerated = Utils.gc.createCompatibleImage(init.getWidth(), init.getHeight(), 3);
|
---|
106 | accelerated.getGraphics().drawImage(init, 0, 0, null);
|
---|
107 | return accelerated;
|
---|
108 | }
|
---|
109 | catch (IOException ioe) {
|
---|
110 | ioe.printStackTrace();
|
---|
111 | return null;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public static BufferedImage loadTransImg(final String fileName, final Color bgColor, final Color shadowColor) {
|
---|
116 | try {
|
---|
117 | BufferedImage init;
|
---|
118 | if (Utils.RUNNING_FROM_JAR) {
|
---|
119 | init = ImageIO.read(Utils.classLoaderReference.getClass().getResource("/images/" + fileName));
|
---|
120 | }
|
---|
121 | else {
|
---|
122 | init = ImageIO.read(new File("images/" + fileName));
|
---|
123 | }
|
---|
124 | init = makeColorTransparent(init, bgColor, 0);
|
---|
125 | init = makeColorTransparent(init, shadowColor, -1593835520);
|
---|
126 | final BufferedImage accelerated = Utils.gc.createCompatibleImage(init.getWidth(), init.getHeight(), 3);
|
---|
127 | accelerated.getGraphics().drawImage(init, 0, 0, null);
|
---|
128 | return accelerated;
|
---|
129 | }
|
---|
130 | catch (IOException ioe) {
|
---|
131 | ioe.printStackTrace();
|
---|
132 | return null;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public static BufferedImage makeColorTransparent(final BufferedImage image, final Color color, final int newColor) {
|
---|
137 | final BufferedImage dimg = new BufferedImage(image.getWidth(), image.getHeight(), 2);
|
---|
138 | final Graphics2D g = dimg.createGraphics();
|
---|
139 | g.setComposite(AlphaComposite.Src);
|
---|
140 | g.drawImage(image, null, 0, 0);
|
---|
141 | g.dispose();
|
---|
142 | for (int i = 0; i < dimg.getHeight(); ++i) {
|
---|
143 | for (int j = 0; j < dimg.getWidth(); ++j) {
|
---|
144 | if (dimg.getRGB(j, i) == color.getRGB()) {
|
---|
145 | dimg.setRGB(j, i, newColor);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | return dimg;
|
---|
150 | }
|
---|
151 | } |
---|