MarioArea.java (11344B)
1 import java.awt.Color; 2 import java.awt.Graphics; 3 import java.awt.Image; 4 import java.awt.MediaTracker; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.io.File; 8 import java.io.IOException; 9 10 import javax.imageio.ImageIO; 11 import javax.swing.JPanel; 12 13 public class MarioArea extends JPanel implements ActionListener { 14 private static final long serialVersionUID = 5208087574284410378L; 15 static int FLOORLEVEL = 589; 16 static final int FLOORWIDTH = 150; 17 static int FLOORDIFF = 605 - FLOORLEVEL; 18 Mario myApplet = null; 19 Image mariotitle; 20 Image icefloor; 21 Image floor2; 22 Image floor3; 23 Image floor4; 24 Image floor5; 25 Image floor6; 26 Image floor7; 27 Image floor8; 28 Image floor9; 29 Image mariograss; 30 Image upmushroom; 31 Image goomba; 32 Image goombaleft; 33 Image goomba2; 34 Image goomba2left; 35 Image kooparedf; 36 Image koopared; 37 Image redshell; 38 Image kooparedfleft; 39 Image kooparedleft; 40 Image koopagreen; 41 Image koopagreenleft; 42 Image greenshell; 43 Image hero1; 44 Image hero1left; 45 Image hero2; 46 Image hero2left; 47 Image herocrouch; 48 Image herocrouchleft; 49 Image herojump; 50 Image herojumpleft; 51 Image obstacle; 52 Image tallobstacle; 53 Image herostand; 54 Image herostandleft; 55 Image piranha1; 56 Image piranha2; 57 Image piranhapeep; 58 Image piranhapeepo; 59 Image piranhapeepy; 60 Image poison; 61 Image Luigi1; 62 Image Luigi1left; 63 Image Luigi2; 64 Image Luigi2left; 65 Image Luigicrouch; 66 Image Luigiccrouchleft; 67 Image Luigijump; 68 Image Luigijumpleft; 69 Image Luigistand; 70 Image Luigistandleft; 71 Image herowarp; 72 Image levelcomplete; 73 Image herodead; 74 Image squishedgoomba; 75 Image Banzai; 76 Image Bullet; 77 Image Wiglerleft; 78 Image Wiglerright; 79 Image Wiglerleft2; 80 Image Wiglerright2; 81 Image Wiglerleft1; 82 Image Wiglerright1; 83 Image Boo1; 84 Image Boo2; 85 Image Fireboo1; 86 Image Fireboo2; 87 Image Bobomb; 88 Image Bobomb2; 89 Image Bobombf; 90 Image Bobombf2; 91 Image Warship1; 92 Image Warship2; 93 Image Yetil1; 94 Image Yetil2; 95 Image Yetir1; 96 Image Yetir2; 97 Image p1fl; 98 Image p2fl; 99 Image p1fr; 100 Image p2fr; 101 Image cube; 102 Image robot; 103 Image chainchomp1; 104 Image chainchomp2; 105 Image chainchompdead; 106 Image Atom1; 107 Image Atom2; 108 Image Floor10; 109 MediaTracker mt = null; 110 boolean showingMessage = false; 111 112 public MarioArea(Mario parent) { 113 mt = new MediaTracker(parent); 114 myApplet = parent; 115 upmushroom = load(parent, "1up!mushroom.gif"); 116 goomba = load(parent, "goomba.gif"); 117 goombaleft = load(parent, "goombaleft.gif"); 118 goomba2 = load(parent, "goomba2.gif"); 119 goomba2left = load(parent, "goombaleft2.gif"); 120 squishedgoomba = load(parent, "goombasquished.gif"); 121 122 kooparedf = load(parent, "koopaparatroopa1.gif"); 123 koopared = load(parent, "koopaparatroopa2.gif"); 124 redshell = load(parent, "koopaparatroopashell.gif"); 125 126 kooparedfleft = load(parent, "koopaparatroopa1left.gif"); 127 kooparedleft = load(parent, "koopaparatroopa2left.gif"); 128 koopagreenleft = load(parent, "koopatroopa.gif"); 129 130 koopagreen = load(parent, "koopatroopaleft.gif"); 131 greenshell = load(parent, "koopatroopashell.gif"); 132 133 hero1 = load(parent, "mario1.gif"); 134 hero1left = load(parent, "mario1left.gif"); 135 hero2 = load(parent, "mario2.gif"); 136 hero2left = load(parent, "mario2left.gif"); 137 herocrouch = load(parent, "mariocrouch.gif"); 138 herocrouchleft = load(parent, "mariocrouchleft.gif"); 139 herojump = load(parent, "mariojump.gif"); 140 herojumpleft = load(parent, "mariojumpleft.gif"); 141 obstacle = load(parent, "mariopipe.gif"); 142 tallobstacle = load(parent, "mariopipe2.gif"); 143 herostand = load(parent, "mariostand.gif"); 144 herostandleft = load(parent, "mariostandleft.gif"); 145 146 piranhapeep = load(parent, "piranhaplanthugepeep.gif"); 147 piranhapeepo = load(parent, "piranhaplanthugepeepo.gif"); 148 piranhapeepy = load(parent, "piranhaplanthugepeepy.gif"); 149 150 piranha1 = load(parent, "piranhaplant1huge.gif"); 151 piranha2 = load(parent, "piranhaplant2huge.gif"); 152 153 poison = load(parent, "poisonmushroom.gif"); 154 herowarp = load(parent, "mariowarp.gif"); 155 icefloor = load(parent, "mariofloor.gif"); 156 floor2 = load(parent, "mariofloor2.gif"); 157 floor3 = load(parent, "mariofloor3.gif"); 158 floor4 = load(parent, "mariofloor4.gif"); 159 floor5 = load(parent, "mariofloor5.gif"); 160 floor6 = load(parent, "mariofloor6.gif"); 161 floor7 = load(parent, "mariofloor7.gif"); 162 floor8 = load(parent, "mariofloor8.gif"); 163 floor9 = load(parent, "mariofloor9.gif"); 164 mariograss = load(parent, "mariograss.gif"); 165 p1fl = load(parent, "piranhaflying1.gif"); 166 p2fl = load(parent, "piranhaflying2.gif"); 167 p1fr = load(parent, "piranharightflying1.gif"); 168 p2fr = load(parent, "piranharightflying2.gif"); 169 170 levelcomplete = load(parent, "missioncomplete.gif"); 171 herodead = load(parent, "mariodead.gif"); 172 173 Banzai = load(parent, "Banzai Bill.gif"); 174 Bullet = load(parent, "Bullet Bill.gif"); 175 176 Wiglerleft = load(parent, "Wigler1Left.gif"); 177 Wiglerright = load(parent, "Wigler1Right.gif"); 178 179 Wiglerleft2 = load(parent, "Wigler2Left.gif"); 180 Wiglerright2 = load(parent, "Wigler2Right.gif"); 181 182 Wiglerleft1 = load(parent, "Wigler1.5Left.gif"); 183 Wiglerright1 = load(parent, "Wigler1.5Right.gif"); 184 185 Boo1 = load(parent, "boo1.gif"); 186 Boo2 = load(parent, "boo2.gif"); 187 Fireboo1 = load(parent, "fireboo1.gif"); 188 Fireboo2 = load(parent, "fireboo2.gif"); 189 Bobomb = load(parent, "bobomb.gif"); 190 Bobomb2 = load(parent, "bobomb2.gif"); 191 Bobombf = load(parent, "bobombr.gif"); 192 Bobombf2 = load(parent, "bobombf2.gif"); 193 Warship1 = load(parent, "warship1.gif"); 194 Warship2 = load(parent, "warship2.gif"); 195 196 Yetil1 = load(parent, "yetileft1.gif"); 197 Yetil2 = load(parent, "yetileft2.gif"); 198 Yetir1 = load(parent, "yetiright1.gif"); 199 Yetir2 = load(parent, "yetiright2.gif"); 200 mariotitle = load(parent, "mariotitle.gif"); 201 cube = load(parent, "cube.gif"); 202 chainchomp1 = load(parent, "Chain Chomp!!!.gif"); 203 chainchomp2 = load(parent, "Chain Chomp2!!!.gif"); 204 robot = load(parent, "Robot.gif"); 205 chainchompdead = load(parent, "Chain Chomp Dead!!!.gif"); 206 207 Atom1 = load(parent, "Atomic Atom1.gif"); 208 Atom2 = load(parent, "Atomic Atom2.gif"); 209 210 Floor10 = load(parent, "mariofloor10.gif"); 211 } 212 213 Image load(Mario parent, String picture) { 214 try { 215 Image im = ImageIO.read(new File("../Assets/" + picture)); 216 checkImage(im, picture); 217 return (im); 218 } catch (IOException e) { 219 // TODO Auto-generated catch block 220 e.printStackTrace(); 221 } 222 return null; 223 } 224 225 void checkImage(Image image, String name) { 226 if (mt != null) { 227 mt.addImage(image, 0); 228 try { 229 mt.waitForID(0, 5000); 230 } catch (InterruptedException ie) { 231 // nothing to do 232 } 233 if (mt.isErrorID(0)) 234 System.out.println("Image Not found: " + name.toString()); 235 } 236 } 237 238 @Override 239 public void paintComponent(Graphics g) { 240 super.paintComponent(g); 241 Image obstacleimage, enemyimage; 242 if (myApplet == null || Fireboo2 == null) 243 return; 244 final int w = getBounds().width; 245 //final int h = getBounds().height; 246 g.setColor(Color.white); 247 g.fillRect(0, 0, w, FLOORLEVEL); // don't white wash the floor! 248 249 g.setColor(Color.black); 250 251 // paint the floor 252 if (Mario.floors[Mario.level - 1] != null) { 253 for (int pos = 0; pos < w; pos += FLOORWIDTH) { 254 g.drawImage(Mario.floors[Mario.level - 1], pos, FLOORLEVEL, Color.black, null); 255 } 256 } 257 if (!Mario.juststarted) { 258 if (Mario.obstaclePositions != null) { 259 myApplet.changePageIfNecessary(); 260 // show the obstacles from right to left (because pipes have white space to 261 // their left but not their right) 262 for (int i = Mario.obstacleNumber - 1; i >= 0; i--) { 263 obstacleimage = myApplet.getObstacleImage(i); 264 if (Mario.isTall[i]) 265 obstacleimage = tallobstacle; // only one image for this for now 266 g.drawImage(obstacleimage, 267 Mario.obstaclePositions[i].x - (Mario.page * Mario.PAGEWIDTH), 268 Mario.obstaclePositions[i].y, Color.white, null); 269 } 270 } 271 272 // show the enemies 273 if (Mario.enemyPositions != null) { 274 for (int i = 0; i < Mario.enemyNumber; i++) { 275 // if (!myApplet.enemyDead[i]) { 276 enemyimage = myApplet.getEnemyImage(i); 277 g.drawImage(enemyimage, Mario.enemyPositions[i].x - (Mario.page * Mario.PAGEWIDTH), 278 Mario.enemyPositions[i].y, null); 279 // } 280 } 281 } 282 283 // draw Hero (on top of preceding images) 284 if (Mario.playerPosition != null) { 285 // show the player 286 Image herosimage = myApplet.getHeroImage(); 287 if (Mario.herocrouching) { 288 if (Mario.heroleft) 289 herosimage = herocrouchleft; 290 else 291 herosimage = herocrouch; 292 } 293 if (Mario.jumping) { 294 if (Mario.heroleft) 295 herosimage = herojumpleft; 296 else 297 herosimage = herojump; 298 } 299 g.drawImage(herosimage, Mario.playerPosition.x - (Mario.page * Mario.PAGEWIDTH), 300 Mario.playerPosition.y, /* Color.white, */ null); 301 } 302 } 303 304 // draw warp image (on top of Hero) 305 if (Mario.warpzone != null) { 306 // show the warp zone at the end of the level 307 g.drawImage(herowarp, Mario.warpzone.x - (Mario.page * Mario.PAGEWIDTH), Mario.warpzone.y, 308 Color.white, null); 309 } 310 311 if (Mario.ingame == false && !Mario.juststarted) { 312 if (Mario.dead) { 313 // Hero is dead 314 g.drawImage(herodead, myApplet.getBounds().width / 2, getBounds().height / 2, Color.white, null); 315 } else { 316 // level complete 317 g.drawImage(levelcomplete, myApplet.getBounds().width / 2, getBounds().height / 2, Color.white, null); 318 } 319 showingMessage = true; 320 } else 321 showingMessage = false; 322 323 myApplet.setSomethingChangedSinceRepaint(false); 324 } 325 326 @Override 327 public void actionPerformed(ActionEvent arg0) { 328 repaint(); 329 } 330 }