今天复习了一下监听的内容,然后自己尝试写了写代码,总算勉强能把坦克大战的初步版代码写出来了,估计再过几天就能编出这款小游戏了,继续努力。下面是今天的总结:
1.事件编程的步骤:
a.编写事件处理类(事件监听者);
b.根据需求给事件处理类实现监听器接口;
c.在事件处理类中重写(实现)事件处理的函数;
d.在事件源类中指定该事件的监听器(响应者)是谁,即注册监听。
2.总结注意事项:
a.java采用“委托”机制处理事件;
b.java中的事件是分类的,比如对窗体事件、鼠标事件、按键事件、操作事件【按按钮】;
c.java中的一个类要监听某个事件,则必须实现相对应的事件监听接口;
d.事件监听接口有多种,程序员应当根据不同的情况实现不同的监听接口,比如监听鼠标事件,就应当实现MouseListener;
e.在实现监听接口的类(事件监听者/类)中,需要重写函数,比如实现了ActionListener接口,就应当重写actionPerformed(ActionEvent e);
f.在事件源中需要注册事件监听类,否则事件监听类接收不到事件源发生的事件。
线程
1.进程是指运行中的应用程序。
2.线程:
a.线程是轻量级的进程;
b.线程没有独立的地址空间(内存空间);
c.线程是由进程创建的(寄生在进程中);
d.一个进程可以拥有多个线程(多线程编程);
e.线程有几种状态:1.新建状态(new);2.就绪状态(Runnable);3.运行状态(Running);4.阻塞状态(Blocked);5.死亡状态(Dead)。
最后写一段代码,实现坦克的前后左右移动(通过W、A、S、D四个按键控制),如下图所示:




import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyTankGame extends JFrame
{
MyPanel mp;
public MyTankGame()
{
mp=new MyPanel();
this.add(mp);
this.setSize(400,300);
this.addKeyListener(mp); //注册监听
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String args[])
{
MyTankGame mtg=new MyTankGame();
}
}
class Tank
{
int x;
int y;
int direct;
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
public void setX(int x)
{
this.x=x;
}
public int getX()
{
return x;
}
public void setY(int y)
{
this.y=y;
}
public int getY()
{
return y;
}
}
class Hero extends Tank
{
public Hero(int x,int y)
{
super(x,y);
}
}
class MyPanel extends JPanel implements KeyListener
{
Hero hero;
public MyPanel()
{
hero=new Hero(200,200); //定义我的坦克的初始位置
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0,0,400,300); //设置背景颜色
this.drawTank(0,0,g,this.hero.direct);
}
public void drawTank(int x,int y,Graphics g,int direct)
{
g.setColor(Color.CYAN); //设置画笔颜色
//direct表示坦克方向,其中:0表示向上,1表示向左,2表示向下,3表示向右
if(this.hero.direct==0)
{
g.fill3DRect(hero.getX()-15,hero.getY()-15,5,30,false);
g.fill3DRect(hero.getX()-10,hero.getY()-10,20,20,false);
g.fill3DRect(hero.getX()+10,hero.getY()-15,5,30,false);
g.fillOval(hero.getX()-5,hero.getY()-5,10,10);
g.drawLine(hero.getX(),hero.getY(),hero.getX(),hero.getY()-15);
}
else if(this.hero.direct==1)
{
g.fill3DRect(hero.getX()-15,hero.getY()-15,30,5,false);
g.fill3DRect(hero.getX()-15,hero.getY()+10,30,5,false);
g.fill3DRect(hero.getX()-10,hero.getY()-10,20,20,false);
g.fillOval(hero.getX()-5,hero.getY()-5,10,10);
g.drawLine(hero.getX(),hero.getY(),hero.getX()-15,hero.getY());
}
else if(hero.direct==2)
{
g.fill3DRect(hero.getX()-15,hero.getY()-15,5,30,false);
g.fill3DRect(hero.getX()-10,hero.getY()-10,20,20,false);
g.fill3DRect(hero.getX()+10,hero.getY()-15,5,30,false);
g.fillOval(hero.getX()-5,hero.getY()-5,10,10);
g.drawLine(hero.getX(),hero.getY(),hero.getX(),hero.getY()+15);
}
else if(hero.direct==3)
{
g.fill3DRect(hero.getX()-15,hero.getY()-15,30,5,false);
g.fill3DRect(hero.getX()-15,hero.getY()+10,30,5,false);
g.fill3DRect(hero.getX()-10,hero.getY()-10,20,20,false);
g.fillOval(hero.getX()-5,hero.getY()-5,10,10);
g.drawLine(hero.getX(),hero.getY(),hero.getX()+15,hero.getY());
}
}
public void KeyTyped(KeyEvent e)
{}
public void KeyReleased(KeyEvent e)
{}
public void KeyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.hero.direct=0;
this.hero.y--;
}
else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.hero.direct=1;
this.hero.x--;
}
else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.hero.direct=2;
this.hero.y++;
}
else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.hero.direct=3;
this.hero.x++;
}
this.repaint(); //最后一定要记得调用repaint函数,否则坦克无法动弹
}
}
大功告成,虽然在MyEclipse写过一遍了,重新再打一遍发现问题还是有好多,对照着源代码检查了一遍,重新放入运行发现还是一大片错误,所以说还是要多手写一些代码,在MyEclipse上快捷键按多了会让人依赖的,又快两点了,碎觉碎觉zzZ
网友评论