我的笔记 坦克大战.docx
《我的笔记 坦克大战.docx》由会员分享,可在线阅读,更多相关《我的笔记 坦克大战.docx(21页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、版本0.1 产生一个800*600的窗口。 功能: 产生一个窗口 掌握: 通过Eclipse建立新的项目 为新的项目指定不同的源代码和输出目录 指定项目所用的JDK版本 通过Eclipse建立新的类 注意: 类名和方法名的命名 见名知意 类名首字母大写 方法名、变量名首字母小写 应用驼峰标识名字是TankClient。尽量使用一个类从Frame继承的方式来创建GUI.import java.awt.*;public class TankClient extends Frame public void lauchFrame() this.setLocation(400, 300);this.se
2、tSize(800, 600);setVisible(true);public static void main(String args) TankClient tc = new TankClient();tc.lauchFrame();/注意使用这种方法去创建GUI版本0.2 功能: 添加关闭窗口的事件处理 不允许窗口的大小改动 setResizable 掌握: 匿名类的用法 思考:匿名类的应用场合? 类短小、不涉及将来的扩展、不涉及重要的业务逻辑 通过Eclipse重写父类的方法import java.awt.*;import java.awt.event.*; /加入了awt.event
3、处理window关闭时间public class TankClient extends Frame public void lauchFrame() this.setLocation(400, 300);this.setSize(800, 600);this.setTitle(TankWar); /改变关闭窗口的事件处理this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););this.setResizable(false); /不允许窗口的大小改变s
4、etVisible(true);public static void main(String args) TankClient tc = new TankClient();tc.lauchFrame();版本0.3 功能: 画出代表坦克的实心圆 掌握: 如何重写paint方法 注意: 不要改变原来的前景色 回顾: paint(Graphics g)方法,窗口重画时自动调用 x轴、y轴的方向import java.awt.*;import java.awt.event.*;public class TankClient extends Frame public void paint(Graphi
5、cs g) Color c = g.getColor();g.setColor(Color.RED);g.fillOval(50, 50, 30, 30);g.setColor(c);public void lauchFrame() this.setLocation(400, 300);this.setSize(800, 600);this.setTitle(TankWar);this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););this.setR
6、esizable(false);this.setBackground(Color.GREEN);setVisible(true);public static void main(String args) TankClient tc = new TankClient();tc.lauchFrame();版本0.4 功能: 让坦克运动起来 步骤: 将位置改变为变量 启动线程不断重画 思考:为什么使用线程重画,而不是每按下一个键进行一次重画? 线程重画更均匀,更能控制重画的速度。 按键重画不能解决子弹自动飞行的问题。 每次重画改变Tank位置 掌握: 内部类的使用 思考:内部类有哪些好处?什么时候使
7、用内部类? 可以方便的访问包装类的方法。不方便公开的,只为包装类服务的类应当定义为内部类。 注意: x, y值得含义,指的是小方块的左上角点import java.awt.*;import java.awt.event.*;public class TankClient extends Frame int x = 50, y = 50; /动起来,就是改变坦克的位置,所以,定义位置public void paint(Graphics g) Color c = g.getColor();g.setColor(Color.RED);g.fillOval(x, y, 30, 30); /定义它的初始
8、位置g.setColor(c);y += 5; /每次repaint当中的位置都在变化public void lauchFrame() this.setLocation(400, 300);this.setSize(800, 600);this.setTitle(TankWar);this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););this.setResizable(false);this.setBackground(Color.GREEN);s
9、etVisible(true);new Thread(new PaintThread().start(); /使用线程让frame不停重画public static void main(String args) TankClient tc = new TankClient();tc.lauchFrame();/内部类,因为这个线程类只为这里服务,所以用内部类最合适,而且还是privateprivate class PaintThread implements Runnable public void run() while(true) repaint();try Thread.sleep(10
10、0); catch (InterruptedException e) e.printStackTrace();版本0.41 功能 使用双缓冲消除闪烁现象 原因 刷新重画频率太快,paint方法还没有完成 逐条显示 解决办法 将所有东西画在虚拟图片上,一次性显示出来 注意 如果不能理解透彻就照抄本版本代码,不影响对J2SE的练习功效import java.awt.*;import java.awt.event.*;public class TankClient extends Frame int x = 50, y = 50;Image offScreenImage = null; / 虚拟图片
11、public void paint(Graphics g) Color c = g.getColor();g.setColor(Color.RED);g.fillOval(x, y, 30, 30);g.setColor(c);y += 5;public void update(Graphics g) if(offScreenImage = null) offScreenImage = this.createImage(800, 600);Graphics gOffScreen = offScreenImage.getGraphics();Color c = gOffScreen.getCol
12、or();gOffScreen.setColor(Color.GREEN);gOffScreen.fillRect(0, 0, 800, 600);gOffScreen.setColor(c);paint(gOffScreen);g.drawImage(offScreenImage, 0, 0, null);public void lauchFrame() this.setLocation(400, 300);this.setSize(800, 600);this.setTitle(TankWar);this.addWindowListener(new WindowAdapter() publ
13、ic void windowClosing(WindowEvent e) System.exit(0););this.setResizable(false);this.setBackground(Color.GREEN);setVisible(true);new Thread(new PaintThread().start();public static void main(String args) TankClient tc = new TankClient();tc.lauchFrame();private class PaintThread implements Runnable pub
14、lic void run() while(true) repaint();try Thread.sleep(50); catch (InterruptedException e) e.printStackTrace(); 把游戏窗口的大小改变为640 X 480? 定义常量以应付将来的改动版本0.5 功能: 代码重构 将以后可能需要多处改变的量定义为常量 Frame的宽度和高度 常量名一般大写 注意: 常量一般是public static final的。import java.awt.*;import java.awt.event.*;public class TankClient exten
15、ds Frame public static final int GAME_WIDTH = 800;public static final int GAME_HEIGHT = 600;int x = 50, y = 50;Image offScreenImage = null;public void paint(Graphics g) Color c = g.getColor();g.setColor(Color.RED);g.fillOval(x, y, 30, 30);g.setColor(c);y += 5;public void update(Graphics g) if(offScr
16、eenImage = 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);paint(gOffScreen);g.drawImage(offS
17、creenImage, 0, 0, null);public void lauchFrame() this.setLocation(400, 300);this.setSize(GAME_WIDTH, GAME_HEIGHT);this.setTitle(TankWar);this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););this.setResizable(false);this.setBackground(Color.GREEN);setVi
18、sible(true);new Thread(new PaintThread().start();public static void main(String args) TankClient tc = new TankClient();tc.lauchFrame();private class PaintThread implements Runnable public void run() while(true) repaint();try Thread.sleep(50); catch (InterruptedException e) e.printStackTrace();版本0.6
19、功能: 让坦克听从我们的指挥 添加键盘监听器类KeyMonitor TankCient添加键盘监听器 针对不同的键改变坦克的位置,与重画线程结合产生不同方向运动 注意: switch case语句中break语句的运用 写程序要循序渐进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;int x = 50, y = 50;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 我的笔记 坦克大战 笔记 坦克 大战
限制150内