编了一个小程序,只有一个坦克,想控制坦克的移动却移动不了,不知道程序哪里出了有关问题,求大神解答
编了一个小程序,只有一个坦克,想控制坦克的移动却移动不了,不知道程序哪里出了问题,求大神解答
package tank;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyTankGame1 extends JFrame
{
MyPanel mp=null;
public static void main(String[] args)
{
MyTankGame1 mtg1=new MyTankGame1();
}
public MyTankGame1()//构造函数
{
mp=new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400,300);
this.setVisible(true);
}
}
class MyPanel extends JPanel implements KeyListener//我的面板
{
Hero hero=null;//定义一个我的坦克
public MyPanel()//构造函数
{
hero=new Hero(10,10);
}
public void paint(Graphics g)//重写paint方法
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(getX(), getY(), g, 0, 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://向上
g.fill3DRect(x, y, 5, 30,false);//画出左边的矩形
g.fill3DRect(x+15, y, 5, 30,false);//画出右边的矩形
g.fill3DRect(x+5, y+5, 10, 20,false);//画出中间的矩形
g.fillOval(x+5, y+10, 10, 10);//画出中间的圆
g.drawLine(x+10, y+15, x+10, y);//画出直线
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void keyPressed(KeyEvent e)
{
// TODO 自动生成的方法存根
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.hero.setDirect(0);//设置我的坦克的方向
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.hero.setDirect(1);//向右
this.hero.moveRight();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.hero.setDirect(2);//向下
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.hero.setDirect(3);//向左
this.hero.moveLeft();
}
this.repaint();//必须重新绘制Panel
}
@Override
public void keyReleased(KeyEvent e) {
// TODO 自动生成的方法存根
}
}
class Tank//坦克类
{
int x=0;//坦克的横坐标
int y=0;
int speed=1;//设置坦克的速度
int direct=0;//0表示上,1表示右,2表示下,3表示左
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
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;
}
}
class Hero extends Tank//我的坦克
{
public Hero(int x,int y)
{
super(x,y);
}
public void moveUp()//坦克向上移动的方法
{
y-=speed;
}
public void moveRight()
{
x+=speed;
}
public void moveDown()
{
y+=speed;
}
public void moveLeft()
{
x-=speed;
}
}
------解决思路----------------------
public void paint(Graphics g)//重写paint方法
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(getX(), getY(), g, 0, 0);
}
改成
public void paint(Graphics g)// 重写paint方法
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(hero.getX(), hero.getY(), g, 0, 0);
}
就可以了。。
建议学习一下debug断点调试,很容易就能找到错误的点了。
------解决思路----------------------
注意 this.drawTank的入参,还有drawTank方法里,switch (direct)处的case补全:
------解决思路----------------------
package tank;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyTankGame1 extends JFrame
{
MyPanel mp=null;
public static void main(String[] args)
{
MyTankGame1 mtg1=new MyTankGame1();
}
public MyTankGame1()//构造函数
{
mp=new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400,300);
this.setVisible(true);
}
}
class MyPanel extends JPanel implements KeyListener//我的面板
{
Hero hero=null;//定义一个我的坦克
public MyPanel()//构造函数
{
hero=new Hero(10,10);
}
public void paint(Graphics g)//重写paint方法
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(getX(), getY(), g, 0, 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://向上
g.fill3DRect(x, y, 5, 30,false);//画出左边的矩形
g.fill3DRect(x+15, y, 5, 30,false);//画出右边的矩形
g.fill3DRect(x+5, y+5, 10, 20,false);//画出中间的矩形
g.fillOval(x+5, y+10, 10, 10);//画出中间的圆
g.drawLine(x+10, y+15, x+10, y);//画出直线
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void keyPressed(KeyEvent e)
{
// TODO 自动生成的方法存根
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.hero.setDirect(0);//设置我的坦克的方向
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.hero.setDirect(1);//向右
this.hero.moveRight();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.hero.setDirect(2);//向下
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.hero.setDirect(3);//向左
this.hero.moveLeft();
}
this.repaint();//必须重新绘制Panel
}
@Override
public void keyReleased(KeyEvent e) {
// TODO 自动生成的方法存根
}
}
class Tank//坦克类
{
int x=0;//坦克的横坐标
int y=0;
int speed=1;//设置坦克的速度
int direct=0;//0表示上,1表示右,2表示下,3表示左
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
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;
}
}
class Hero extends Tank//我的坦克
{
public Hero(int x,int y)
{
super(x,y);
}
public void moveUp()//坦克向上移动的方法
{
y-=speed;
}
public void moveRight()
{
x+=speed;
}
public void moveDown()
{
y+=speed;
}
public void moveLeft()
{
x-=speed;
}
}
------解决思路----------------------
public void paint(Graphics g)//重写paint方法
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(getX(), getY(), g, 0, 0);
}
改成
public void paint(Graphics g)// 重写paint方法
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(hero.getX(), hero.getY(), g, 0, 0);
}
就可以了。。
建议学习一下debug断点调试,很容易就能找到错误的点了。
------解决思路----------------------
注意 this.drawTank的入参,还有drawTank方法里,switch (direct)处的case补全:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyTankGame1 extends JFrame {
MyPanel mp = null;
public static void main(String[] args) {
MyTankGame1 mtg1 = new MyTankGame1();
}
public MyTankGame1()// 构造函数
{
mp = new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400, 300);
this.setVisible(true);
}
}
class MyPanel extends JPanel implements KeyListener// 我的面板
{
Hero hero = null;// 定义一个我的坦克
public MyPanel()// 构造函数
{
hero = new Hero(10, 10);
}
public void paint(Graphics g)// 重写paint方法
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
// 这里要使用hero的参数
this.drawTank(hero.getX(), hero.getY(), g, hero.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:// 向上
g.fill3DRect(x, y, 5, 30, false);// 画出左边的矩形
g.fill3DRect(x + 15, y, 5, 30, false);// 画出右边的矩形
g.fill3DRect(x + 5, y + 5, 10, 20, false);// 画出中间的矩形
g.fillOval(x + 5, y + 10, 10, 10);// 画出中间的圆
g.drawLine(x + 10, y + 15, x + 10, y);// 画出直线
break;
case 1:// 向右
g.fill3DRect(x-5, y+5, 30, 5, false);// 画出上边的矩形
g.fill3DRect(x-5, y + 20, 30, 5, false);// 画出下边的矩形
g.fill3DRect(x , y+10 , 20, 10, false);// 画出中间的矩形
g.fillOval(x + 5, y + 10, 10, 10);// 画出中间的圆
g.drawLine(x + 10, y + 15, x + 25, y + 15);// 画出直线
break;
case 2:// 向下
g.fill3DRect(x, y, 5, 30, false);// 画出左边的矩形
g.fill3DRect(x + 15, y, 5, 30, false);// 画出右边的矩形
g.fill3DRect(x + 5, y + 5, 10, 20, false);// 画出中间的矩形
g.fillOval(x + 5, y + 10, 10, 10);// 画出中间的圆
g.drawLine(x + 10, y + 15, x + 10, y + 30);// 画出直线
break;
case 3:// 向左
g.fill3DRect(x-5, y+5, 30, 5, false);// 画出上边的矩形
g.fill3DRect(x-5, y + 20, 30, 5, false);// 画出下边的矩形
g.fill3DRect(x , y+10 , 20, 10, false);// 画出中间的矩形
g.fillOval(x + 5, y + 10, 10, 10);// 画出中间的圆
g.drawLine(x + 10, y + 15, x-5, y + 15);// 画出直线
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void keyPressed(KeyEvent e) {
// TODO 自动生成的方法存根
if (e.getKeyCode() == KeyEvent.VK_W) {
this.hero.setDirect(0);// 设置我的坦克的方向
this.hero.moveUp();
} else if (e.getKeyCode() == KeyEvent.VK_D) {
this.hero.setDirect(1);// 向右
this.hero.moveRight();
} else if (e.getKeyCode() == KeyEvent.VK_S) {
this.hero.setDirect(2);// 向下
this.hero.moveDown();
} else if (e.getKeyCode() == KeyEvent.VK_A) {
this.hero.setDirect(3);// 向左
this.hero.moveLeft();
}
super.repaint();// 必须重新绘制Panel
}
@Override
public void keyReleased(KeyEvent e) {
// TODO 自动生成的方法存根
}
}
class Tank// 坦克类
{
int x = 0;// 坦克的横坐标
int y = 0;
int speed = 1;// 设置坦克的速度
int direct = 0;// 0表示上,1表示右,2表示下,3表示左
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
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;
}
}
class Hero extends Tank// 我的坦克
{
public Hero(int x, int y) {
super(x, y);
}
public void moveUp()// 坦克向上移动的方法
{
y -= speed;
}
public void moveRight() {
x += speed;
}
public void moveDown() {
y += speed;
}
public void moveLeft() {
x -= speed;
}
}
------解决思路----------------------