代码拉取完成,页面将自动刷新
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);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。