知识大全 Java写的华容道
Posted 知
篇首语:知识的领域是无限的,我们的学习也是无限期的。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java写的华容道相关的知识,希望对你有一定的参考价值。
Java写的华容道 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
import java awt *;
import java awt event *;
import javax swing *;
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 number ImageIcon c)
super(c);
this c=c;
this number=number;
addFocusListener(this);
public void focusGained(FocusEvent e)
setBackground(Color red);
public void focusLost(FocusEvent e)
class Hua_Rong_Road extends Frame implements MouseListener KeyListener ActionListener
Person person[]=new Person[ ];
Button left right above below;
Button restart=new Button( 重新开始 );
public Hua_Rong_Road ()
init();
setBounds( );
setResizable(false);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System exit( );
);
public void init()
setLayout(null);
add(restart);
restart setBounds( );
restart addActionListener(this);
for(int k= ;k< ;k++)
person[k]=new Person(k new 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);
left setBounds( );
right setBounds( );
above setBounds( );
below setBounds( );
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= y= ;
x=e getX();
y=e getY();
int w=man getBounds() width;
int h=man getBounds() height;
if(y>h/ )
go(man below);
if(y<h/ )
go(man above);
if(x<w/ )
go(man left);
if(x>w/ )
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+ ;
else if(direction==above)
y=y ;
else if(direction==left)
x=x ;
else if(direction==right)
x=x+ ;
manRect setLocation(x y);
Rectangle directionRect=direction getBounds();
for(int k= ;k< ;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 ();
cha138/Article/program/Java/hx/201311/25603
相关参考