Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Michael McKenna
3,451 PointsWhat is going on in the BufferedImage class?
public class Resources {
public static BufferedImage welcome, iconimage, line, legolas;
public static AudioClip hit, bounce;
public static Color darkBlue, darkRed;
public static void load() {
welcome = loadImage("welcome.png");
legolas = loadImage("legolas.png");
iconimage = loadImage("iconimage.png");
line = loadImage("line.png");
hit = loadSound("hit.wav");
bounce = loadSound("bounce.wav");
darkBlue = new Color(25, 83, 105); // Constructor accepts RGB
darkRed = new Color(105, 13, 13); // Constructor accepts RGB
}
private static AudioClip loadSound(String filename) {
URL fileURL = Resources.class.getResource("/resources/" + filename);
return Applet.newAudioClip(fileURL);
}
private static BufferedImage loadImage(String filename) {
BufferedImage img = null;
try {
img = ImageIO.read(Resources.class.getResourceAsStream("/resources/" + filename));
} catch (IOException e) {
System.out.println("Error while reading: " + filename);
e.printStackTrace();
}
return img;
}
}
filename is being called but there is no photo labeled as filename? The textbook I'm using didn't do a very good job explaining why they did what they did here.
2 Answers
Craig Dennis
Treehouse TeacherAlso in the awt package, here is BufferedImage.
Craig Dennis
Treehouse TeacherThis line calls the method loadSound, passing in "hit.wav" as the filename argument.
hit = loadSound("hit.wav");
This then loads the file from the resources directory.
Michael McKenna
3,451 PointsMichael McKenna
3,451 PointsThat doesn't answer my question.
To be more explicit, the first half the class I understand. The second half, starting at: private static AudioClip loadSound(String filename) { URL fileURL = Resources.class.getResource("/resources/" + filename); return Applet.newAudioClip(fileURL); }
is where I am getting confused. The syntax is not a problem, but the logic. For example, the use of "filename" is not defined anywhere. For example, when I'm trying to rationalize it, I see file name as being set to something, such as a picture, then in the code I posted immediately above the filename (legolas in this case) would be loaded.
Basically, I just want an explanation of the logic in the second half of the code in this class.