cancerl 2016-03-21 02:41 采纳率: 100%
浏览 3057
已采纳

求大神各位给一个华容道类的代码

不会解华容道这类的算法 如果可以 还请有一些注释 谢大神

  • 写回答

4条回答

  • lx624909677 2016-03-21 05:38
    关注
     import java.awt.*;
    import java.awt.event.*;
    public class MoveExample
    {
     public static void main(String args[])
     {
      new Hua_Rong_Road();
     }
    }
     class Person extends Button implements FocusListener
    {
     int number;
     Color c=new Color(255,245,170);
     public Person(final int number,final String s)
     {
      super(s);
      setBackground(c);
      this.number=number;
      c=getBackground();
      addFocusListener(this);
     }
     public void focusGained(final FocusEvent e)
     {
      setBackground(Color.red);
     }
     public void focusLost(final FocusEvent e)
     {
      setBackground(c);
     }
    }
     class Hua_Rong_Road extends Frame implements  MouseListener,KeyListener,ActionListener
     {
      Person person[]=new Person[10];
      Button left,right,above,below;
      Button restart=new Button("重新开始");
      public Hua_Rong_Road()
      {
       init();
       setBounds(100,100,320,360);
       setVisible(true);
       validate();
       addWindowListener(new WindowAdapter()
       {public void windowClosing(WindowEvent e)
       {
        System.out.println(0);
       }});
      }
      public void init()
      {
       setLayout(null);
       add(restart);
       restart.setBounds(100,320,120,25);
       restart.addActionListener(this);
       String name[]={"曹操","关羽","张飞","刘备","赵云","黄忠","兵","兵","兵","兵"};
       for(int k=0;k<name.length;k++)
       {
        person[k]=new Person(k,name[k]);
        person[k].addMouseListener(this);
        person[k].addKeyListener(this);
        add(person[k]);
       }
       person[0].setBounds(104,54,100,100);
       person[1].setBounds(104,154,100,50);
       person[2].setBounds(54,154,50,100);
       person[3].setBounds(204,154,50,100);
       person[4].setBounds(54,54,50,100);
       person[5].setBounds(204,54,50,100);
       person[6].setBounds(54,254,50,50);
       person[7].setBounds(204,254,50,50);
       person[8].setBounds(104,204,50,50);
       person[9].setBounds(154,204,50,50);
       person[9].requestFocus();
       left=new Button();right=new Button();
       above=new Button();below=new Button();
       add(left);add(right);add(above);add(below);
       left.setBounds(49,49,5,260);
       right.setBounds(254,49,5,260);
       above.setBounds(49,49,210,5);
       below.setBounds(49,304,210,5);
       validate();
      }
      public void keyTyped(KeyEvent e){}
      public void KeyReleased(KeyEvent e){}
      public void KeyPressed(KeyEvent e)
      {
       Person man=(Person)e.getSource();
       if(e.getKeyCode()==KeyEvent.VK_DOWN)
       {
        go(man,below);
       }
       if(e.getKeyCode()==KeyEvent.VK_UP)
       {
        go(man,above);
       }
       if(e.getKeyCode()==KeyEvent.VK_LEFT)
       {
        go(man,left);
       }
       if(e.getKeyCode()==KeyEvent.VK_RIGHT)
       {
        go(man,right);
       }
      }
      public void mousePressed(MouseEvent e)
      {
       Person man=(Person)e.getSource();
       int x=-1,y=-1;
       x=e.getX();
       y=e.getY();
       int w=man.getBounds().width;
       int h=man.getBounds().height;
       if(y>h/2)
       {
        go(man,below);
       }
       if(y<h/2)
       {
        go(man,above);
       }
       if(x<w/2)
       {
        go(man,left);
       }
       if(x>w/2)
       {
        go(man,right);
       }
      }
      public void mouseReleased(MouseEvent e){}
      public void mouseEntered(MouseEvent e){}
      public void mouseExited(MouseEvent e){}
      public void mouseClicked(MouseEvent e){}
      public void go(Person man,Button direction)
      {
       boolean move=true;
       Rectangle manRect=man.getBounds();
       int x=man.getBounds().x;
       int y=man.getBounds().y;
       if(direction==below)
        y=y+50;
       else if(direction==above)
        y=y-50;
       else if(direction==left)
        x=x-50;
       else if(direction==right)
        x=x+50;
       manRect.setLocation(x,y);
       Rectangle directionRect=direction.getBounds();
       for(int k=0;k<10;k++)
       {
        Rectangle personRect=person[k].getBounds();
        if((manRect.intersects(personRect))&&(man.number!=k))
        {
         move=false;
        }
       }
       if(manRect.intersects(directionRect))
       {
        move=false;
       }
       if(move==true)
       {
        man.setLocation(x,y);
       }
      }
      public void actionPerformed(ActionEvent e)
      {
       dispose();
       new Hua_Rong_Road();
      }
      public void keyPressed(KeyEvent arg0) { 
      }
      public void keyReleased(KeyEvent arg0) {
    
      }
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名