diff --git a/src/com/msb/ResourcesMgr.java b/src/com/msb/ResourcesMgr.java index 4e73a48..7c4f241 100644 --- a/src/com/msb/ResourcesMgr.java +++ b/src/com/msb/ResourcesMgr.java @@ -1,5 +1,7 @@ package com.msb; +import com.msb.util.ImageUtil; + import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.IOException; @@ -12,21 +14,27 @@ import java.io.IOException; */ public class ResourcesMgr { - public static BufferedImage tankL, tankR, tankU, tankD; + public static BufferedImage goodTankL, goodTankR, goodTankU, goodTankD; + public static BufferedImage badTankL, badTankR, badTankU, badTankD; public static BufferedImage bulletL, bulletR, bulletU, bulletD; public static BufferedImage [] explodeImages = new BufferedImage[16]; static { try { - tankL = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/tankL.gif")); - tankR = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/tankR.gif")); - tankU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/tankU.gif")); - tankD = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/tankD.gif")); - - bulletL = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletL.gif")); - bulletR = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletR.gif")); - bulletU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletU.gif")); - bulletD = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletD.gif")); + goodTankU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/GoodTank1.png")); + goodTankL = ImageUtil.rotateImage(goodTankU, -90); + goodTankR = ImageUtil.rotateImage(goodTankU, 90); + goodTankD = ImageUtil.rotateImage(goodTankU, 180); + + badTankU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/BadTank1.png")); + badTankL = ImageUtil.rotateImage(badTankU, -90); + badTankR = ImageUtil.rotateImage(badTankU, 90); + badTankD = ImageUtil.rotateImage(badTankU, 180); + + bulletU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletU.png")); + bulletL = ImageUtil.rotateImage(bulletU, -90); + bulletR = ImageUtil.rotateImage(bulletU, 90); + bulletD = ImageUtil.rotateImage(bulletU, 180); for (int i=1; i<=explodeImages.length ; i++) { explodeImages[i-1] = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/e" + i + ".gif")); diff --git a/src/com/msb/Tank.java b/src/com/msb/Tank.java index 4084611..0ea3832 100644 --- a/src/com/msb/Tank.java +++ b/src/com/msb/Tank.java @@ -17,8 +17,8 @@ public class Tank { private boolean move = true; //为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用 private TankFrame tankFrame; - public static final int WIDTH = ResourcesMgr.tankD.getWidth(); - public static final int HEIGHT = ResourcesMgr.tankD.getHeight(); + public static final int WIDTH = ResourcesMgr.goodTankU.getWidth(); + public static final int HEIGHT = ResourcesMgr.goodTankU.getHeight(); private boolean live = true; private Random random = new Random(); private GroupEnum group = GroupEnum.BAD; @@ -41,12 +41,13 @@ public class Tank { } switch (dir) { - case UP: g.drawImage(ResourcesMgr.tankU, x, y, null); break; - case DOWN: g.drawImage(ResourcesMgr.tankD, x, y, null); break; - case LEFT: g.drawImage(ResourcesMgr.tankL, x, y, null); break; - case RIGHT: g.drawImage(ResourcesMgr.tankR, x, y, null); break; + case UP: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankU : ResourcesMgr.badTankU, x, y, null); break; + case DOWN: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankD : ResourcesMgr.badTankD, x, y, null); break; + case LEFT: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankL : ResourcesMgr.badTankL, x, y, null); break; + case RIGHT: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankR : ResourcesMgr.badTankR, x, y, null); break; default: break; } + moving(); } @@ -110,7 +111,9 @@ public class Tank { public void die() { this.live = false; - tankFrame.explodes.add(new Explode(this.x, this.y, tankFrame)); + int eX = this.x + WIDTH/2 - Explode.WIDTH/2; + int eY = this.y + HEIGHT/2 - Explode.HEIGHT/2; + tankFrame.explodes.add(new Explode(eX, eY, tankFrame)); } public GroupEnum getGroup() { diff --git a/src/com/msb/util/ImageUtil.java b/src/com/msb/util/ImageUtil.java new file mode 100644 index 0000000..48d9033 --- /dev/null +++ b/src/com/msb/util/ImageUtil.java @@ -0,0 +1,24 @@ +package com.msb.util; + +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; + +public class ImageUtil { + public static BufferedImage rotateImage(final BufferedImage bufferedimage, + final int degree) { + int w = bufferedimage.getWidth(); + int h = bufferedimage.getHeight(); + int type = bufferedimage.getColorModel().getTransparency(); + BufferedImage img; + Graphics2D graphics2d; + (graphics2d = (img = new BufferedImage(w, h, type)) + .createGraphics()).setRenderingHint( + RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BILINEAR); + graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2); + graphics2d.drawImage(bufferedimage, 0, 0, null); + graphics2d.dispose(); + return img; + } +}