I am about to finish a memory or card game in java where the half of the codes (Or solution) are given by my school itself. So my task is to finish the rest and build the game in netbeans. Here I will present the codes theat I got from my school which I don't really understand specially the "try" and "catch" statement. If anyone pls can explain the all codes from the begining in "MemoryCard" class (off course I also have three other classes), I will be very grateful to you. Thankx
package memory; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; /** * Class to handle one single meory card * (Each card is supposed to be placed at two positions on the board) * @author Sandra Nilsson */ public class MemoryCard { private Image img; private Image backImg; private Clip sound; /** * Creates a new memory card object and reads the given files from disc into * memory. * Use this constructor when you want to have sounds associated with each card! * @param imgName the name of the file where to find the image file that shall represent this card * @param backImgName the name of the file where to find the back side file that shall represent this card * @param audioFileName the name of the file where to find the sound file that shall represent this card */ public MemoryCard(String imgName, String backImgName, String audioFileName) { initImages(imgName, backImgName); initSound(audioFileName); } /** * Creates a new memory card object and reads the given files from disc into * memory and saves the data in this object * @param imgName the name of the file where to find the image file that shall represent this card * @param backImgName the name of the file where to find the back side file that shall represent this card */ public MemoryCard(String imgName, String backImgName) { initImages(imgName, backImgName); } /** * Reads the images from file * @param imgName the name of the file where to find the image that shall represent this card * @param backImgName the name of the file where to find the back side image that shall represent this card */ private void initImages(String imgName, String backImgName) { try { File f = new File(imgName); System.out.println("Reading " + f.getAbsolutePath()); this.img = ImageIO.read(f); } catch (IOException ex) { System.out.println("Error when reading image for " + imgName); ex.printStackTrace(); } try { File f = new File(backImgName); BufferedImage bi = ImageIO.read(f); backImg = ImageIO.read(f); } catch (IOException ex) { System.out.println("Error when reading back image " + backImgName); ex.printStackTrace(); } } /** * Reads the given sound file and saves it for later use. * @param soundFile the path to the sound file. */ private void initSound(String soundFile) { // Open an input stream to the audio file. InputStream in = null; AudioInputStream stream = null; try { stream = AudioSystem.getAudioInputStream(new File(soundFile)); AudioFormat format = stream.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat()); sound = (Clip) AudioSystem.getLine(info); sound.open(stream); } catch (LineUnavailableException ex) { System.out.println("Error when creating sound from file " + soundFile); ex.printStackTrace(); } catch (UnsupportedAudioFileException ex) { System.out.println("Error when creating sound from file " + soundFile); ex.printStackTrace(); } catch (FileNotFoundException ex) { System.out.println("Error when creating sound from file " + soundFile); ex.printStackTrace(); } catch (IOException ex) { System.out.println("Error when creating sound from file " + soundFile); ex.printStackTrace(); } finally { try { stream.close(); } catch (IOException ex) { //Do not bother about this } } } /** * Method to get the image object that represent the back of this card * @return the back image object */ public Image getBackImg() { return backImg; } /** * Method to get the image object that represents the front of this card * @return */ public Image getImg() { return img; } /** * Plays the sound file of this card (if any) */ public void play() { if (sound != null) { sound.flush(); //Restart the sound every time sound.setFramePosition(0); System.out.println("Playing"); sound.start(); } else { // System.out.println("No sound available"); } } }