java项目之——坦克大战 04
2016-08-12 16:18
465 查看
功能:让坦克动起来
内容:改变位置,坦克就会动。a.设置成员变量,x , y;
b.每一段时间重画一次:y+=5;
c.重画线程类。(优点:线程重画坦克,比较均匀。)
public class TankClient extends Frame {
int x = 30; int y = 30; //定义在方法外面
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 40);
g.setColor(c);
y += 5;
}
public void lauchFrame(){
this.setSize(800,600);
this.setTitle("TankWar");
this.setLocation(80, 60);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
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 (Exception e) { e.printStackTrace(); } } } }
}
关键代码:线程。
启动线程:
new Thread(new paintThread()).start();
内容:改变位置,坦克就会动。a.设置成员变量,x , y;
b.每一段时间重画一次:y+=5;
c.重画线程类。(优点:线程重画坦克,比较均匀。)
public class TankClient extends Frame {
int x = 30; int y = 30; //定义在方法外面
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 40);
g.setColor(c);
y += 5;
}
public void lauchFrame(){
this.setSize(800,600);
this.setTitle("TankWar");
this.setLocation(80, 60);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
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 (Exception e) { e.printStackTrace(); } } } }
}
关键代码:线程。
private class paintThread implements Runnable { //线程 内部类 为此线程服务 public void run() { while(true){ repaint(); try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } } } }实现Runnable接口,repaint()方法一直重画,延时时间sleep(50)
启动线程:
new Thread(new paintThread()).start();
相关文章推荐
- Spring框架学习之第1节
- Caused by: java.lang.SecurityException: Prohibited package name: java.util.xsd
- Spring Aop的实现
- java实习生的成长之路<转>
- 安卓中碰到的JAVA引用传递问题
- 源码分析-java-Vector、Stack-与ArrayList的相关性
- Java中OutOfMemoryError
- 《Pro Java 7 NIO.2 》 The Asychronous Channel API 部分翻译
- spring security @PreAuthorize在controller中配置失效解决方法
- Spring常用注解
- java几种经典排序
- zz java反射性能低?如何优化
- Eclipse 直接开发HANA U…
- Java AIO 认识
- HANA Studio (Eclipse…
- Spring mvc / 和 /*的区别
- java死锁的一个例子
- java Gson java代码转字符串、对象使用
- eclipse中ant打包,报异常解决办法
- PAT乙级(Basic Level)1016(Java)