LinkArea.java (5126B)
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 8 import javax.imageio.ImageIO; 9 import javax.swing.JPanel; 10 11 public class LinkArea extends JPanel { 12 private static final long serialVersionUID = 635033554190094921L; 13 static final int FLOORLEVEL = 587; 14 static final int FLOORWIDTH = 150; 15 Link myApplet = null; 16 Image yellowgem; 17 Image spider; 18 Image obstacle; 19 Image redgem; 20 Image octopellets; 21 Image octo; 22 Image madblob; 23 Image heroswordright; 24 Image heroswordleft; 25 Image herostand; 26 Image hero1; 27 Image hero2; 28 Image herostandleft; 29 Image hero1left; 30 Image hero2left; 31 Image heroshieldright; 32 Image heroshieldleft; 33 Image greengem; 34 Image bluegem; 35 Image goldskullata; 36 Image gemstandgems; 37 Image gemstandempty; 38 Image blob; 39 MediaTracker mt = null; 40 41 public LinkArea(Link parent) { 42 mt = new MediaTracker(parent); 43 myApplet = parent; 44 yellowgem = load(myApplet, "yellowgem.gif"); 45 spider = load(myApplet, "spider.gif"); 46 obstacle = load(myApplet, "rock.gif"); 47 redgem = load(myApplet, "redgem.gif"); 48 octopellets = load(myApplet, "octopellets.gif"); 49 octo = load(myApplet, "octo.gif"); 50 madblob = load(myApplet, "madblob.gif"); 51 heroswordright = load(myApplet, "linkswordright.gif"); 52 heroswordleft = load(myApplet, "linkswordleft.gif"); 53 herostand = load(myApplet, "linkstandright.gif"); 54 hero1 = load(myApplet, "linkstandright.gif"); 55 hero2 = load(myApplet, "linkstandright2.gif"); 56 herostandleft = load(myApplet, "linkstandleft.gif"); 57 hero1left = load(myApplet, "linkstandleft.gif"); 58 hero2left = load(myApplet, "linkstandleft2.gif"); 59 heroshieldright = load(myApplet, "linkshieldright.gif"); 60 heroshieldleft = load(myApplet, "linkshieldleft.gif"); 61 greengem = load(myApplet, "greengem.gif"); 62 bluegem = load(myApplet, "bluegem.gif"); 63 goldskullata = load(myApplet, "goldskullata.gif"); 64 gemstandgems = load(myApplet, "gemstand(with gems).gif"); 65 gemstandempty = load(myApplet, "gemstand(without gems).gif"); 66 blob = load(myApplet, "blob.gif"); 67 } 68 69 void checkImage(Image image, String name) { 70 if (mt != null) { 71 mt.addImage(image, 0); 72 try { 73 mt.waitForID(0, 5000); 74 } catch (InterruptedException ie) { 75 // nothing to do 76 } 77 if (mt.isErrorID(0)) 78 System.out.println("Image Not found: " + name.toString()); 79 } 80 } 81 82 Image load(Link parent, String picture) { 83 try { 84 Image im = ImageIO.read(new File("../Assets/" + picture)); 85 checkImage(im, picture); 86 return (im); 87 } catch (IOException e) { 88 // TODO Auto-generated catch block 89 e.printStackTrace(); 90 } 91 return null; 92 } 93 94 @Override 95 public synchronized void paintComponent(Graphics g) { 96 super.paintComponent(g); 97 Image obstacleimage; 98 if (myApplet == null) 99 return; 100 final int w = getBounds().width; 101 final int h = getBounds().height; 102 g.setColor(Color.white); 103 g.fillRect(0, 0, w, h); 104 g.setColor(Color.black); 105 /* 106 * // paint the floor for (int pos=0; pos < w; pos+=FLOORWIDTH) { 107 * g.drawImage(floor,pos, FLOORLEVEL,Color.black, null); } 108 */ 109 110 if (Link.obstaclePositions != null) { 111 // show the obstacles 112 for (int i = 0; i < Link.obstaclePositions.length; i++) { 113 obstacleimage = myApplet.getObstacleImage(i); 114 g.drawImage(obstacleimage, Link.obstaclePositions[Link.level - 1][i].x, 115 Link.obstaclePositions[Link.level - 1][i].y - 4, Color.white, null); 116 } 117 } 118 119 /* 120 * // show the enemies for (int i=0; i < myApplet.enemyPositions.length; i++) { 121 * if (myApplet.enemyPositions[i].x > -1) g.drawImage(enemyImage, 122 * myApplet.enemyPositions[i].x, myApplet.enemyPositions[i].y,null); } } 123 */ 124 // draw Hero (on top of preceding images) 125 if (Link.playerPosition != null) { 126 // show the player 127 Image herosimage = myApplet.getHeroImage(); 128 /* 129 * if (myApplet.herocrouching) { if (myApplet.heroleft) herosimage = 130 * herocrouchleft; else herosimage = herocrouch; } if (myApplet.jumping) { if 131 * (myApplet.heroleft) herosimage = herojumpleft; else herosimage = herojump; } 132 */ 133 g.drawImage(herosimage, Link.playerPosition.x, Link.playerPosition.y, /* Color.white, */ null); 134 } 135 /* 136 * // draw warp image (on top of Hero) if (myApplet.warpzone != null) { // show 137 * the warp zone at the end of the level g.drawImage(herowarp, 138 * myApplet.warpzone.x, myApplet.warpzone.y,Color.white, null); } 139 */ 140 141 } 142 }