-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRessourcesFetcher.java
More file actions
81 lines (71 loc) · 2.02 KB
/
RessourcesFetcher.java
File metadata and controls
81 lines (71 loc) · 2.02 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
import java.net.URL;
import java.util.Scanner;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
* classe permettant d'acceder aux ressources du jeu
* @author Clément Chomicki
*/
public class RessourcesFetcher
{
/**
* Find the file requested
* @return an URL indicating the emplacement of the requested file OR null if the file doesn't exist/the filename is invalid.
* @param pName the name of the file
* @param pType the Subfolder of Ressources/ the file should be in.
*/
static public URL findRessource( final String pName, final String pType)
{
if ( pName.matches("[a-zA-Z_0-9]*.[a-zA-Z_0-9]*") )
{
String vPath = "Ressources/" + pType + "/" + pName;
URL vURL = RessourcesFetcher.class.getClassLoader().getResource( vPath );
// URL vURL = this.getClass().getClassLoader().getResource( vPath );
if (vURL == null)
System.out.println ( "Ressource not found: " + vPath );
return vURL;
}
else {
System.out.println ( "Invalid file name" );
return null;
}
} //findRessource()
/**
* Tries to open a script
* @return a Scanner of the text file OR null
* @param pName the name of the script (with or without '.txt' at the end)
*/
static public Scanner openScript( final String pName )
{
URL vURL = RessourcesFetcher.findRessource( pName, "Scripts" );
try {
return new Scanner( vURL.openStream() );
}
catch (NullPointerException e) {
return null;
}
catch (IOException e) {
return null;
}
} //openScript()
/**
* Tries to open an Image
* @return a BufferedImage OR null
* @param pName the name of the Image
*/
static public BufferedImage openImage( final String pName )
{
URL vURL = RessourcesFetcher.findRessource( pName+".jpg", "Images" );
BufferedImage vImg;
try {
vImg = ImageIO.read(vURL);
}
catch(Exception pE)
{
System.out.println("Erreur: Image manquante!");
vImg = null;
}
return vImg;
} //openImage()
}