?? showable.java
字號:
package dungeonsanddragons.model;import java.awt.Graphics;import java.awt.Image;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;/** * Super class that represents any item that can show up in a room. * All items that can be shown in rooms are Showables * @author Sandra Nilsson */public abstract class Showable { /** The image representing this item*/ protected Image img; /**The sound representing this item*/ private Clip sound; /**The room that this item is located in*/ protected Room currentRoom; /** * Create a new Showable represented by the given image * @param imgName the file name of the image that shall represent this showable */ public Showable(String imgName){ initImage(imgName); } /** * Create a new Showable represented by the given image and the given sound * @param imgName the file name of the image that shall represent this showable * @param soundName the file name of the sound file that shall represent this showable */ public Showable(String imgName, String soundName){ initImage(imgName); initSound(soundName); } /** * Draw this item at the correct location * @param g */ public void draw(Graphics g){ if(currentRoom != null){ g.drawImage(img, currentRoom.getX()+10, currentRoom.getY()+10, null); } } /** * * @return the room where this showable is located */ public Room getCurrentRoom () { return currentRoom; } /** * Set the room where this showable shall be located * @param val the room */ public void setCurrentRoom (Room val) { this.currentRoom = val; } /** * Initialize the image object * @param imgName the file name of the image */ private void initImage(String imgName) { 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(); } } /** * Method to get the image object that represents this showalbe * @return the image object */ public Image getImg() { return img; } /** * Mehtod to get the sound object that represents this showable * @return the sound Clip object */ public Clip getSound() { return sound; } /** * 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 } } } /** * Plays the sound file of this showable (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"); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -