1 Star 0 Fork 0

OOSAD-软工21011-21012/赵嘉晨21006330115

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TankClient.java 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
ZhaoJiachen 提交于 2023-04-19 17:07 . version 0.7重要版本
import java.awt.*;
import java.awt.event.*;
public class TankClient extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
Tank myTank = new Tank(50, 50);
//这是一张虚拟图片
Image offScreenImage = null;
//绘制坦克
public void paint(Graphics g) {
myTank.draw(g);
}
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.GREEN);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
print(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
//绘制窗口
public void launchFrame() {
this.setLocation(300, 50);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
addWindowListener(new WindowAdapter() {//关闭窗口
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
this.setBackground(Color.GREEN);
this.addKeyListener(new KeyMonitor());
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.launchFrame();
}
private class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
//添加键盘监听器
private class KeyMonitor extends KeyAdapter {
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/oosad-soft-work-21011-21012/zhao-jiachen-21006330115.git
git@gitee.com:oosad-soft-work-21011-21012/zhao-jiachen-21006330115.git
oosad-soft-work-21011-21012
zhao-jiachen-21006330115
赵嘉晨21006330115
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385