代码拉取完成,页面将自动刷新
package V11;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TankClient11 extends Frame {
public static final int GAME_WINDTH = 800;//宽度常量
public static final int GAME_HEIGHT = 600;//高度常量
Tank myTank = new Tank(50, 50,this);
Missile m = new Missile(50, 50, Tank.Direction.R);
Image offScreenImage = null;//虚拟图片
public void launchFrame() {
//设置窗口出现的位置
this.setLocation(300, 50);
//设置窗口的宽度高度
this.setSize(GAME_WINDTH, GAME_HEIGHT);
this.setBackground(Color.green);
this.setTitle("TankWar");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
setVisible(true);
this.setBackground(Color.GREEN);
this.addKeyListener(new KeyMonitor());
new Thread(new PaintThread()).start();
}
public void paint(Graphics g) {
myTank.draw(g);
m.draw(g);
}
private class KeyMonitor extends KeyAdapter {
public void keyPressed(KeyEvent e) {
myTank.KyePressed(e);
}
public void keyReleased(KeyEvent e) {
myTank.kyeReleased(e);
}
}
private class PaintThread implements Runnable {
//内部类(a)可以方便的访问包装类的方法。不方便公开的,只为包装类服务的类应当定义为内部类)
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void update(Graphics g) {
if (offScreenImage == null) { //若为null则创建一张图
offScreenImage = this.createImage(GAME_WINDTH, GAME_HEIGHT);
}
//拿到这个图片的画笔
Graphics gOffScreen = offScreenImage.getGraphics();
//默认黑色,所以需要与效果背景一致获取原色
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);//与原背景色一致
//使用画笔画出一个实现的方框代替原画的背景效果
gOffScreen.fillRect(0, 0, GAME_WINDTH, GAME_HEIGHT);
gOffScreen.setColor(c);//设置原回来
//使用虚拟图像的画笔画圆
paint(gOffScreen);
//将虚拟图片画下来
g.drawImage(offScreenImage, 0, 0, null);
}
public static void main(String[] args) {
TankClient11 tc = new TankClient11();
tc.launchFrame();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。