图形界面编程,坦克大战
分类:高并发

//图片 1图片 2

转自:

import java.awt.*;
import java.awt.event.*;

java 坦克大战 子弹击中敌方坦克 子弹消失 坦克不会消失 20C
import java.util.Vector;

import java.awt.Button;

GUI(图形用户界面) 

public class LetterLabel extends Button implements FocusListener,
MouseListener{
    //构造器
    LetterLabel(){
        addFocusListener(this); //将当前对象注册为自身的焦点事件监视器
        addMouseListener(this); //将当前对象注册为自身的鼠标事件监视器
        
        this.setBackground(Color.cyan);//青色
        this.setFont(new Font("",Font.BOLD,32));//字体
    }
    public static LetterLabel[] getLetterLabel(int n){
        LetterLabel a[]=new LetterLabel[n];
        for (int i = 0; i<a.length; i++){
            a[i]=new LetterLabel();
        }
        return a;
    }
    
    public void focusGained(FocusEvent e){
        this.setBackground(Color.red);
    }
    public void focusLost(FocusEvent e){
        this.setBackground(Color.cyan);
    }
    
    public void mousePressed(MouseEvent e){
        this.requestFocus();
    }
/*???????????????????????????????????????????*/    
    public void setText(char c){
        setLabel(""+c);
    }
    public char[] getText(){
        return getLabel().toCharArray();
    }
/*???????????????????????????????????????????*/        
    public void mouseClicked(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}    
    public void mouseExited(MouseEvent e){}
}

public class MyTank {
int x=0;
int y=0;
int direction=0;
int sudu=2;
public MyTank(){

import java.awt.Color;

Java代码

 

}public int getX() { return x;}public void setX { this.x = x;}public int getY() { return y;}public void setY { this.y = y;}public int getDirection() { return direction;}public void setDirection(int direction) { this.direction = direction;}public int getSudu() { return sudu;}public void setSudu { this.sudu = sudu;}public MyTank(int x,int y,int direction,int sudu){ this.x=x; this.y=y; this.direction=direction; this.sudu=sudu;}public MyTank(int x,int y,int direction){ this.x=x; this.y=y; this.direction=direction;}public MyTank(int x,int y){ this.x=x; this.y=y;}public void shang(){ y-=sudu; this.setY;}public void xia(){ y+=sudu; this.setY;}public void zuo(){ x-=sudu; this.setX;}public void you(){ x+=sudu; this.setX;}Zd zd=new Zd(this.getX(),this.getY(),this.direction);Vector<Zd> zds=new Vector<Zd>();//判断发射方向public void fs(){ switch(this.direction){ case 0: zd=new Zd(x+8,y-5,this.direction); zds.add; break; case 1: zd=new Zd(x+8,y+28,this.direction); zds.add; break; case 2: zd=new Zd(x-5,y+8,this.direction); zds.add; break; case 3: zd=new Zd(x+28,y+8,this.direction); zds.add; break; } Thread t=new Thread; t.start();}

import java.awt.Rectangle;

  1 import java.awt.Button;  
  2 import java.awt.FlowLayout;  
  3 import java.awt.Frame;  
  4 import java.awt.event.WindowAdapter;  
  5 import java.awt.event.WindowEvent;  
  6   
  7 /** 
  8  * GUI(图形用户界面) 
  9  *  Graphical User Interface(图形用户接口) 
 10  *  用图形的方式,来显示计算机操作的界面,这样更方便更直观. 
 11  *  
 12  * CLI 
 13  *  Command Line User Interface(命令行用户接口) 
 14  *  就是常用的Dos命令行操作. 
 15  *  需要记忆一些常用的命令.操作更直观. 
 16  *  
 17  * 举例: 
 18  *   比如:创建文件夹,或者删除文件夹等  
 19  *   md haha   del haha   
 20  *    
 21  *    
 22  * Java的GUI提供的对象都存在 java.Awt 和 javax.Swing 两个包中. 
 23  *  
 24  * java.Awt:Abstract Window ToolKit(抽象 窗口工具包) 
 25  *    需要调用本地系统方法实现功能.属重量级控件 (跨平台不够强) 
 26  *   
 27  * java.Swing:在AWT的基础上,建立的一套图形界面系统,器重提供了更多的组件, 
 28  *   而且完全由java实现,增强了移植性,属于轻量级控件.(跨平台很好) 
 29  *    
 30  * java.swt: IBM 公司开发 Eclipse 用的组件工具 可以Eclipse网站下载后就可以使用了. 
 31  *  
 32  *  
 33  * 布局管理器 
 34  * 1)容器中的组件的排放方式,就是布局. 
 35  * 2)常见的布局管理器 
 36  *   FlowLayout(流式布局管理器) 
 37  *     从左到右的顺序排列 
 38  *     Panel默认的布局管理器 
 39  *   BorderLayout(辩解布局管理器) 
 40  *     东  南  西  北   中 
 41  *     Frame 默认的布局管理器 
 42  *     不指定布局方式,默认 满屏覆盖,在添加一个 也是 满屏覆盖 
 43  *   GridLayout (网格布局管理器) 
 44  *     规则的矩阵 
 45  *   CardLayout  (卡片布局管理器) 
 46  *     选项卡 
 47  *   GridBagLayout(网格包布局管理器) 
 48  *    非规则的矩阵 
 49  *     
 50  * 事件监听机制组成 
 51  *  事件源:   
 52  *  事件:Event 
 53  *  监听器:Listener 
 54  *  时间处理:(引发事件后处理方式) 
 55  *   
 56  *  事件源:就是awt包或者swing包中的那些图像界面组件. 
 57  *  事件:每个事件源都有自己特定的对应时间和共性时间. 
 58  *  监听器:可以出发某一个事件的动作都已经封装到监听器中. 
 59  */  
 60   
 61   
 62 public class GuiDemo {  
 63     public static void main(String[] args) {  
 64         Frame f=new Frame("my awt");  
 65         f.setSize(500,400);  
 66         f.setLocation(300,200);  
 67         f.setLayout(new FlowLayout());  
 68           
 69         Button b=new Button("我是一个按钮");  
 70           
 71         f.add(b);  
 72           
 73         f.addWindowListener(new MyWin());  
 74           
 75         f.setVisible(true);  
 76         System.out.println("Hello world!");  
 77     }  
 78   
 79 }  
 80   
 81 //因为接口WindowLinstener中的所有方法都被子类 WindowAdapter实现了,.  
 82 //并且覆盖了其中的所有方法,那么我们只能继承 WindowAdapter 覆盖我们的方法即可  
 83 class MyWin extends WindowAdapter{  
 84       
 85     @Override  
 86     public void windowClosing(WindowEvent e) {  
 87         // TODO Auto-generated method stub  
 88         //System.out.println("Window closing"+e.toString());  
 89         System.out.println("我关了");  
 90         System.exit(0);  
 91     }  
 92     @Override  
 93     public void windowActivated(WindowEvent e) {  
 94         //每次获得焦点 就会触发  
 95         System.out.println("我活了");    
 96         //super.windowActivated(e);  
 97     }  
 98     @Override  
 99     public void windowOpened(WindowEvent e) {  
100         // TODO Auto-generated method stub  
101         System.out.println("我开了");  
102         //super.windowOpened(e);  
103     }  
104       
105 }  

/*产生随机字符组合*/
public class RondomString{
    String str="";
    //获取字符串
    public String getRondomString(String s){
        StringBuffer strBuffer=new StringBuffer(s);
        int m=strBuffer.length();//字符串的长度
        for (int i = 0; i<m; i++){
            int index=(int)(Math.random()*strBuffer.length());
            char c=strBuffer.charAt(index);
            str+=c;
            //移除此序列指定位置的 char。此序列将缩短一个 char。
            strBuffer=strBuffer.deleteCharAt(index);
        }
        return str;
    }
}

}
//子弹类
class Zd implements Runnable{
int x=0;
int y=0;
int direction=0;
int sudu=8;
boolean shengming=true;
public Zd(int x,int y){
this.x=x;
this.y=y;
}
public Zd(int x,int y,int direction){
this.x=x;
this.y=y;
this.direction=direction;
}
public void run(){
while{
try {
Thread.sleep;
} catch (Exception e) {
}
switch(direction){
case 0:
y-=sudu;
break;
case 1:
y+=sudu;
break;
case 2:
x-=sudu;
break;
case 3:
x+=sudu;
break;
}
//判断子弹出界面
if(x500||y400){
this.shengming=false;
}
}

import java.awt.event.ActionEvent;

简单Frame 

 

}

import java.awt.event.ActionListener;

Java代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

}

import java.awt.event.FocusEvent;

 1 import java.awt.Button;  
 2 import java.awt.FlowLayout;  
 3 import java.awt.Frame;  
 4 import java.awt.event.ActionEvent;  
 5 import java.awt.event.ActionListener;  
 6 import java.awt.event.WindowAdapter;  
 7 import java.awt.event.WindowEvent;  
 8   
 9 public class FrameDemo {  
10   
11     //定义该图像中所需要的组件的引用  
12     private Frame f;  
13     private Button but;  
14       
15     FrameDemo(){  
16         init();  
17           
18     }  
19       
20     public void init(){  
21           
22         f=new Frame("my freame");  
23         f.setBounds(300,100,600,500);  
24         f.setLayout(new FlowLayout());  //采用流式布局  
25           
26         but=new  Button("my button");  
27         //将组件添加到 frame中  
28         f.add(but);  
29           
30         //加载一下窗体上的事件.  
31         myEvent();  
32         //显示窗体  
33         f.setVisible(true);  
34     }  
35     private void myEvent(){  
36         f.addWindowListener(new WindowAdapter(){  
37             @Override  
38             public void windowClosing(WindowEvent e) {  
39                 // TODO Auto-generated method stub  
40                 //super.windowClosing(e);  
41                 System.exit(0);  
42             }  
43         });  
44           
45         //让按钮具备退出程序的功能  
46         /* 
47          * 按钮就是事件源 
48          * 那么选择那个监听器呢? 
49          * 通过关闭窗体实例了解到,下个要知道那个组件具备什么样的特有监听器, 
50          * 需要查看该组件对象的功能. 
51          */  
52         //添加一个活动监听  
53         but.addActionListener(new ActionListener() {  
54               
55             @Override  
56             public void actionPerformed(ActionEvent e) {  
57                 // TODO Auto-generated method stub  
58                 System.out.println("退出, 按钮干的");  
59                 System.exit(0);  
60             }  
61         });  
62           
63     }  
64       
65     public static void main(String[] args) {  
66         FrameDemo f=new FrameDemo();  
67     }  
68   
69 }  

public class SpellingWordFrame extends Frame implements KeyListener,
ActionListener{
    //组件声明
    TextField inputWord;
    Button button;
    LetterLabel label[];
    Panel northP,centerP;
    Box wordBox;
    String hintMessage="用鼠标单击字母。按左右键交换字母,将其排列成所输入的单词";
    Label messageLabel=new Label(hintMessage);
    String word="";
    
    //构造器
    SpellingWordFrame(){
        inputWord=new TextField(12);
        button=new Button("确定");
        button.addActionListener(this);//注册事件监听器
        inputWord.addActionListener(this);
        
        //北区面板
        northP=new Panel();
        northP.add(new Label("输入一个英文单词"));
        northP.add(inputWord);
        northP.add(button);
        //中区面板
        centerP=new Panel();
        wordBox=Box.createHorizontalBox();//创建水平Box
        centerP.add(wordBox);
        
        //整个窗体布局设置
        this.add(northP,BorderLayout.NORTH);
        this.add(centerP,BorderLayout.CENTER);
        this.add(messageLabel,BorderLayout.SOUTH);
        //设置坐标和大小
        this.setBounds(200,200,350,180);
        this.setVisible(true);//可见
        this.validate();//验证此容器及其所有子组件。
        
        //响应关闭按钮
        addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
        
    }
    
    //运行时执行下面的操作
    public    void actionPerformed(ActionEvent e){
        word=inputWord.getText();//获取文本中的字符
        int n=word.length();//把字符串的长度赋值给n
        RondomString rondom=new RondomString();
        String randomWord=rondom.getRondomString(word);
        wordBox.removeAll();
        messageLabel.setText(hintMessage);
        
        if(n>0){
            label=LetterLabel.getLetterLabel(n);
            for (int i = 0; i<label.length; i++){
                label[i].addKeyListener(this); //讲当前窗口注册为label[i]的键盘监视器
                label[i].setText(randomWord.charAt(i));  //
                   wordBox.add(label[i]);

//敌方坦克类
public class EnemyTank {
int x=0;
int y=0;
int direction=0;
int sudu=5;
boolean shengming=true;

import java.awt.event.FocusListener;

键盘和鼠标事件 

            }
            this.validate();
            this.inputWord.setText(null);
            this.label[0].requestFocus();
        }
    }
    
    //按下某个键时调用此方法
    public void keyPressed(KeyEvent e){
        LetterLabel sourceLabel=(LetterLabel)e.getSource();
        int index=-1;
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            for (int i = 0; i<label.length; i++){
                if(label[i]==sourceLabel){
                    index=i;
                    break;
                }
            }
////////////////////////////////////////////////////////
                if(index!=0){
                    char temp=label[index].getText()[0];
                    label[index].setText(label[index-1].getText()[0]);
                    label[index-1].setText(temp);
                    label[index-1].requestFocus();
                }
        }else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            for (int i = 0; i<label.length; i++){
                if(label[i]==sourceLabel){
                    index=i;
                    break;
                }
            }
            if(index!=label.length-1){
                char temp=label[index].getText()[0];
                label[index].setText(label[index+1].getText()[0]);
                label[index+1].setText(temp);
                label[index+1].requestFocus();
            }
        }
        this.validate();
    }
    //键入某个键时调用此方法
    public void keyTyped(KeyEvent e){};
    //释放某个键时调用此方法
    public void keyReleased(KeyEvent e){
        String success="";
        for (int i = 0; i<label.length; i++){
            char str=label[i].getText()[0];
            success+=str;
        }
        
        if(success.equals(word)){
            messageLabel.setText("恭喜你,成功了");
            for (int i = 0; i<label.length; i++){
                label[i].removeKeyListener(this);
                label[i].removeFocusListener(label[i]);
                label[i].setBackground(Color.green);
            }
            inputWord.requestFocus();
        }
    }
}

public int getX() { return x;}public void setX { this.x = x;}public int getY() { return y;}public void setY { this.y = y;}public int getDirection() { return direction;}public void setDirection(int direction) { this.direction = direction;}public int getSudu() { return sudu;}public void setSudu { this.sudu = sudu;}public EnemyTank(int x,int y,int direction){ this.x=x; this.y=y; this.direction=direction;}public EnemyTank(int x,int y){ this.x=x; this.y=y;}

import java.awt.event.KeyEvent;

Java代码

 

}

import java.awt.event.KeyListener;

  1 import java.awt.Button;  
  2 import java.awt.FlowLayout;  
  3 import java.awt.Frame;  
  4 import java.awt.TextField;  
  5 import java.awt.event.ActionEvent;  
  6 import java.awt.event.ActionListener;  
  7 import java.awt.event.KeyAdapter;  
  8 import java.awt.event.KeyEvent;  
  9 import java.awt.event.MouseAdapter;  
 10 import java.awt.event.MouseEvent;  
 11 import java.awt.event.WindowAdapter;  
 12 import java.awt.event.WindowEvent;  
 13   
 14 public class MouseAndKeyEvent {  
 15   
 16     private Frame f;  
 17     private Button but;  
 18     private TextField tf;  
 19       
 20     public MouseAndKeyEvent() {  
 21         init();  
 22     }  
 23   
 24     private void init(){  
 25         f=new Frame("me frame");  
 26         f.setBounds(300, 200, 600, 500);  
 27         f.setLayout(new FlowLayout());  
 28           
 29         tf=new TextField(20);  
 30         but=new Button("my Botton");  
 31           
 32         f.add(tf);  
 33         f.add(but);  
 34           
 35         event();  
 36           
 37         f.setVisible(true);  
 38     }  
 39   
 40     private void event(){  
 41         f.addWindowListener(new WindowAdapter() {  
 42             @Override  
 43             public void windowClosing(WindowEvent e) {  
 44                 // TODO Auto-generated method stub  
 45                 System.exit(0);  
 46             }  
 47           
 48         });  
 49         tf.addKeyListener(new KeyAdapter() {  
 50             public void keyPressed(KeyEvent e){  
 51                 int code=e.getKeyCode();  
 52                 if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){  
 53                     System.out.println(code+"....非法的输入");  
 54                     e.consume();  //不执行加入文本框.  
 55                 }  
 56                   
 57             }  
 58               
 59         });  
 60           
 61           
 62           
 63         but.addActionListener(new ActionListener() {  
 64               
 65             @Override  
 66             public void actionPerformed(ActionEvent e) {  
 67                 // TODO Auto-generated method stub  
 68                 System.out.println("actionPerformed 活动一次");  
 69             }  
 70         });  
 71           
 72         but.addMouseListener(new MouseAdapter() {  
 73             private int count=0;  
 74             private int clickCount=1;  
 75             public void mouseEntered(MouseEvent e){  
 76                 System.out.println("鼠标进入到改组件"+count++);  
 77             }  
 78               
 79             public void mouseClicked(MouseEvent e){  
 80                 if(e.getClickCount()==2){  
 81                     System.out.println("双击动作");  
 82                 }else  
 83                     System.out.println("点击动作"+clickCount++);  
 84                   
 85             }  
 86               
 87         });  
 88         //添加 键盘事件  
 89         but.addKeyListener(new KeyAdapter() {  
 90           
 91             public void keyPressed(KeyEvent e){  
 92                 System.out.println(e.getKeyChar()+"..."+e.getKeyCode());  
 93                 System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());  
 94                 //enter  就退出  
 95                 /*if(e.getKeyCode()==KeyEvent.VK_ENTER) 
 96                     System. 
 97                     exit(0);*/  
 98                   
 99                 //ctrl + Enter 发送消息  
100                 if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER){  
101                     System.out.println("我要发送消息!");  
102                 }  
103                   
104             }  
105           
106         });  
107     }  
108   
109     public static void main(String[] args) {  
110         // TODO Auto-generated method stub  
111         new MouseAndKeyEvent();  
112     }  
113   
114 }  

public class WordMianClass{
    
    public static void main(String[] agrs){
        new SpellingWordFrame();    
    }
}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import java.awt.event.MouseEvent;

 

 

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.event.MouseListener;

  1. import java.awt.Button;  
  2. import java.awt.FlowLayout;  
  3. import java.awt.Frame;  
  4. import java.awt.TextField;  
  5. import java.awt.event.ActionEvent;  
  6. import java.awt.event.ActionListener;  
  7. import java.awt.event.KeyAdapter;  
  8. import java.awt.event.KeyEvent;  
  9. import java.awt.event.MouseAdapter;  
  10. import java.awt.event.MouseEvent;  
  11. import java.awt.event.WindowAdapter;  
  12. import java.awt.event.WindowEvent;  
  13.   
  14. public class MouseAndKeyEvent {  
  15.   
  16.     private Frame f;  
  17.     private Button but;  
  18.     private TextField tf;  
  19.       
  20.     public MouseAndKeyEvent() {  
  21.         init();  
  22.     }  
  23.   
  24.     private void init(){  
  25.         f=new Frame("me frame");  
  26.         f.setBounds(300, 200, 600, 500);  
  27.         f.setLayout(new FlowLayout());  
  28.           
  29.         tf=new TextField(20);  
  30.         but=new Button("my Botton");  
  31.           
  32.         f.add(tf);  
  33.         f.add(but);  
  34.           
  35.         event();  
  36.           
  37.         f.setVisible(true);  
  38.     }  
  39.   
  40.     private void event(){  
  41.         f.addWindowListener(new WindowAdapter() {  
  42.             @Override  
  43.             public void windowClosing(WindowEvent e) {  
  44.                 // TODO Auto-generated method stub  
  45.                 System.exit(0);  
  46.             }  
  47.           
  48.         });  
  49.         tf.addKeyListener(new KeyAdapter() {  
  50.             public void keyPressed(KeyEvent e){  
  51.                 int code=e.getKeyCode();  
  52.                 if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){  
  53.                     System.out.println(code+"....非法的输入");  
  54.                     e.consume();  //不执行加入文本框.  
  55.                 }  
  56.                   
  57.             }  
  58.               
  59.         });  
  60.           
  61.           
  62.           
  63.         but.addActionListener(new ActionListener() {  
  64.               
  65.             @Override  
  66.             public void actionPerformed(ActionEvent e) {  
  67.                 // TODO Auto-generated method stub  
  68.                 System.out.println("actionPerformed 活动一次");  
  69.             }  
  70.         });  
  71.           
  72.         but.addMouseListener(new MouseAdapter() {  
  73.             private int count=0;  
  74.             private int clickCount=1;  
  75.             public void mouseEntered(MouseEvent e){  
  76.                 System.out.println("鼠标进入到改组件"+count++);  
  77.             }  
  78.               
  79.             public void mouseClicked(MouseEvent e){  
  80.                 if(e.getClickCount()==2){  
  81.                     System.out.println("双击动作");  
  82.                 }else  
  83.                     System.out.println("点击动作"+clickCount++);  
  84.                   
  85.             }  
  86.               
  87.         });  
  88.         //添加 键盘事件  
  89.         but.addKeyListener(new KeyAdapter() {  
  90.           
  91.             public void keyPressed(KeyEvent e){  
  92.                 System.out.println(e.getKeyChar()+"..."+e.getKeyCode());  
  93.                 System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());  
  94.                 //enter  就退出  
  95.                 /*if(e.getKeyCode()==KeyEvent.VK_ENTER) 
  96.                     System. 
  97.                     exit(0);*/  
  98.                   
  99.                 //ctrl + Enter 发送消息  
  100.                 if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER){  
  101.                     System.out.println("我要发送消息!");  
  102.                 }  
  103.                   
  104.             }  
  105.           
  106.         });  
  107.     }  
  108.   
  109.     public static void main(String[] args) {  
  110.         // TODO Auto-generated method stub  
  111.         new MouseAndKeyEvent();  
  112.     }  
  113.   
  114. }  

public class Tank1 {

import java.awt.event.WindowAdapter;

public static void main(String[] args) { MianBan mb=new MianBan();}

import java.awt.event.WindowEvent;

}
class MianBan extends JFrame{
HuaBi hb=null;
public MianBan(){
this.setSize;
this.setLocation;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

import javax.swing.JFrame;

 hb=new HuaBi(); this.add; this.addKeyListener; this.setVisible;}

public class MoveExample {

}

 /**

class HuaBi extends JPanel implements KeyListener ,Runnable{
MyTank mt=null;
public HuaBi(){
mt=new MyTank;
}

  * @param args

public void paint(Graphics g){ super.paint; g.fillRect(0, 0, 500, 400); //背景 //我放坦克 this.huaTank, mt.getY(), g, 1, this.mt.getDirection; //子弹 for(int i=0;i<mt.zds.size{ Zd zd=mt.zds.get; if(mt.zd!=null&&mt.zd.shengming==true){ g.setColor(Color.white); g.fill3DRect(zd.x,zd.y, 3, 3, false); } if(zd.shengming==false){ mt.zds.remove; } } //敌方坦克 Vector<EnemyTank> dtk=new Vector<EnemyTank>(); EnemyTank et=null; int num=3; for(int i=0;i<num;i++){ et=new EnemyTank*226+6,10,1); dtk.add; } //绘制敌方坦克 for(int i=0;i<dtk.size{ et=dtk.get; if(et!=null&&et.shengming==true){ this.huaTank, et.getY(), g, 0, 1); } if(et.shengming==false){ dtk.remove; } } //比较子弹和坦克坐标 for(int i=0;i<mt.zds.size{ for(int j=0;j<dtk.size{ if(mt.zds.get.shengming==true){ if(dtk.get.shengming==true){ this.jzdf(mt.zds.get, dtk.get; } } } } this.repaint();}//判断子弹是否击中敌方坦克public void jzdf(Zd zd,EnemyTank et){ if(zd.x>et.x&&zd.x<et.x+20&&zd.y>et.y&&zd.y<et.y+30){ zd.shengming=false; et.shengming=false; }}//fx为坦克方向public void huaTank(int x,int y,Graphics g,int color,int fx){ switch{ case 0: g.setColor(Color.green); break; case 1: g.setColor(Color.yellow); break; } //坦克方向 switch{ case 0: //上 g.fill3DRect(x, y, 5, 26, false); g.fill3DRect(x+15, y, 5, 26, false); g.fill3DRect(x+4, y+4, 16, 20, false); g.fillOval(x+4, y+8, 10, 10); g.drawLine(x+9, y+15, x+9, y-4); break; case 1: //下 g.fill3DRect(x, y, 5, 26, false); g.fill3DRect(x+15, y, 5, 26, false); g.fill3DRect(x+4, y+1, 16, 20, false); g.fillOval(x+4, y+5, 10, 10); g.drawLine(x+9, y+15, x+9, y+27); break; case 2: //左 g.fill3DRect(x, y, 26, 5, false); g.fill3DRect(x, y+15, 26, 5, false); g.fill3DRect(x+4, y+4, 20, 16, false); g.fillOval(x+10, y+5, 10, 10); g.drawLine(x+15, y+9, x-4, y+9); break; case 3: //右 g.fill3DRect(x, y, 26, 5, false); g.fill3DRect(x, y+15, 26, 5, false); g.fill3DRect(x+1, y+4, 20, 16, false); g.fillOval(x+5, y+5, 10, 10); g.drawLine(x+15, y+9, x+27, y+9); break; } this.repaint();}public void keyTyped(KeyEvent e) {}public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_W){

  */

// System.out.println;
this.mt.setDirection;
this.mt.shang();
}else if(e.getKeyCode()==KeyEvent.VK_A){
// System.out.println;
this.mt.zuo();
this.mt.setDirection;
}else if(e.getKeyCode()==KeyEvent.VK_D){
// System.out.println;
this.mt.you();
this.mt.setDirection;
}else if(e.getKeyCode()==KeyEvent.VK_S){
// System.out.println;
this.mt.setDirection;
this.mt.xia();
}
if(e.getKeyCode()==KeyEvent.VK_J){
if(mt.zds.size{
this.mt.fs();
}
}
this.repaint();
}
public void keyReleased(KeyEvent e) {

 public static void main(String[] args) {

}@Overridepublic void run() {}

  new Hua_Rong_Road();

}

 }

}

class Person extends Button implements FocusListener {

 int number;

 Color c = new Color(255, 245, 170);

 Person(int number, String s) {

  super(s);

  setBackground(c);

  this.number = number;

  c = getBackground();

  addFocusListener(this);

 }

 public void focusGained(FocusEvent e) {

  setBackground(Color.red);

 }

 public void focusLost(FocusEvent e) {

  setBackground(c);

 }

}

class Hua_Rong_Road extends JFrame implements MouseListener, KeyListener,

  ActionListener {

 Person person[] = new Person[10];

 Button left, right, above, below;

 Button restart = new Button("重新开始");

 public Hua_Rong_Road() {

  init();

  setBounds(100, 200, 320, 360);

  setVisible(true);

  validate();

  addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent e) {

    System.exit(0);

   }

  }

  );

 }

 public void init() {

  setLayout(null);

  add(restart);

  restart.setBounds(100, 320, 120, 25);

  restart.addActionListener(this);

  String name[] = { "曹操", "关羽", "张飞", "刘备", "赵云", "黄忠", "兵", "兵", "兵",

    "兵" };

  for (int k = 0; k < name.length; k++) {

   person[k] = new Person(k, name[k]);

   person[k].addMouseListener(this);

   person[k].addKeyListener(this);

   add(person[k]);

  }

  person[0].setBounds(104, 54, 100, 100);

  person[1].setBounds(104, 154, 100, 50);

  person[2].setBounds(54, 154, 50, 100);

  person[3].setBounds(204, 154, 50, 100);

  person[4].setBounds(54, 54, 50, 100);

  person[5].setBounds(204, 54, 50, 100);

  person[6].setBounds(54, 254, 50, 50);

  person[7].setBounds(204, 254, 50, 50);

  person[8].setBounds(104, 204, 50, 50);

  person[9].setBounds(154, 204, 50, 50);

  person[9].requestFocus();

  left = new Button();

  right = new Button();

  above = new Button();

  below = new Button();

  add(left);

  add(right);

  add(above);

  add(below);

  left.setBounds(49, 49, 5, 260);

  right.setBounds(254, 49, 5, 260);

  above.setBounds(49, 49, 210, 5);

  below.setBounds(49, 304, 210, 5);

  validate();

 }

 public void keyTyped(KeyEvent e) {

 }

 public void keyReleased(KeyEvent e) {

 }

 public void keyPressed(KeyEvent e)

 {

  Person man = (Person) e.getSource();

  if (e.getKeyCode() == KeyEvent.VK_DOWN) {

   go(man, below);

  }

  if (e.getKeyCode() == KeyEvent.VK_UP) {

   go(man, above);

  }

  if (e.getKeyCode() == KeyEvent.VK_LEFT) {

   go(man, left);

  }

  if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

   go(man, right);

  }

 }

 public void mousePressed(MouseEvent e) {

  Person man = (Person) e.getSource();

  int x = -1, y = -1;

  x = e.getX();

  y = e.getY();

  int w = man.getBounds().width;

  int h = man.getBounds().height;

  if (y > h / 2) {

   go(man, below);

  }

  if (y < h / 2) {

   go(man, above);

  }

  if (x < w / 2) {

   go(man, left);

  }

  if (x > w / 2) {

   go(man, right);

  }

 }

 public void mouseReleased(MouseEvent e) {

 }

 public void mouseEntered(MouseEvent e) {

 }

 public void mouseClicked(MouseEvent e) {

 }

 public void go(Person man, Button direction) {

  boolean move = true;

  Rectangle manRect = man.getBounds();

  int x = man.getBounds().x;

  int y = man.getBounds().y;

  if (direction == below)

   y = y + 50;

  else if (direction == above)

   y = y - 50;

  else if (direction == left)

   x = x - 50;

  else if (direction == right)

   x = x + 50;

  manRect.setLocation(x, y);

  Rectangle directionRect = direction.getBounds();

  for (int k = 0; k < 10; k++) {

   Rectangle personRect = person[k].getBounds();

   if ((manRect.intersects(personRect)) && (man.number != k)) {

    move = false;

   }

  }

  if (manRect.intersects(directionRect)) {

   move = false;

  }

  if (move == true) {

   man.setLocation(x, y);

  }

 }

 public void actionPerformed(ActionEvent e) {

  dispose();

  new Hua_Rong_Road();

 }

 public void mouseExited(MouseEvent e) {

  // TODO 自动生成方法存根

 }

}

/*<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

 <HEAD>

  <TITLE> New Document </TITLE>

  <META NAME="Generator" CONTENT="EditPlus">

  <META NAME="Author" CONTENT="">

  <META NAME="Keywords" CONTENT="">

  <META NAME="Description" CONTENT="">

 </HEAD>

 <BODY>

  <APPLET CODE="Hua_Rong_Road .class" HEIGHT=200 WIDTH=200></APPLET>

 </BODY>

</HTML>

*/

本文由10bet手机官网发布于高并发,转载请注明出处:图形界面编程,坦克大战

上一篇:没有了 下一篇:没有了
猜你喜欢
热门排行
精彩图文