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

master
zhanxinlin 3 years ago
parent 21229d3a8d
commit ab9d2e9839

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

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

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

@ -1,15 +1,17 @@
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-27 22:00
*/
public class Main {
public static void main(String[] args) throws InterruptedException {
FrameTank frameTank = new FrameTank();
while (true) {
Thread.sleep(50);
frameTank.repaint(); // 重画窗口重新调用paint方法
}
}
}
package com.mashibing.tank;
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-27 22:00
*/
public class Main {
public static void main(String[] args) throws InterruptedException {
FrameTank frameTank = new FrameTank();
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.*;
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-27 22:49
*/
public class Tank {
private static final int SPEED = 5;
private int x;
private int y;
private Direction direction;
private boolean moving = false;
private FrameTank frameTank = null;
public Tank(int x, int y, Direction direction, FrameTank frameTank) {
this.x = x;
this.y = y;
this.direction = direction;
this.frameTank = frameTank;
}
/**
* tank
*
* @param g
*/
public void paint(Graphics g) {
Color color = g.getColor();
g.setColor(Color.GREEN);
g.fillRect(x, y, 50, 50);
g.setColor(color);
move();
}
/**
*
*/
private void move() {
if (!moving) {
return;
}
// 定义坦克的移动特征
switch (direction) {
case LEFT:
x -= SPEED;
break;
case UP:
y -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case DOWN:
y += SPEED;
break;
}
}
/**
*
* newbulletFrameTankbullet
* FrameTank
*/
public void fire() {
// 将子弹加入子弹集合
frameTank.bulletList.add(new Bullet(this.x, this.y, this.direction, this.frameTank));
}
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;
}
}
package com.mashibing.tank;
import java.awt.*;
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-27 22:49
*/
public class Tank {
private static final int SPEED = 5;
private int x;
private int y;
private Direction direction;
private boolean moving = false;
private FrameTank frameTank = null;
public Tank(int x, int y, Direction direction, FrameTank frameTank) {
this.x = x;
this.y = y;
this.direction = direction;
this.frameTank = frameTank;
}
/**
* tank
*
* @param g
*/
public void paint(Graphics g) {
// 画出坦克前后左右移动的图片
switch (direction) {
case LEFT:
g.drawImage(ResourceImageRead.TANK_LEFT, x, y, null);
break;
case UP:
g.drawImage(ResourceImageRead.TANK_UP, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceImageRead.TANK_RIGHT, x, y, null);
break;
case DOWN:
g.drawImage(ResourceImageRead.TANK_DOWN, x, y, null);
break;
}
// 坦克移动
move();
}
/**
*
*/
private void move() {
if (!moving) {
return;
}
// 定义坦克的移动特征
switch (direction) {
case LEFT:
x -= SPEED;
break;
case UP:
y -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case DOWN:
y += SPEED;
break;
}
}
/**
*
* newbulletFrameTankbullet
* FrameTank
*/
public void fire() {
// 将子弹加入子弹集合
frameTank.bulletList.add(new Bullet(this.x, this.y, this.direction, this.frameTank));
}
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