day3,将图片放入images,建立test测试包,测试读取图片,实现坦克移动图片

master
zhanxinlin 3 years ago
parent 21229d3a8d
commit ab9d2e9839

@ -1,61 +1,63 @@
import java.awt.*; package com.mashibing.tank;
/** import java.awt.*;
* @Description:
* @Author: zhanxinlin /**
* @Date: 2022-09-27 23:10 * @Description:
*/ * @Author: zhanxinlin
public class Bullet { * @Date: 2022-09-27 23:10
private static final int SPEED = 10; */
private static final int WIDTH = 20; public class Bullet {
private static final int HEIGHT = 20; private static final int SPEED = 10;
private static final int WIDTH = 20;
private int x; private static final int HEIGHT = 20;
private int y;
private FrameTank frameTank; private int x;
private Direction direction; private int y;
private boolean live = true; private FrameTank frameTank;
private Direction direction;
public Bullet(int x, int y, Direction direction, FrameTank frameTank) { private boolean live = true;
this.x = x;
this.y = y; public Bullet(int x, int y, Direction direction, FrameTank frameTank) {
this.direction = direction; this.x = x;
this.frameTank = frameTank; this.y = y;
} this.direction = direction;
this.frameTank = frameTank;
public void paint(Graphics g) { }
// 子弹超出界限,从子弹集合中移除
if (!live) { public void paint(Graphics g) {
frameTank.bulletList.remove(this); // 子弹超出界限,从子弹集合中移除
} if (!live) {
frameTank.bulletList.remove(this);
Color color = g.getColor(); }
g.setColor(Color.RED);
g.fillOval(x, y, WIDTH, HEIGHT); Color color = g.getColor();
g.setColor(color); g.setColor(Color.RED);
move(); g.fillOval(x, y, WIDTH, HEIGHT);
} g.setColor(color);
move();
private void move() { }
// 定义坦克的移动特征
switch (direction) { private void move() {
case LEFT: // 定义坦克的移动特征
x -= SPEED; switch (direction) {
break; case LEFT:
case UP: x -= SPEED;
y -= SPEED; break;
break; case UP:
case RIGHT: y -= SPEED;
x += SPEED; break;
break; case RIGHT:
case DOWN: x += SPEED;
y += SPEED; break;
break; case DOWN:
} y += SPEED;
break;
// 子弹超出临界范围 }
if (x < 0 || y < 0 || y > FrameTank.GAME_HEIGHT || x > FrameTank.GAME_WIDTH) {
live = false; // 子弹超出临界范围
} if (x < 0 || y < 0 || y > FrameTank.GAME_HEIGHT || x > FrameTank.GAME_WIDTH) {
} live = false;
} }
}
}

@ -1,8 +1,10 @@
/** package com.mashibing.tank;
* @Description:
* @Author: zhanxinlin /**
* @Date: 2022-09-27 22:21 * @Description:
*/ * @Author: zhanxinlin
public enum Direction { * @Date: 2022-09-27 22:21
LEFT, UP, RIGHT, DOWN */
} public enum Direction {
LEFT, UP, RIGHT, DOWN
}

@ -1,161 +1,163 @@
import java.awt.*; package com.mashibing.tank;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.*;
import java.awt.event.WindowAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.WindowEvent; import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.awt.event.WindowAdapter;
import java.util.List; import java.awt.event.WindowEvent;
import java.util.ArrayList;
/** import java.util.List;
* @Description:
* @Author: zhanxinlin /**
* @Date: 2022-09-27 21:34 * @Description:
*/ * @Author: zhanxinlin
public class FrameTank extends Frame{ * @Date: 2022-09-27 21:34
static final int GAME_WIDTH = 800; */
static final int GAME_HEIGHT = 600; public class FrameTank extends Frame{
static final int GAME_WIDTH = 800;
Tank myTank = new Tank(200 ,200, Direction.DOWN, this); static final int GAME_HEIGHT = 600;
List<Bullet> bulletList = new ArrayList<>(); // 用集合来装子弹
Tank myTank = new Tank(200 ,200, Direction.DOWN, this);
// 构造方法 List<Bullet> bulletList = new ArrayList<>(); // 用集合来装子弹
public FrameTank() {
this.setSize(GAME_WIDTH, GAME_HEIGHT); // 构造方法
this.setResizable(false); // 窗口大小不可拉伸 public FrameTank() {
this.setVisible(true); // 窗口可见 this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("tank war"); this.setResizable(false); // 窗口大小不可拉伸
this.setVisible(true); // 窗口可见
// 窗口监听器,点击"×",则关闭窗口,停止程序 this.setTitle("tank war");
this.addWindowListener(new WindowAdapter() {
@Override // 窗口监听器,点击"×",则关闭窗口,停止程序
public void windowClosing(WindowEvent e) { this.addWindowListener(new WindowAdapter() {
System.exit(0); @Override
} public void windowClosing(WindowEvent e) {
}); System.exit(0);
}
// 键盘按钮监听器 });
this.addKeyListener(new MyKeyListener());
} // 键盘按钮监听器
this.addKeyListener(new MyKeyListener());
Image offScreenImage = null; }
/** Image offScreenImage = null;
*
* /**
* @param g *
*/ *
@Override * @param g
public void update(Graphics g) { */
if (offScreenImage == null) { @Override
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT); public void update(Graphics g) {
} if (offScreenImage == null) {
Graphics gOffScreen = offScreenImage.getGraphics(); offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
Color c = gOffScreen.getColor(); }
gOffScreen.setColor(Color.BLACK); Graphics gOffScreen = offScreenImage.getGraphics();
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); Color c = gOffScreen.getColor();
gOffScreen.setColor(c); gOffScreen.setColor(Color.BLACK);
paint(gOffScreen); gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
g.drawImage(offScreenImage, 0, 0, null); gOffScreen.setColor(c);
} paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
/** }
*
* /**
* @param g *
*/ *
@Override * @param g
public void paint(Graphics g) { */
Color color = g.getColor(); @Override
g.setColor(Color.WHITE); public void paint(Graphics g) {
g.drawString("子弹的数量:" + bulletList.size(), 10, 60); Color color = g.getColor();
g.setColor(color); g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bulletList.size(), 10, 60);
// 坦克把自己画出来 g.setColor(color);
myTank.paint(g);
// 坦克把自己画出来
// 子弹把自己画出来 myTank.paint(g);
for (int i = 0; i < bulletList.size(); i++) {
bulletList.get(i).paint(g); // 子弹把自己画出来
} for (int i = 0; i < bulletList.size(); i++) {
} bulletList.get(i).paint(g);
}
class MyKeyListener extends KeyAdapter { }
boolean leftDirection = false;
boolean upDirection = false; class MyKeyListener extends KeyAdapter {
boolean rightDirection = false; boolean leftDirection = false;
boolean downDirection = false; boolean upDirection = false;
boolean rightDirection = false;
@Override boolean downDirection = false;
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode(); @Override
switch (key) { public void keyPressed(KeyEvent e) {
case KeyEvent.VK_LEFT: int key = e.getKeyCode();
leftDirection = true; switch (key) {
break; case KeyEvent.VK_LEFT:
case KeyEvent.VK_UP: leftDirection = true;
upDirection = true; break;
break; case KeyEvent.VK_UP:
case KeyEvent.VK_RIGHT: upDirection = true;
rightDirection = true; break;
break; case KeyEvent.VK_RIGHT:
case KeyEvent.VK_DOWN: rightDirection = true;
downDirection = true; break;
break; case KeyEvent.VK_DOWN:
case KeyEvent.VK_CONTROL: downDirection = true;
myTank.fire(); break;
break; case KeyEvent.VK_CONTROL:
default: myTank.fire();
break; break;
} default:
break;
setMainTankDirection(); }
}
setMainTankDirection();
@Override }
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode(); @Override
switch (key) { public void keyReleased(KeyEvent e) {
case KeyEvent.VK_LEFT: int key = e.getKeyCode();
leftDirection = false; switch (key) {
break; case KeyEvent.VK_LEFT:
case KeyEvent.VK_UP: leftDirection = false;
upDirection = false; break;
break; case KeyEvent.VK_UP:
case KeyEvent.VK_RIGHT: upDirection = false;
rightDirection = false; break;
break; case KeyEvent.VK_RIGHT:
case KeyEvent.VK_DOWN: rightDirection = false;
downDirection = false; break;
break; case KeyEvent.VK_DOWN:
default: downDirection = false;
break; break;
} default:
break;
setMainTankDirection(); }
}
setMainTankDirection();
/** }
*
*/ /**
private void setMainTankDirection() { *
if (!leftDirection && !upDirection && !downDirection && !rightDirection) { // 没有按方向键,静止不动 */
myTank.setMoving(false); private void setMainTankDirection() {
} else { // 按了方向键 if (!leftDirection && !upDirection && !downDirection && !rightDirection) { // 没有按方向键,静止不动
myTank.setMoving(true); myTank.setMoving(false);
} else { // 按了方向键
if (leftDirection) { myTank.setMoving(true);
myTank.setDirection(Direction.LEFT);
} if (leftDirection) {
if (rightDirection) { myTank.setDirection(Direction.LEFT);
myTank.setDirection(Direction.RIGHT); }
} if (rightDirection) {
if (upDirection) { myTank.setDirection(Direction.RIGHT);
myTank.setDirection(Direction.UP); }
} if (upDirection) {
if (downDirection) { myTank.setDirection(Direction.UP);
myTank.setDirection(Direction.DOWN); }
} if (downDirection) {
} myTank.setDirection(Direction.DOWN);
} }
} }
} }
}
}

@ -1,15 +1,17 @@
/** package com.mashibing.tank;
* @Description:
* @Author: zhanxinlin /**
* @Date: 2022-09-27 22:00 * @Description:
*/ * @Author: zhanxinlin
public class Main { * @Date: 2022-09-27 22:00
public static void main(String[] args) throws InterruptedException { */
FrameTank frameTank = new FrameTank(); public class Main {
public static void main(String[] args) throws InterruptedException {
while (true) { FrameTank frameTank = new FrameTank();
Thread.sleep(50);
frameTank.repaint(); // 重画窗口重新调用paint方法 while (true) {
} Thread.sleep(50);
} frameTank.repaint(); // 重画窗口重新调用paint方法
} }
}
}

@ -0,0 +1,29 @@
package com.mashibing.tank;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-28 23:14
*/
public class ResourceImageRead {
public static BufferedImage TANK_LEFT;
public static BufferedImage TANK_UP;
public static BufferedImage TANK_RIGHT;
public static BufferedImage TANK_DOWN;
// 当ResourceImageRead类加载时静态代码块执行直接初始化
static {
try {
TANK_LEFT = ImageIO.read(ResourceImageRead.class.getClassLoader().getResourceAsStream("images/tankL.gif"));
TANK_UP = ImageIO.read(ResourceImageRead.class.getClassLoader().getResourceAsStream("images/tankU.gif"));
TANK_RIGHT = ImageIO.read(ResourceImageRead.class.getClassLoader().getResourceAsStream("images/tankR.gif"));
TANK_DOWN = ImageIO.read(ResourceImageRead.class.getClassLoader().getResourceAsStream("images/tankD.gif"));
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -1,86 +1,101 @@
import java.awt.*; package com.mashibing.tank;
/** import java.awt.*;
* @Description:
* @Author: zhanxinlin /**
* @Date: 2022-09-27 22:49 * @Description:
*/ * @Author: zhanxinlin
public class Tank { * @Date: 2022-09-27 22:49
private static final int SPEED = 5; */
private int x; public class Tank {
private int y; private static final int SPEED = 5;
private Direction direction; private int x;
private boolean moving = false; private int y;
private FrameTank frameTank = null; private Direction direction;
private boolean moving = false;
public Tank(int x, int y, Direction direction, FrameTank frameTank) { private FrameTank frameTank = null;
this.x = x;
this.y = y; public Tank(int x, int y, Direction direction, FrameTank frameTank) {
this.direction = direction; this.x = x;
this.frameTank = frameTank; this.y = y;
} this.direction = direction;
this.frameTank = frameTank;
/** }
* tank
* /**
* @param g * tank
*/ *
public void paint(Graphics g) { * @param g
Color color = g.getColor(); */
g.setColor(Color.GREEN); public void paint(Graphics g) {
g.fillRect(x, y, 50, 50); // 画出坦克前后左右移动的图片
g.setColor(color); switch (direction) {
move(); case LEFT:
} g.drawImage(ResourceImageRead.TANK_LEFT, x, y, null);
break;
/** case UP:
* g.drawImage(ResourceImageRead.TANK_UP, x, y, null);
*/ break;
private void move() { case RIGHT:
if (!moving) { g.drawImage(ResourceImageRead.TANK_RIGHT, x, y, null);
return; break;
} case DOWN:
g.drawImage(ResourceImageRead.TANK_DOWN, x, y, null);
// 定义坦克的移动特征 break;
switch (direction) { }
case LEFT:
x -= SPEED; // 坦克移动
break; move();
case UP: }
y -= SPEED;
break; /**
case RIGHT: *
x += SPEED; */
break; private void move() {
case DOWN: if (!moving) {
y += SPEED; return;
break; }
}
} // 定义坦克的移动特征
switch (direction) {
/** case LEFT:
* x -= SPEED;
* newbulletFrameTankbullet break;
* FrameTank case UP:
*/ y -= SPEED;
public void fire() { break;
// 将子弹加入子弹集合 case RIGHT:
frameTank.bulletList.add(new Bullet(this.x, this.y, this.direction, this.frameTank)); x += SPEED;
} break;
case DOWN:
public Direction getDirection() { y += SPEED;
return direction; break;
} }
}
public void setDirection(Direction direction) {
this.direction = direction; /**
} *
* newbulletFrameTankbullet
public boolean isMoving() { * FrameTank
return moving; */
} public void fire() {
// 将子弹加入子弹集合
public void setMoving(boolean moving) { frameTank.bulletList.add(new Bullet(this.x, this.y, this.direction, this.frameTank));
this.moving = moving; }
}
} public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 824 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 868 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 936 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 855 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 852 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 847 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1022 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 814 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 460 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 565 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 846 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 864 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 863 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 861 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 863 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 673 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 670 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 674 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

@ -0,0 +1,33 @@
package test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.junit.jupiter.api.Test;
/**
*
*/
class ImageTest {
@Test
void test() {
try {
// 读取图片路径写死,不推荐
// BufferedImage image = ImageIO.read(new File("C:/work/javaprojects/Tank_60/src/images/bulletD.gif"));
// assertNotNull(image);
BufferedImage image2 = ImageIO.read(ImageTest.class.getClassLoader().getResourceAsStream("images/bulletD.gif"));
assertNotNull(image2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Loading…
Cancel
Save