之前写的这个,不稳定http://blog.csdn.net/zwyjg/article/details/8122639
原因是子弹到了边界和碰到别的圆时没有删除自己的线程导致的
修改后的程序,可在一局结束后自动再次初始化所有圆。还更改了圆的颜色,视觉效果比较好。。。。。
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; public class Hufan extends JFrame{ static HuPanel hp; public static void main(String[] args) { // TODO Auto-generated method stub new Hufan(); } public Hufan(){ hp=new HuPanel(); this.add(hp); hp.addMouseListener(hp); new Thread(hp).start(); this.setTitle("疯狂女医生"); this.setSize(290, 400); this.setLocation(500, 300); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } class HuPanel extends JPanel implements MouseListener,Runnable{ //使用Vector更安全 public static Vector<Balloon> al=new Vector<Balloon>();//存放圆 public static Vector<Bomb> alBomb=new Vector<Bomb>();//存放子弹 Balloon ball; Bomb bomb; Random r=new Random(); int times;//点击次数 //初始化所有圆 public void init(){ times=0; //初始化每个圆,并且随机大小傎 for(int i=0;i<7;i++){ for(int j=0;j<7;j++){ ball=new Balloon(i, j, r.nextInt(5)); al.add(ball); } } } public HuPanel(){ init();//启动时初始化一次所有圆 } public void paint(Graphics g){ super.paint(g); g.setColor(Color.black); g.fillRect(0, 0, 400, 400); g.setColor(Color.white); g.fillRect(0, 0, 280, 280); //画格子 for(int i=0;i<8;i++){ g.setColor(Color.red); g.drawLine(0, i*40, 280, i*40); g.drawLine(i*40, 0, i*40, 280); } //画球 //g.setColor(Color.blue); for(int i=0;i<al.size();i++){ Balloon b=al.get(i); if (b != null) { switch (b.life) { case 0: g.setColor(Color.blue); g.fillOval(b.x * 40 + 15, b.y * 40 + 10, 10, 15); break; case 1: g.setColor(Color.black); g.fillOval(b.x * 40 + 10, b.y * 40 + 10, 19, 14); break; case 2: g.setColor(Color.cyan); g.fillOval(b.x * 40 + 10, b.y * 40 + 10, 17, 22); break; case 3: g.setColor(Color.gray); g.fillOval(b.x * 40 + 8, b.y * 40 + 10, 25, 20); break; case 4: g.setColor(Color.green); g.fillOval(b.x * 40 + 5, b.y * 40 + 5, 30, 30); break; } } } //画炸弹 g.setColor(Color.red); for(int i=0;i<alBomb.size();i++){ Bomb bomb=alBomb.get(i); if(bomb!=null){ g.fillOval(bomb.x, bomb.y, 10, 10); } } //画点击次数 g.setColor(Color.white); g.setFont(new Font(null,1,25)); g.drawString("你点击次数为: "+times, 40, 330); } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub int x=e.getX()/40; int y=e.getY()/40; if(alBomb.size()==0){//子弹集合为0时,就是没有子弹了 if(al.contains(new Balloon(x,y))){//如果集合里存在此对象 times++; for(int i=0;i<al.size();i++){ Balloon b=al.get(i); if(b.x==x && b.y==y){ b.life++; if(b.life==5){ al.remove(b); for(int j=0;j<4;j++){ bomb=new Bomb(b.x*40+15, b.y*40+15, j); alBomb.add(bomb); new Thread(bomb).start(); } } break; } } }else{ ball=new Balloon(x, y, 0); al.add(ball); } } } public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mousePressed(MouseEvent arg0) {} public void mouseReleased(MouseEvent arg0) {} @Override public void run() { // TODO Auto-generated method stub while(true){ try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.repaint(); } } } class Balloon{//圆类 int x; int y; int life;//圆的生命历程,到5时就会爆 public Balloon(int x, int y) { this.x = x; this.y = y; } public Balloon(int x, int y, int life) { this.x = x; this.y = y; this.life = life; } public boolean equals(Object obj) { Balloon u = (Balloon) obj; if(x==u.x && y==u.y){ return true; }else{ return false; } } } class Bomb implements Runnable{//子弹类 int x; int y; int speed=5; int direct; Bomb bomb; boolean isStop=true; public Bomb(int x, int y, int direct) { this.x = x; this.y = y; this.direct = direct; } @Override //public synchronized void run() //安全锁 public void run() { // TODO Auto-generated method stub while(true){ switch(direct){ case 0: y-=speed; break; case 1: x-=speed; break; case 2: y+=speed; break; case 3: x+=speed; break; } if(x<0||x>280||y<0||y>280){ isStop=false; //到达边界,就退出线程 HuPanel.alBomb.remove(this); } if(HuPanel.al.size()==0&&HuPanel.alBomb.size()==0){ Hufan.hp.init(); } //synchronized保证在同一时刻最多只有一个线程执行该段代码 synchronized(this) { for(int i=0;i<HuPanel.al.size();i++){ Balloon b=HuPanel.al.get(i); //子弹碰到集合里的球时 if(x>b.x*40+10&&x<b.x*40+20&&y>b.y*40+10&&y<b.y*40+20){ HuPanel.alBomb.remove(this);//从子弹集合里删除此子弹 b.life++;//圆的生命值加1 if(b.life==5){//如果圆的生命值为5时,就要爆,并再生成四个子弹 for(int j=0;j<4;j++){ bomb=new Bomb(b.x*40+15, b.y*40+15, j); HuPanel.alBomb.add(bomb); new Thread(bomb).start(); } HuPanel.al.remove(b);//圆集合里删除b } isStop=false; //碰到其他圆时,退出线程 break; } } } if(isStop==false){//退出线程 break; } try { Thread.sleep(30); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }