java

位置:IT落伍者 >> java >> 浏览文章

Java写的华容道


发布日期:2019年09月20日
 
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();

}

}

               

上一篇:Java通用权限控制算法

下一篇:Java swing组件的串行化和读取