java小程序死机_求解,刚写的小程序,一运行我机器就死机
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
this.speed = speed;
}
//創(chuàng)建坦克向4個方向運動的方法
public void goUp()
{
this.setDirect(0);
y -= speed;
}
public void goDown()
{
this.setDirect(1);
y += speed;
}
public void goLeft()
{
this.setDirect(2);
x -= speed;
}
public void goRight()
{
this.setDirect(3);
x += speed;
}
}
//開始定義詳細的坦克類
class MyTank extends Tank
{
//坦克開火時創(chuàng)建一個子彈,并啟動線程
Shot shot = null;
//構造函數(shù),創(chuàng)建一個新對象
public MyTank(int x ,int y ,int color,int direct)
{
super(x,y,color,direct);
}
//開火的方法
public void fire()
{
//根據(jù)坦克的方向確定在哪個點畫出子彈
switch(this.getDirect())
{
case 0:
shot = new Shot(this.getX()-1, this.getY()-23,0);
break;
case 1:
shot = new Shot(this.getX()-1, this.getY()+20,1);
break;
case 2:
shot = new Shot(this.getX()-23, this.getY()-1,2);
break;
case 3:
shot = new Shot(this.getX()+20, this.getY()-1,3);
break;
} //畫出子彈后,啟動子彈的線程
Thread t1 = new Thread(this.shot);
t1.start();
}
} //定義敵人類
class EnemyTank extends Tank
{
public EnemyTank(int x ,int y ,int color,int direct)
{
super(x,y,color,direct);
}
}
//開始定義JPanel類,開始畫坦克
class MyJPanel extends JPanel
{
MyTank mt1 = null;
int size = 3;
Vector aa = new Vector();
//在構造方法里直接添加一個我的坦克對象,那么new panel時也吧new 坦克的任務完成了
public MyJPanel(MyTank mt1)
{
this.mt1=mt1;
//畫敵人的坦克
for(int i=0;i
{
EnemyTank tmp = new EnemyTank((i+1)*50,50,0,1);
aa.add(tmp);
}
}
//重寫paint方法,在里面初始化
public void paint(Graphics g)
{
//先初始化畫筆,這句話必須寫的
super.paint(g);
//開始畫畫了,先畫出一塊黑的屏幕出來,就是畫出一個屏幕那么大的黑塊
g.setColor(Color.BLACK);
g.fillRect(0, 0, 500, 500);
this.drawTank(mt1.getX(), mt1.getY(), g, mt1.getColor(), mt1.getDirect());
this.drawShot(g);
for(int i =0;i
{
this.drawTank(aa.get(i).getX(), aa.get(i).getY(), g,aa.get(i).getColor(),aa.get(i).getDirect());
}
}
//把話坦克的內容封裝成一個方法,在這直接添加
//開始寫畫坦克的程序了
public void drawTank(int x,int y,Graphics g ,int color,int direct)
{
//選擇顏色
switch(color)
{
case 0:
g.setColor(Color.BLUE);
break;
case 1:
g.setColor(Color.YELLOW);
break;
}
//0上 1下 2左 3右
switch(direct)
{
case 0:
g.fill3DRect(x-15, y-15, 5, 30,false);
g.fill3DRect(x-10,y-10,20,20,false);
g.fill3DRect(x+10, y-15, 5, 30, false);
g.fillOval(x-5, y-5, 10, 10);
g.drawLine(x,y,x,y-20);
break;
case 1:
g.fill3DRect(x-15, y-15, 5, 30,false);
g.fill3DRect(x-10,y-10,20,20,false);
g.fill3DRect(x+10, y-15, 5, 30, false);
g.fillOval(x-5, y-5, 10, 10);
g.drawLine(x,y,x,y+20);
break;
case 2:
g.fill3DRect(x-15, y-15, 30, 5,false);
g.fill3DRect(x-10,y-10,20,20,false);
g.fill3DRect(x-15, y+10, 30, 5, false);
g.fillOval(x-5, y-5, 10, 10);
g.drawLine(x,y,x-20,y);
break;
case 3:
g.fill3DRect(x-15, y-15, 30, 5,false);
g.fill3DRect(x-10,y-10,20,20,false);
g.fill3DRect(x-15, y+10, 30, 5, false);
g.fillOval(x-5, y-5, 10, 10);
g.drawLine(x,y,x+20,y);
break;
}
}
//畫子彈
public void drawShot(Graphics g)
{
g.fill3DRect(mt1.shot.getX(), mt1.shot.getY(), 3, 3, false);
}
} //設計監(jiān)聽類
class MyListener implements KeyListener
{ private MyTank mt = null;
private MyJPanel mp =null;
public MyListener(MyTank mt,MyJPanel mp)
{
this.mt=mt;
this.mp=mp;
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP)
{
mt.goUp();
}else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
mt.goDown();
}else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
mt.goLeft();
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
mt.goRight();
}
if(e.getKeyCode()==KeyEvent.VK_J)
{
mt.fire();
}
mp.repaint();
} @Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
} @Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
總結
以上是生活随笔為你收集整理的java小程序死机_求解,刚写的小程序,一运行我机器就死机的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java环境教程_window下Java
- 下一篇: java修饰符继承_Java修饰符和继承