能帮我看看这个子弹的坐标哪里错了吗
我的这个坦克哪里错了啊运行坦克可以正常移动,但是子弹的坐标老是错误,子弹也飞不出去,到底是哪里错了啊,检查很久了
package unfinished;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TryTank
{
MyPanel my;
JFrame jf;
public static void main(String[] args)
{
TryTank tr = new TryTank();
}
public TryTank()
{
jf = new JFrame("我的坦克大战");
my = new MyPanel();
jf.addKeyListener(my);
Thread t2 = new Thread(my);
t2.start();
my.setBackground(Color.black);
jf.add(my);
jf.setVisible(true);
jf.setSize(400,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements KeyListener,Runnable
{
Mine t;
public MyPanel()
{
t = new Mine(10,10);
}
public void paint(Graphics g)
{
super.paint(g);
t.setType(2);
t.setSpeed(4);
this.paintTank(t.getX(), t.getY(), g, t.getDirect(), t.getType());
if(t.b != null&&t.b.isAlive == true)
{
t.b.setSpeed(2);
g.fillOval(t.b.getX(), t.b.getY(), 2, 2);
}
}
//画坦克
public void paintTank(int x, int y, Graphics g, int direct, int type)
{
//判断颜色,1是敌人,黄色。2是自己,红色
if(type == 1)
{
g.setColor(Color.YELLOW);
switch(direct)
{
case 1:
g.fill3DRect(x, y,5,30,false);
g.fill3DRect(x+5,y+5,10,20,false);
g.fill3DRect(x+15,y,5,30,false);
g.fillOval(x+4,y+10,10,10);
g.drawLine(x+9,y+15,x+9,y-4);
break;
case 2:
g.fill3DRect(x, y,30,5,false);
g.fill3DRect(x+5,y+5,20,10,false);
g.fill3DRect(x,y+15,30,5,false);
g.fillOval(x+10,y+5,10,10);
g.drawLine(x+15,y+10,x+34,y+10);
break;
case 3:
g.fill3DRect(x, y,5,30,false);
g.fill3DRect(x+5,y+5,10,20,false);
g.fill3DRect(x+15,y,5,30,false);
g.fillOval(x+4,y+10,10,10);
g.drawLine(x+9,y+15,x+9,y+34);
break;
case 4:
g.fill3DRect(x, y,30,5,false);
g.fill3DRect(x+5,y+5,20,10,false);
g.fill3DRect(x,y+15,30,5,false);
g.fillOval(x+10,y+5,10,10);
g.drawLine(x+15,y+10,x-4,y+10);
}
}
else if(type == 2)
{
g.setColor(Color.RED);
switch(direct)
{
case 1:
g.fill3DRect(x, y,5,30,false);
g.fill3DRect(x+5,y+5,10,20,false);
g.fill3DRect(x+15,y,5,30,false);
g.fillOval(x+4,y+10,10,10);
g.drawLine(x+9,y+15,x+9,y-4);
break;
case 2:
g.fill3DRect(x, y,30,5,false);
g.fill3DRect(x+5,y+5,20,10,false);
g.fill3DRect(x,y+15,30,5,false);
g.fillOval(x+10,y+5,10,10);
g.drawLine(x+15,y+10,x+34,y+10);
break;
case 3:
g.fill3DRect(x, y,5,30,false);
g.fill3DRect(x+5,y+5,10,20,false);
g.fill3DRect(x+15,y,5,30,false);
g.fillOval(x+4,y+10,10,10);
g.drawLine(x+9,y+15,x+9,y+34);
break;
case 4:
g.fill3DRect(x, y,30,5,false);
g.fill3DRect(x+5,y+5,20,10,false);
g.fill3DRect(x,y+15,30,5,false);
g.fillOval(x+10,y+5,10,10);
g.drawLine(x+15,y+10,x-4,y+10);
}
}
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
//设置WASD为移动
if(e.getKeyCode() == KeyEvent.VK_W)
{
t.setDirect(1);
t.moveUp();
}
else if(e.getKeyCode() == KeyEvent.VK_D)
{
t.setDirect(2);
t.moveRight();
}
else if(e.getKeyCode() == KeyEvent.VK_S)
{
t.setDirect(3);
t.moveDown();
}
else if(e.getKeyCode() == KeyEvent.VK_A)
{
t.setDirect(4);
t.moveLeft();
}
if(e.getKeyCode() == KeyEvent.VK_J)
{
t.fired();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void run()
{
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
this.repaint();
}
class Tank
{
int x = 10, y = 10;
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;
}
}
class Mine extends Tank
{
int type = 0,direct = 1,speed =1;
Bullet b;
public Mine(int x, int y)
{
super(x,y);
}
public int getType()
{
return type;
}
public void setType(int type)
{
this.type = type;
}
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 void moveUp()
{
y-=this.getSpeed();
}
public void moveRight()
{
x+=this.getSpeed();
}
public void moveDown()
{
y+=this.getSpeed();
}
public void moveLeft()
{
x-=this.getSpeed();
}
//开火
public void fired()
{
switch(this.direct)
{
case 1:
b = new Bullet(x+10,y,1);
case 2:
b = new Bullet(x+30,y+10,2);
case 3:
b = new Bullet(x+10,y+30,3);
case 4:
b = new Bullet(x,y+10,4);
}
Thread t1 = new Thread(b);
t1.start();
}
}
class Bullet implements Runnable
{
int x = 10, y = 10,speed = 1,direct = 1;
boolean isAlive = true;
public Bullet(int x, int y,int direct)
{
this.x = x;
this.y = y;
this.direct = direct;
}
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 getSpeed()
{
return speed;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public int getDirect()
{
return direct;
}
public void setDirect(int direct)
{
this.direct = direct;
}
public void run()
{
while(true)
{
try
{
Thread.sleep(50);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(this.direct)
{
case 1:
y-=this.getSpeed();
break;
case 2:
x+=this.getSpeed();
break;
case 3:
y+=this.getSpeed();
break;
case 4:
x-=this.getSpeed();
break;
}
System.out.println("["+x+","+y+"]");
// if(x<0||x>400||y<0||y>300);
// {
// isAlive = false;
// break;
// }
}
}
}
}