import java
import java
import javax
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
{
super(c);
this
this
addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
setBackground(Color
}
public void focusLost(FocusEvent e)
{
}
}
class Hua_Rong_Road
{
Person person[]=new Person[
Button left
Button restart=new Button(
public Hua_Rong_Road
{
init();
setBounds(
setResizable(false);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System
}
}
);
}
public void init()
{
setLayout(null);
add(restart);
restart
restart
for(int k=
{
person[k]=new Person(k
person[k]
person[k]
add(person[k]);
}
person[
person[
person[
person[
person[
person[
person[
person[
person[
person[
person[
left=new Button();
right=new Button();
above=new Button();
below=new Button();
add(left);
add(right);
add(above);
add(below);
left
right
above
below
validate();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
Person man=(Person)e
if(e
{
go(man
}
if(e
{
go(man
}
if(e
{
go(man
}
if(e
{
go(man
}
}
public void mousePressed(MouseEvent e)
{
Person man=(Person)e
int x=
x=e
y=e
int w=man
int h=man
if(y>h/
{
go(man
}
if(y<h/
{
go(man
}
if(x<w/
{
go(man
}
if(x>w/
{
go(man
}
}
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
{
boolean move=true;
Rectangle manRect=man
int x=man
int y=man
if(direction==below)
y=y+
else if(direction==above)
y=y
else if(direction==left)
x=x
else if(direction==right)
x=x+
manRect
Rectangle directionRect=direction
for(int k=
{
Rectangle personRect=person[k]
if((manRect
{
move=false;
}
}
if(manRect
{
move=false;
}
if(move==true)
{
man
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25603.html