首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

java韩顺平老师的坦克程序,无法实现坦克的运动( 多谢高手)

2011-12-18 
java韩顺平老师的坦克程序,无法实现坦克的运动(在线等 谢谢高手)/**控制坦克的泡铜方向**/import javax.sw

java韩顺平老师的坦克程序,无法实现坦克的运动(在线等 谢谢高手)

/*
 *控制坦克的泡铜方向
 *
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class MyTankGame3 extends JFrame {

  Mypanel mp = null;

public static void main(String[] args) {
MyTankGame3 mtg = new MyTankGame3();

}

// 构造函数
public MyTankGame3() {
mp = new Mypanel();
this.add(mp);
// 注册监听
this.addKeyListener(mp);
this.setSize(400, 300);
  //this.setLocation(50,50);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setVisible(true);
}
}

// 我的面板
class Mypanel extends JPanel implements KeyListener {
// 定义一个我的坦克
Hero hero = null;

// 定义一个敌人的坦克组
Vector<EnemyTank> ets = new Vector<EnemyTank>();
int enSize = 3;

// 构造函数
public Mypanel() {

  hero = new Hero(150, 150);
   
// 初始化敌人的坦克
for (int i = 1; i <= enSize; i++) 
  {
// 创建一辆敌人的坦克
  EnemyTank et = new EnemyTank((i + 1) * 50, 15);
et.setColor(0); 
et.setDirect(2);
// 加入到敌人坦克组中
ets.add(et);
}
}

public void paint(Graphics g) {

super.paint(g);
  //设置背景颜色,400,300为窗体的长宽
g.fillRect(0, 0, 400, 300);
// 调用drawTank()传递系列参数
this.drawTank(hero.getX(), hero.getY(), g, hero.direct, 1);
// 画出敌人的坦克 很多辆 不确定
for (int i = 0; i <= ets.size(); i++) {
this.drawTank(ets.get(i).getX() , ets.get(i).getY(), g, ets.get(i)
.getDirect(), 0);

   
}

public void drawTank(int x, int y, Graphics g, int direct, int type) {
switch (type) {
case 0:
g.setColor(Color.CYAN);
break;
case 1:
g.setColor(Color.yellow);
break;
}
switch (direct) {
case 0:
// 画出我的坦克 向上
// 1.画出左边的矩形
g.fill3DRect(x - 10, y - 15, 5, 30, false);
// 2.画出右边的矩形
g.fill3DRect(x + 5, y - 15, 5, 30, false);
// 3.画出中间矩形
g.fill3DRect(x - 5, y - 10, 10, 20, false);
// 4.画出圆形
g.fillOval(x - 5, y - 5, 10, 10);
// 5.画出线
g.drawLine(x, y, x, y - 15);
break;
// 向右
case 1:
// 1.画出上边的矩形
g.fill3DRect(x - 15, y - 10, 30, 5, false);
// 2.画出下边的矩形
g.fill3DRect(x - 15, y + 5, 30, 5, false);
// 3.画出中间矩形
g.fill3DRect(x - 10, y - 5, 20, 10, false);
// 4.画出圆形
g.fillOval(x - 5, y - 5, 10, 10);
// 5.画出线
g.drawLine(x, y, x + 15, y);
break;
// 向下
case 2:
// 1.画出左边的矩形
g.fill3DRect(x - 10, y - 15, 5, 30, false);
// 2.画出右边的矩形
g.fill3DRect(x + 5, y - 15, 5, 30, false);
// 3.画出中间矩形
g.fill3DRect(x - 5, y - 10, 10, 20, false);
// 4.画出圆形
g.fillOval(x - 5, y - 5, 10, 10);
// 5.画出线
g.drawLine(x, y, x, y + 15);
break;
// 向左
case 3:
// 1.画出上边的矩形
g.fill3DRect(x - 15, y - 10, 30, 5, false);
// 2.画出下边的矩形
g.fill3DRect(x - 15, y + 5, 30, 5, false);
// 3.画出中间矩形
g.fill3DRect(x - 10, y - 5, 20, 10, false);
// 4.画出圆形
g.fillOval(x - 5, y - 5, 10, 10);
// 5.画出线
g.drawLine(x, y, x - 15, y);
break;

}
}

// 键按下处理,a表示向左,s表示向下,d表示向右,w表示向上
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_W) {
// 设置我的坦克的方向
this.hero.setDirect(0);
if (this.hero.y > 15) {
this.hero.moveUp();
}
} else if (arg0.getKeyCode() == KeyEvent.VK_D) {


// 向右
this.hero.setDirect(1);
if (this.hero.x < 400 - 15) {
this.hero.moveRight();
}
} else if (arg0.getKeyCode() == KeyEvent.VK_S) {
this.hero.setDirect(2);
if (this.hero.y < 300 - 15) {
this.hero.moveDown();
}
} else if (arg0.getKeyCode() == KeyEvent.VK_A) {
this.hero.setDirect(3);
if (this.hero.x > 15) {
this.hero.moveLeft();
}
}

this.repaint();
}

public void keyReleased(KeyEvent arg0) {
}

public void keyTyped(KeyEvent arg0) {
}

}




class Tank 
{
int x=0;
int y=0;
//坦克方向 0 1 2 3 上右下左
  int direct=0;
  int speed=1;
  int color;


   
public Tank(int x,int y)
{
this.x =x;
this.y=y;
}


public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public int getSpeed()
{return speed;}
public void setSpeed(int speed)
{this.speed= speed;}


public int getColor() {
return color;
}


public void setColor(int color) {
this.color = color;
}

}
//敌人的坦克类
class EnemyTank extends Tank {

public EnemyTank(int x,int y)
{
super(x,y);
}



}


//我的坦克类
class Hero extends Tank
{
public Hero(int x,int y)
{super(x,y);}

//坦克向上移动
public void moveUp()
{
this.y-=this.speed;
}
//坦克向右移动
public void moveRight()
{
x+=speed;
}
//坦克向下移动
public void moveDown()
{
y+=speed;
}
//坦克向左移动
public void moveLeft()
{
x-=speed;
}
}


[解决办法]
其实你代码里面就是 写错了一行导致无法运行
69行左右 for (int i = 0; i <= ets.size(); i++) { 这里i<=导致了数组越界的问题,改成
 for (int i = 0; i <ets.size(); i++) {就可以了
下面给贴出全部代码,同时添加了方向键操作坦克的能力

Java code
/* *控制坦克的炮筒方向 * */import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class MyTankGame3 extends JFrame {    Mypanel mp = null;    public static void main(String[] args) {        MyTankGame3 mtg = new MyTankGame3();    }    // 构造函数    public MyTankGame3() {        mp = new Mypanel();        this.add(mp);// 注册监听        this.addKeyListener(mp);        this.setSize(400, 300);        //this.setLocation(50,50);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setVisible(true);    }}// 我的面板class Mypanel extends JPanel implements KeyListener {    // 定义一个我的坦克    Hero hero = null;    // 定义一个敌人的坦克组    Vector<EnemyTank> ets = new Vector<EnemyTank>();    int enSize = 3;    // 构造函数    public Mypanel() {        hero = new Hero(150, 150);// 初始化敌人的坦克        for (int i = 1; i <= enSize; i++)        {// 创建一辆敌人的坦克            EnemyTank et = new EnemyTank((i + 1) * 50, 15);            et.setColor(0);            et.setDirect(2);// 加入到敌人坦克组中            ets.add(et);        }    }    @Override    public void paint(Graphics g) {        super.paint(g);        //设置背景颜色,400,300为窗体的长宽        g.fillRect(0, 0, 400, 300);        // 调用drawTank()传递系列参数        this.drawTank(hero.getX(), hero.getY(), g, hero.direct, 1);        // 画出敌人的坦克 很多辆 不确定//        for (int i = 0; i <= ets.size(); i++) {        for (int i = 0; i <ets.size(); i++) {    //这一行修改            this.drawTank(ets.get(i).getX() , ets.get(i).getY(), g, ets.get(i)                    .getDirect(), 0);        }    }    public void drawTank(int x, int y, Graphics g, int direct, int type) {        switch (type) {            case 0:                g.setColor(Color.CYAN);                break;            case 1:                g.setColor(Color.yellow);                break;        }        switch (direct) {            case 0:// 画出我的坦克 向上// 1.画出左边的矩形                g.fill3DRect(x - 10, y - 15, 5, 30, false);// 2.画出右边的矩形                g.fill3DRect(x + 5, y - 15, 5, 30, false);// 3.画出中间矩形                g.fill3DRect(x - 5, y - 10, 10, 20, false);// 4.画出圆形                g.fillOval(x - 5, y - 5, 10, 10);// 5.画出线                g.drawLine(x, y, x, y - 15);                break;// 向右            case 1:// 1.画出上边的矩形                g.fill3DRect(x - 15, y - 10, 30, 5, false);// 2.画出下边的矩形                g.fill3DRect(x - 15, y + 5, 30, 5, false);// 3.画出中间矩形                g.fill3DRect(x - 10, y - 5, 20, 10, false);// 4.画出圆形                g.fillOval(x - 5, y - 5, 10, 10);// 5.画出线                g.drawLine(x, y, x + 15, y);                break;// 向下            case 2:// 1.画出左边的矩形                g.fill3DRect(x - 10, y - 15, 5, 30, false);// 2.画出右边的矩形                g.fill3DRect(x + 5, y - 15, 5, 30, false);// 3.画出中间矩形                g.fill3DRect(x - 5, y - 10, 10, 20, false);// 4.画出圆形                g.fillOval(x - 5, y - 5, 10, 10);// 5.画出线                g.drawLine(x, y, x, y + 15);                break;// 向左            case 3:// 1.画出上边的矩形                g.fill3DRect(x - 15, y - 10, 30, 5, false);// 2.画出下边的矩形                g.fill3DRect(x - 15, y + 5, 30, 5, false);// 3.画出中间矩形                g.fill3DRect(x - 10, y - 5, 20, 10, false);// 4.画出圆形                g.fillOval(x - 5, y - 5, 10, 10);// 5.画出线                g.drawLine(x, y, x - 15, y);                break;        }    }    // 键按下处理,a表示向左,s表示向下,d表示向右,w表示向上    public void keyPressed(KeyEvent arg0) {// TODO Auto-generated method stub         System.out.println("用户操作");        if (arg0.getKeyCode() == KeyEvent.VK_W) {//            System.out.println("坦克向上行走");            // 设置我的坦克的方向            this.hero.setDirect(0);            if (this.hero.y > 15) {                this.hero.moveUp();            }        } else if (arg0.getKeyCode() == KeyEvent.VK_D||                arg0.getKeyCode() ==KeyEvent.VK_UP) {            // 向右//            System.out.println("坦克向右行走");            this.hero.setDirect(1);            if (this.hero.x < 400 - 15) {                this.hero.moveRight();            }        } else if (arg0.getKeyCode() == KeyEvent.VK_S||                arg0.getKeyCode() ==KeyEvent.VK_DOWN) {//            System.out.println("坦克向下行走");            this.hero.setDirect(2);            if (this.hero.y < 300 - 15) {                this.hero.moveDown();            }        } else if (arg0.getKeyCode() == KeyEvent.VK_A||                arg0.getKeyCode()==KeyEvent.VK_LEFT) {            System.out.println("坦克向左行走");            this.hero.setDirect(3);            if (this.hero.x > 15) {                this.hero.moveLeft();            }        }        this.repaint();    }    public void keyReleased(KeyEvent arg0) {    }    public void keyTyped(KeyEvent arg0) {    }}class Tank{    int x=0;    int y=0;    //坦克方向 0 1 2 3 上右下左    int direct=0;    int speed=1;    int color;    public Tank(int x,int y)    {        this.x =x;        this.y=y;    }    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }    public int getDirect() {        return direct;    }    public void setDirect(int direct) {        this.direct = direct;    }    public int getSpeed()    {return speed;}    public void setSpeed(int speed)    {this.speed= speed;}    public int getColor() {        return color;    }    public void setColor(int color) {        this.color = color;    }}//敌人的坦克类class EnemyTank extends Tank {    public EnemyTank(int x,int y)    {        super(x,y);    }}//我的坦克类class Hero extends Tank{    public Hero(int x,int y)    {super(x,y);}    //坦克向上移动    public void moveUp()    {        this.y-=this.speed;    }    //坦克向右移动    public void moveRight()    {        x+=speed;    }    //坦克向下移动    public void moveDown()    {        y+=speed;    }    //坦克向左移动    public void moveLeft()    {        x-=speed;    }} 


[解决办法]
上面的上下左右键 代码有点问题,重新贴

Java code
/* *控制坦克的炮筒方向 * */import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class MyTankGame3 extends JFrame {    Mypanel mp = null;    public static void main(String[] args) {        MyTankGame3 mtg = new MyTankGame3();    }    // 构造函数    public MyTankGame3() {        mp = new Mypanel();        this.add(mp);// 注册监听        this.addKeyListener(mp);        this.setSize(400, 300);        //this.setLocation(50,50);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setVisible(true);    }}// 我的面板class Mypanel extends JPanel implements KeyListener {    // 定义一个我的坦克    Hero hero = null;    // 定义一个敌人的坦克组    Vector<EnemyTank> ets = new Vector<EnemyTank>();    int enSize = 3;    // 构造函数    public Mypanel() {        hero = new Hero(150, 150);// 初始化敌人的坦克        for (int i = 1; i <= enSize; i++)        {// 创建一辆敌人的坦克            EnemyTank et = new EnemyTank((i + 1) * 50, 15);            et.setColor(0);            et.setDirect(2);// 加入到敌人坦克组中            ets.add(et);        }    }    @Override    public void paint(Graphics g) {        super.paint(g);        //设置背景颜色,400,300为窗体的长宽        g.fillRect(0, 0, 400, 300);        // 调用drawTank()传递系列参数        this.drawTank(hero.getX(), hero.getY(), g, hero.direct, 1);        // 画出敌人的坦克 很多辆 不确定//        for (int i = 0; i <= ets.size(); i++) {        for (int i = 0; i <ets.size(); i++) {    //这一行修改            this.drawTank(ets.get(i).getX() , ets.get(i).getY(), g, ets.get(i)                    .getDirect(), 0);        }    }    public void drawTank(int x, int y, Graphics g, int direct, int type) {        switch (type) {            case 0:                g.setColor(Color.CYAN);                break;            case 1:                g.setColor(Color.yellow);                break;        }        switch (direct) {            case 0:// 画出我的坦克 向上// 1.画出左边的矩形                g.fill3DRect(x - 10, y - 15, 5, 30, false);// 2.画出右边的矩形                g.fill3DRect(x + 5, y - 15, 5, 30, false);// 3.画出中间矩形                g.fill3DRect(x - 5, y - 10, 10, 20, false);// 4.画出圆形                g.fillOval(x - 5, y - 5, 10, 10);// 5.画出线                g.drawLine(x, y, x, y - 15);                break;// 向右            case 1:// 1.画出上边的矩形                g.fill3DRect(x - 15, y - 10, 30, 5, false);// 2.画出下边的矩形                g.fill3DRect(x - 15, y + 5, 30, 5, false);// 3.画出中间矩形                g.fill3DRect(x - 10, y - 5, 20, 10, false);// 4.画出圆形                g.fillOval(x - 5, y - 5, 10, 10);// 5.画出线                g.drawLine(x, y, x + 15, y);                break;// 向下            case 2:// 1.画出左边的矩形                g.fill3DRect(x - 10, y - 15, 5, 30, false);// 2.画出右边的矩形                g.fill3DRect(x + 5, y - 15, 5, 30, false);// 3.画出中间矩形                g.fill3DRect(x - 5, y - 10, 10, 20, false);// 4.画出圆形                g.fillOval(x - 5, y - 5, 10, 10);// 5.画出线                g.drawLine(x, y, x, y + 15);                break;// 向左            case 3:// 1.画出上边的矩形                g.fill3DRect(x - 15, y - 10, 30, 5, false);// 2.画出下边的矩形                g.fill3DRect(x - 15, y + 5, 30, 5, false);// 3.画出中间矩形                g.fill3DRect(x - 10, y - 5, 20, 10, false);// 4.画出圆形                g.fillOval(x - 5, y - 5, 10, 10);// 5.画出线                g.drawLine(x, y, x - 15, y);                break;        }    }    // 键按下处理,a表示向左,s表示向下,d表示向右,w表示向上    public void keyPressed(KeyEvent arg0) {// TODO Auto-generated method stub         System.out.println("用户操作");        if (arg0.getKeyCode() == KeyEvent.VK_W                ||arg0.getKeyCode() ==KeyEvent.VK_UP) {//            System.out.println("坦克向上行走");            // 设置我的坦克的方向            this.hero.setDirect(0);            if (this.hero.y > 15) {                this.hero.moveUp();            }        } else if (arg0.getKeyCode() == KeyEvent.VK_D||                arg0.getKeyCode()==KeyEvent.VK_RIGHT) {            // 向右//            System.out.println("坦克向右行走");            this.hero.setDirect(1);            if (this.hero.x < 400 - 15) {                this.hero.moveRight();            }        } else if (arg0.getKeyCode() == KeyEvent.VK_S||                arg0.getKeyCode() ==KeyEvent.VK_DOWN) {//            System.out.println("坦克向下行走");            this.hero.setDirect(2);            if (this.hero.y < 300 - 15) {                this.hero.moveDown();            }        } else if (arg0.getKeyCode() == KeyEvent.VK_A||                arg0.getKeyCode()==KeyEvent.VK_LEFT) {            System.out.println("坦克向左行走");            this.hero.setDirect(3);            if (this.hero.x > 15) {                this.hero.moveLeft();            }        }        this.repaint();    }    public void keyReleased(KeyEvent arg0) {    }    public void keyTyped(KeyEvent arg0) {    }}class Tank{    int x=0;    int y=0;    //坦克方向 0 1 2 3 上右下左    int direct=0;    int speed=1;    int color;    public Tank(int x,int y)    {        this.x =x;        this.y=y;    }    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }    public int getDirect() {        return direct;    }    public void setDirect(int direct) {        this.direct = direct;    }    public int getSpeed()    {return speed;}    public void setSpeed(int speed)    {this.speed= speed;}    public int getColor() {        return color;    }    public void setColor(int color) {        this.color = color;    }}//敌人的坦克类class EnemyTank extends Tank {    public EnemyTank(int x,int y)    {        super(x,y);    }}//我的坦克类class Hero extends Tank{    public Hero(int x,int y)    {super(x,y);}    //坦克向上移动    public void moveUp()    {        this.y-=this.speed;    }    //坦克向右移动    public void moveRight()    {        x+=speed;    }    //坦克向下移动    public void moveDown()    {        y+=speed;    }    //坦克向左移动    public void moveLeft()    {        x-=speed;    }} 

热点排行