-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.java
More file actions
123 lines (109 loc) · 2.72 KB
/
Utilities.java
File metadata and controls
123 lines (109 loc) · 2.72 KB
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
import java.awt.Font;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* This class contains various utility methods that can be used by all of the
* classes.
*
* @author John Kurlak (kurlak)
* Weston Thayer (weston5)
* Panhavorn Hok (onehok1)
* @version 12.07.09
*/
public class Utilities
{
/**
* This corresponds to the delimiter of the file path for the current
* operating system.
*/
public static String slash = (System.getProperty("os.name")
.indexOf("Windows") != -1) ? "\\" : "/";
/**
* This method loads an image from a relative file path.
*
* @param relativeFilePath The relative file path
* @return Returns a reference to the image corresponding to the given
* file path.
*/
public static Image loadImage(String relativeFilePath)
{
BufferedImage img;
try
{
img = ImageIO.read(Utilities.loadFile(relativeFilePath));
return img;
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.exit(1);
return null;
}
}
/**
* This method loads a file from a relative file path.
*
* @param relativeFilePath The relative file path
* @return Returns a reference to the file corresponding to the given
* file path.
*/
public static File loadFile(String relativeFilePath)
{
String filePath = Utilities.getAppPath() + "includes" + slash +
relativeFilePath;
return new File(filePath);
}
/**
* This method loads a font from a relative file path.
*
* @param relativeFilePath The relative file path
* @return Returns a reference to the font corresponding to the given
* file path.
*/
public static Font loadFont(String relativeFilePath)
{
File fontFile = loadFile(relativeFilePath);
try
{
FileInputStream fontStream = new FileInputStream(fontFile);
try
{
Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
return font;
}
catch (Exception e)
{
// Not needed
}
}
catch (FileNotFoundException e)
{
// We will assume there is no corruption of our installation
}
return new Font("sansserif", Font.PLAIN, 10);
}
/**
* This method gets the path of the running application.
*
* @return Returns the directory of the current application
*/
private static String getAppPath()
{
File currentDirectory = new File(".");
try
{
return currentDirectory.getCanonicalPath() + slash;
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.exit(1);
return "";
}
}
}