熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java寫的華容道

2022-06-13   來源: Java核心技術 

  import javaawt*;

  import javaawtevent*;

  import javaxswing*;

  public class Hua_Rong_Road {

  /**

  * @param args

  */

  public static void main(String[] args) {

  // TODO 自動生成方法存根

  new Hua_Rong_Road();

  }

  }

  class Person extends JButton implements FocusListener

  {

  int number;

  ImageIcon c;

  Person(int numberImageIcon c)

  {

  super(c);

  thisc=c;

  thisnumber=number;

  addFocusListener(this);

  }

  public void focusGained(FocusEvent e)

  {

  setBackground(Colorred);

  }

  public void focusLost(FocusEvent e)

  {

  }

  }

  class Hua_Rong_Road extends Frame implements MouseListenerKeyListenerActionListener

  {

  Person person[]=new Person[];

  Button leftrightabovebelow;

  Button restart=new Button(重新開始);

  public Hua_Rong_Road()

  {

  init();

  setBounds();

  setResizable(false);

  setVisible(true);

  validate();

  addWindowListener(new WindowAdapter()

  {

  public void windowClosing(WindowEvent e)

  {

  Systemexit();

  }

  }

  );

  }

  public void init()

  {

  setLayout(null);

  add(restart);

  restartsetBounds();

  restartaddActionListener(this);

  for(int k=;k<;k++)

  {

  person[k]=new Person(knew ImageIcon());

  person[k]addMouseListener(this);

  person[k]addKeyListener(this);

  add(person[k]);

  }

  person[]setBounds();

  person[]setBounds();

  person[]setBounds();

  person[]setBounds();

  person[]setBounds();

  person[]setBounds();

  person[]setBounds();

  person[]setBounds();

  person[]setBounds();

  person[]setBounds();

  person[]requestFocus();

  left=new Button();

  right=new Button();

  above=new Button();

  below=new Button();

  add(left);

  add(right);

  add(above);

  add(below);

  leftsetBounds();

  rightsetBounds();

  abovesetBounds();

  belowsetBounds();

  validate();

  }

  public void keyTyped(KeyEvent e){}

  public void keyReleased(KeyEvent e){}

  public void keyPressed(KeyEvent e)

  {

  Person man=(Person)egetSource();

  if(egetKeyCode()==KeyEventVK_DOWN)

  {

  go(manbelow);

  }

  if(egetKeyCode()==KeyEventVK_UP)

  {

  go(manabove);

  }

  if(egetKeyCode()==KeyEventVK_LEFT)

  {

  go(manleft);

  }

  if(egetKeyCode()==KeyEventVK_RIGHT)

  {

  go(manright);

  }

  }

  public void mousePressed(MouseEvent e)

  {

  Person man=(Person)egetSource();

  int x=y=;

  x=egetX();

  y=egetY();

  int w=mangetBounds()width;

  int h=mangetBounds()height;

  if(y>h/)

  {

  go(manbelow);

  }

  if(y<h/)

  {

  go(manabove);

  }

  if(x<w/)

  {

  go(manleft);

  }

  if(x>w/)

  {

  go(manright);

  }

  }

  public void mouseReleased(MouseEvent e){}

  public void mouseEntered(MouseEvent e){}

  public void mouseExited(MouseEvent e){}

  public void mouseClicked(MouseEvent e){}

  public void go(Person manButton direction)

  {

  boolean move=true;

  Rectangle manRect=mangetBounds();

  int x=mangetBounds()x;

  int y=mangetBounds()y;

  if(direction==below)

  y=y+;

  else if(direction==above)

  y=y;

  else if(direction==left)

  x=x;

  else if(direction==right)

  x=x+;

  manRectsetLocation(xy);

  Rectangle directionRect=directiongetBounds();

  for(int k=;k<;k++)

  {

  Rectangle personRect=person[k]getBounds();

  if((manRectintersects(personRect))&&(mannumber!=k))

  {

  move=false;

  }

  }

  if(manRectintersects(directionRect))

  {

  move=false;

  }

  if(move==true)

  {

  mansetLocation(xy);

  }

  }

  public void actionPerformed(ActionEvent e)

  {

  dispose();

  new Hua_Rong_Road();

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25603.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.