CaveArea.java (4068B)
1 import java.awt.Color; 2 import java.awt.Graphics; 3 import java.awt.Image; 4 import java.awt.MediaTracker; 5 import java.io.File; 6 import java.io.IOException; 7 //import java.applet.AudioClip; 8 9 import javax.imageio.ImageIO; 10 import javax.swing.JPanel; 11 12 public class CaveArea extends JPanel { 13 private static final long serialVersionUID = -7163898717940240890L; 14 CaveGame myApplet = null; 15 Image title[] = new Image[10]; 16 MediaTracker mt = null; 17 Image firstRoom, leftDoor, firstCave; 18 Image river, bridge; 19 Image cavern, hole, rope; 20 Image hotRoom, secondCave, gameComplete; 21 Image currentImage; 22 23 /* 24 * AudioClip ACfirstRoom, ACleftDoor, ACfirstCave; AudioClip ACriver, ACbridge; 25 * AudioClip ACcavern, AChole, ACrope; AudioClip AChotRoom, ACsecondCave, 26 * ACgameComplete; AudioClip currentMusic; 27 */ 28 public CaveArea(CaveGame parent) { 29 mt = new MediaTracker(parent); 30 myApplet = parent; 31 32 firstRoom = load(parent, "1ST ROOM.gif"); 33 leftDoor = load(parent, "GAMER PICKS A.gif"); 34 firstCave = load(parent, "GAMER PICKS C.gif"); 35 river = load(parent, "GAMER PICKS B.gif"); 36 bridge = load(parent, "GAMER PICKS B THEN A.gif"); 37 cavern = load(parent, "GAMER PICKS B THEN B.gif"); 38 hole = load(parent, "GAMER PICKS B THEN B THEN B.gif"); 39 rope = load(parent, "GAMER PICKS B THEN B THEN C.gif"); 40 hotRoom = load(parent, "GAMER PICKS B THEN B THEN A.gif"); 41 secondCave = load(parent, "GAMER PICKS B THEN B THEN A THEN B.gif"); 42 gameComplete = load(parent, "GAME COMPLETE.gif"); 43 44 /* 45 * ACfirstRoom = loadSound(parent, "The Prophecy.mp3"); ACleftDoor = 46 * loadSound(parent, "The Black Rider.mp3"); ACfirstCave = loadSound(parent, 47 * "The Shadow of the Past.mp3"); ACriver = loadSound(parent, 48 * "The Great River.mp3"); ACbridge = loadSound(parent, 49 * "Flight to the Ford.mp3"); ACcavern = loadSound(parent, 50 * "A Journey in the Dark.mp3"); AChole = loadSound(parent, 51 * "A Knife in the Dark.mp3"); ACrope = loadSound(parent, "Amon Hen.mp3"); 52 * AChotRoom = loadSound(parent, "The Bridge of Khazad Dhum.mp3"); ACsecondCave 53 * = loadSound(parent, "The Treason of Isengard.mp3"); ACgameComplete = 54 * loadSound(parent, "Concerning Hobbits.mp3"); 55 */ 56 } 57 58 Image load(CaveGame parent, String picture) { 59 try { 60 Image im = ImageIO.read(new File("../Assets/" + picture)); 61 checkImage(im, picture); 62 return (im); 63 } catch (IOException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } 67 return null; 68 } 69 70 /* 71 * AudioClip loadSound(CaveGame parent, String soundfile) { AudioClip s = null; 72 * s = parent.getAudioClip(myApplet.getCodeBase(), "CaveGame\" + soundfile); 73 * checkAudio(s, soundfile); return (s); } 74 * 75 * void checkAudio(AudioClip ac, String name) { if (ac == null) 76 * System.out.println("Audio Clip Not found: " + name.toString()); } 77 */ 78 void checkImage(Image image, String name) { 79 if (mt != null) { 80 mt.addImage(image, 0); 81 try { 82 mt.waitForID(0, 5000); 83 } catch (InterruptedException ie) { 84 // nothing to do 85 } 86 if (mt.isErrorID(0)) 87 System.out.println("Image Not found: " + name.toString()); 88 } 89 } 90 91 @Override 92 public void paintComponent(Graphics g) { 93 super.paintComponent(g); 94 95 if (myApplet == null) 96 return; 97 98 g.setColor(Color.white); 99 g.setColor(Color.black); 100 g.drawImage(currentImage, 0, 0, Color.white, null); 101 } 102 103 public void showTitle(Graphics g) { 104 for (int i = 0; i < 10; i++) { 105 g.drawImage(title[i], myApplet.getBounds().width / 2, getBounds().height / 2, Color.white, null); 106 try { 107 Thread.sleep(50); 108 } catch (Exception e) { 109 } 110 } 111 } 112 }