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

master
zhanxinlin 3 years ago
parent 21229d3a8d
commit ab9d2e9839

@ -1,3 +1,5 @@
package com.mashibing.tank;
import java.awt.*;
/**

@ -1,3 +1,5 @@
package com.mashibing.tank;
/**
* @Description:
* @Author: zhanxinlin

@ -1,3 +1,5 @@
package com.mashibing.tank;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

@ -1,3 +1,5 @@
package com.mashibing.tank;
/**
* @Description:
* @Author: zhanxinlin

@ -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,3 +1,5 @@
package com.mashibing.tank;
import java.awt.*;
/**
@ -26,10 +28,23 @@ public class 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);
// 画出坦克前后左右移动的图片
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();
}

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