【 java版坦克大战--事件处理】 坦克动起来了

折腾了这么久,坦克总算能动了。只贴代码编辑不给上首页,花了半个小时的时间写了n多注释。

再顺便把绘图的原理发在这里:

绘图原理

Component类提供了两个和绘图有关的重要方法:

①   paint(Graphics g)绘制组件的外观.

②   repaint()刷新组件的外观

当组件第一次在屏幕显示的时候,程序会自动调用paint()方法绘制组件.

类 Panel

java.lang.Object

  java.awt.Component

      java.awt.Container

          java.awt.Panel

在上一节种

public MyPanel extends Panel{

         public void paint(Graphics g){

                   super.paint(g);

                   g.drawRect();

                   …

         }

}

现在可以为什么MyPanel继承了Panel就会有paint方法,可以画图了.

在以下情况下paint()方法将会被调用.

1.窗口最小化,再最大化.

2.窗口大小发生变化.

3.repaint函数被调用.

事件监听的步骤:

一个类实现监听的步骤:

  1. 实现相应的接口(KeyListener,MouseListener,ActionListener,WindowListener)
  2. 实现对事件的处理方法
  3. 在事件源上注册监听

MyTankGame2

  1 /**
  2  * 坦克游戏的2.0版
  3  * 1.画出坦克
  4  * 2.我的坦克可以上下左右移动
  5  */
  6 package com.test5;
  7 
  8 import java.awt.*;
  9 import java.awt.event.*;
 10 import javax.swing.*;
 11 
 12 public class MyTankGame2 extends JFrame {
 13     
 14     MyPanel mp = null;
 15     public static void main(String[] args) {
 16         MyTankGame2 myTankGame2 = new MyTankGame2();
 17     }
 18     //构造函数
 19     public MyTankGame2(){
 20         mp = new MyPanel();
 21         this.add(mp);
 22         //c)在事件源上注册监听,mp为监听器
 23         this.addKeyListener(mp);
 24         
 25         this.setSize(400,300);
 26         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 27         this.setVisible(true);
 28     }
 29 }
 30 /*
 31  * 我的面板,不清楚为什么MyPanel继承Panel监听没有效果。而继承JPanel则正常。有知道的大神告诉我下,先谢谢了。
 32  */
 33 class MyPanel extends Panel implements KeyListener{ //a)实现键盘监听接口
 34     //定义一个我的坦克 
 35     Hero hero = null; //创建工作放在构造函数
 36     public MyPanel(){
 37         hero = new Hero(100,100);
 38     }
 39     //重写paint
 40     public void paint(Graphics g){
 41         super.paint(g);
 42         //将活动区域设置背景为黑色
 43         g.fillRect(0, 0, 400, 300);
 44         //画出我的坦克[封装成函数]
 45         this.drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),0);
 46     }
 47     public void drawTank(int x,int y,Graphics g,int direct,int type){
 48         //1.设置颜色,画出左边的矩形
 49         switch(type){
 50             case 0:
 51                 g.setColor(Color.cyan);
 52                 break;
 53             case 1:
 54                 g.setColor(Color.yellow);
 55                 break;
 56         }
 57         switch(direct){
 58             case 0:
 59                 g.fill3DRect(x,y, 5, 30,false);
 60                 //2.画出右边的矩形
 61                 g.fill3DRect(x+15,y, 5, 30,false);
 62                 //3.画出中间的矩形
 63                 g.fill3DRect(x+5,y+5, 10, 20,false);
 64                 //4.画出中间的圆型
 65                 g.fillOval(x+5,y+10, 10, 10);
 66                 //5.画炮管
 67                 g.drawLine(x+10,y,x+10,y+15);
 68                 break;
 69             case 1:
 70                 /*
 71                  * 炮筒向右
 72                  */
 73                 //画上面的矩形
 74                 g.fill3DRect(x,y, 30,5,false);
 75                 //2.画出右边的矩形
 76                 g.fill3DRect(x,y+15, 30, 5,false);
 77                 //3.画出中间的矩形
 78                 g.fill3DRect(x+5,y+5, 20, 10,false);
 79                 //4.画出中间的圆型
 80                 g.fillOval(x+10,y+5, 10, 10);
 81                 //5.画炮管
 82                 g.drawLine(x+15,y+10,x+30,y+10);
 83                 break;
 84             case 2:
 85                 g.fill3DRect(x,y, 5, 30,false);
 86                 //2.画出右边的矩形
 87                 g.fill3DRect(x+15,y, 5, 30,false);
 88                 //3.画出中间的矩形
 89                 g.fill3DRect(x+5,y+5, 10, 20,false);
 90                 //4.画出中间的圆型
 91                 g.fillOval(x+5,y+10, 10, 10);
 92                 //5.画炮管
 93                 g.drawLine(x+10,y+15,x+10,y+30);
 94                 break;
 95             case 3:
 96                 /*
 97                  * 炮筒向左边
 98                  */
 99                 //画上面的矩形
100                 g.fill3DRect(x,y, 30,5,false);
101                 //2.画出右边的矩形
102                 g.fill3DRect(x,y+15, 30, 5,false);
103                 //3.画出中间的矩形
104                 g.fill3DRect(x+5,y+5, 20, 10,false);
105                 //4.画出中间的圆型
106                 g.fillOval(x+10,y+5, 10, 10);
107                 //5.画炮管
108                 g.drawLine(x+15,y+10,x,y+10);
109                 break;
110                 
111         }
112     }
113     public void keyTyped(KeyEvent e) {
114     }
115     //对键按下处理,用wdsa控制
116     //b)实现对事件的处理方法
117     public void keyPressed(KeyEvent e) {
118         //按下w键,向上
119         if(e.getKeyCode()==KeyEvent.VK_W){
120             this.hero.setDirect(0);//设置坦克方向
121             this.hero.moveUp();//向上移动
122         }else if(e.getKeyCode()==KeyEvent.VK_D){
123             //d键,向右
124             this.hero.setDirect(1);
125             this.hero.moveRight();
126         }else if(e.getKeyCode()==KeyEvent.VK_S){
127             //s键,向下
128             this.hero.setDirect(2);
129             this.hero.moveDown();
130         }else if(e.getKeyCode()==KeyEvent.VK_A){
131             //a键,向左
132             this.hero.setDirect(3);
133             this.hero.moveLeft();
134         }
135         this.repaint(); //重绘坦克。当重回被调用的时候界面才能被刷新。
136     }
137     public void keyReleased(KeyEvent e) {
138     }
139 }
140 //画坦克,分析:坦克生活在哪个区域(MyPanel中)

members类

 1 package com.test5;
 2 //坦克类
 3 class Tank{
 4     //坦克的横坐标
 5     int x = 0;
 6     int y = 0;
 7     //坦克方向 0上,1右,2下,3左
 8     int direct = 0;
 9     //坦克速度
10     int speed = 1;
11     public int getSpeed() {
12         return speed;
13     }
14     public void setSpeed(int speed) {
15         this.speed = speed;
16     }
17     public int getDirect() {
18         return direct;
19     }
20     public void setDirect(int direct) {
21         this.direct = direct;
22     }
23     public int getX() {
24         return x;
25     }
26     public void setX(int x) {
27         this.x = x;
28     }
29     public int getY() {
30         return y;
31     }
32     public void setY(int y) {
33         this.y = y;
34     }
35     
36     public Tank(int x,int y){
37         this.x = x;
38         this.y = y;
39     }
40 }
41 //我的坦克
42 class Hero extends Tank{
43     public Hero(int x,int y){
44         super(x,y);
45     }
46     //坦克向上移动
47     public void moveUp(){
48         y-=speed;
49     }
50     //坦克向右移动
51     public void moveRight(){
52         x+=speed;
53     }
54     //坦克向下移动
55     public void moveDown(){
56         y+=speed;
57     }
58     //坦克向左移动
59     public void moveLeft(){
60         x-=speed;
61     }
62 }

 

如果感觉不错,请 一个!
by simpman
原文地址:https://www.cnblogs.com/simpman/p/3568491.html