This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.java
169 lines (148 loc) · 4.95 KB
/
Utility.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Stores stylistic data and methods used throughout the program
*
* Last edit: 6/18/2020
* @author Celeste, Eric
* @version 1.1
* @since 1.0
*/
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.imageio.*;
public final class Utility {
/**
* The width of the frame, in pixels
*/
public static final int FRAME_WIDTH = 800;
/**
* The height of the frame, in pixels
*/
public static final int FRAME_HEIGHT = 500;
/**
* Font for titles
*/
public static final Font TITLE_FONT = loadFont("8-BIT_WONDER.TTF",42);
/**
* Smaller font for titles
*/
public static final Font TITLE_FONT_SMALL = loadFont("8-BIT_WONDER.TTF",32);
/**
* Font for labels, headings, and the like
*/
public static final Font LABEL_FONT = loadFont("DTM-Sans.otf",35);
/**
* Font for descriptions, captions, and the like
*/
public static final Font TEXT_FONT = loadFont("Inconsolata-Medium.ttf",25);
/**
* Font for small captions
*/
public static final Font TEXT_FONT_SMALL = loadFont("Inconsolata-Medium.ttf",15);
/**
* Primary red shade of the graphics
*/
public static final Color RED = new Color(214, 0, 0);
/**
* Primary cyan shade of the graphics
*/
public static final Color CYAN = new Color(233, 255, 251);
/**
* The name of the resources folder
*/
public static final String RES_NAME = "/res";
/**
* Caches loaded images
*/
public static final Map<String, Image> imageLibrary = new HashMap<String, Image>();
/**
* Loads a final static Font object
* @param name the name of the file, including the extension
* @param size the desired font size
* @return a Font object of the given properties, or null if an Exception was thrown
*/
private static final Font loadFont(String name, float size) {
try {
return Font.createFont(Font.TRUETYPE_FONT, Utility.class.getResourceAsStream(RES_NAME+"/fonts/"+name)).deriveFont(size);
} catch (Exception e) {}
return null;
}
/**
* Creates an Image object from a file
* @param name the name of the file, including the extension
* @param width the desired width of the image, in pixels
* @param height the desired height of the image, in pixels
* @return an Image object of the given specifications, or null if an IOException was thrown
*/
public static final Image loadImage(String name, int width, int height) {
String key = name+"-"+width+"-"+height;
if (imageLibrary.containsKey(key)) return imageLibrary.get(key);
try {
Image ret = ImageIO.read(Utility.class.getResourceAsStream(RES_NAME+"/img/"+name)).getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
imageLibrary.put(key, ret);
return ret;
} catch (Exception e) {}
return null;
}
/**
* Changes and updates the frame
* @param drawing the component to be updated to
*/
public static void changeDrawing(JComponent drawing) {
CovidCashier.frame.setContentPane(drawing);
CovidCashier.frame.revalidate();
CovidCashier.frame.repaint();
}
/**
* Changes and updates the frame to the restauarant (which is assumed to be in pastDrawing)
*/
public static void backToRestaurant() {
try {
CovidCashier.getPastRestaurant().activate();
changeDrawing(CovidCashier.getPastRestaurant().getDrawing());
CovidCashier.getPastRestaurant().bgm = new BGM("restaurant");
CovidCashier.getPastRestaurant().bgm.play();
} catch (NullPointerException e) {}
}
/**
* Changes and updates the frame
* @param img the Image to "cast"
*/
public static BufferedImage toBufferedImage(Image img) {
if (img instanceof BufferedImage)
return (BufferedImage) img;
BufferedImage bImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bImage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bImage;
}
/**
* Creates a BufferedReader from a given file
* @param name the name of the file, including the extension
* @return a BufferedReader object of the given file
*/
public static BufferedReader getBufferedReader(String name) {
return new BufferedReader(new InputStreamReader(Utility.class.getResourceAsStream(RES_NAME+"/text/" + name)));
}
/**
* Gets the width of a String based on a given Font
* @param f the font to use for the text
* @param text the String to get the width of
* @return the width of the String, in pixels
*/
public static int getStringWidth(String text, Font f) {
return (new JPanel()).getFontMetrics(f).charsWidth(text.toCharArray(),0,text.length());
}
/**
* Gets the width of a String based on a given Graphics object
* @param g the Graphics object whose current font is to be used
* @param text the String to get the width of
* @return the width of the String, in pixels
*/
public static int getStringWidth(String text, Graphics g) {
return g.getFontMetrics().charsWidth(text.toCharArray(),0,text.length());
}
}