J2SE 坦克大战 马士兵(4)
2012-03-15 20:24
507 查看
项目4要求
代码如下:
public class TankClient extends Frame{
int x = 50;
int y = 50 ;
public void lanuchFrame(){
this.setLocation(300,200);
this.setSize(800,600);
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Frame f =(Frame)e.getSource();
f.setVisible(false);
}
});
new Thread(new PaintThread()).start();
}
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 static void main(String[] args) {
new TankClient().lanuchFrame();
}
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
//repaint方法,通过一个线程来不断地重画,调用的顺序为repaint ->update(Graphics g)-> paint(Graphics g)
//并且由于该方法是内部类,可以直接访问外部类的属性与方法,因为生成内部类的对象
//启动线程的方法:实现Runnable或者继承Thread。
代码如下:
public class TankClient extends Frame{
int x = 50;
int y = 50 ;
public void lanuchFrame(){
this.setLocation(300,200);
this.setSize(800,600);
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Frame f =(Frame)e.getSource();
f.setVisible(false);
}
});
new Thread(new PaintThread()).start();
}
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 static void main(String[] args) {
new TankClient().lanuchFrame();
}
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
//repaint方法,通过一个线程来不断地重画,调用的顺序为repaint ->update(Graphics g)-> paint(Graphics g)
//并且由于该方法是内部类,可以直接访问外部类的属性与方法,因为生成内部类的对象
//启动线程的方法:实现Runnable或者继承Thread。
相关文章推荐
- J2SE 坦克大战 马士兵(5)
- J2SE 坦克大战 马士兵(5)
- J2SE 坦克大战 马士兵(1)
- J2SE 坦克大战 马士兵(2)
- J2SE 坦克大战 马士兵(3)
- 马士兵的坦克大战,以前做过,今天又找出来看了看
- nyoj 284-坦克大战
- 脚本游戏之四: 坦克大战源码注释(待续。。。)
- 自学Unity笔记 坦克大战项目
- java坦克大战0.3(画出代表坦克的实心圆)
- 坦克大战 python 游戏
- 坦克大战,使敌人坦克随机运动
- cocos2d-x的初步学习十六之坦克大战三
- nyo-284-坦克大战--(bfs+优先队列)
- NUOJ 284 坦克大战【BFS】
- 记录Java学习过程之坦克大战1
- NYOJ 284--坦克大战【BFS && 优先队列】
- java视频 坦克大战制作
- 坦克大战中,给子弹加载图片
- NYOJ-284 坦克大战 ------BFS/优先队列