华容道

一共有三个类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class HuaRongDao extends JFrame implements KeyListener, MouseListener, ActionListener,FocusListener
{
    private Person[]  person = new Person[10];
    private JButton left,right,above,below;
    private JButton restart = new JButton("重新开始");
    private Color c = new Color(255,245,170);
    private Person focusPerson = null;
    private int sum = 0;
    public HuaRongDao()
    {
        init();
        c = person[0].getBackground();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100,100,320,500);
        setVisible(true);
        validate();
    }
    public void init()
    {
        setLayout(null);
        add(restart);
        //addMouseListener(this);
        addMouseListener(this);
        restart.setBounds(100,320,120,35);
        restart.addActionListener(this);
        String name[] = {"曹操","关羽","张","刘","周","黄","兵","兵","兵","兵"};
        for(int k=0;kfx+fw)
        {
            System.out.println("右");
            go(focusPerson,right);
        }


        /*
        int w = man.getBounds().width;
        int h = man.getBounds().height;
        if(y>h/2)
            go(man,below);
        if(yw/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,JButton 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 i=0;i<10;i++)
        {
            Rectangle personRect = person[i].getBounds();
            //如果移动之后的形状和其他人的形状发生重叠 &&
            if((manRect.intersects(personRect))&&(man.number!=i))
            {
                move = false;
            }
        }
        if(manRect.intersects(directionRect))
        {
            move = false;
        }
        if(move == true)
        {
            sum++;
            man.setLocation(x,y);
            System.out.println("新位置:x = "+x+" y="+y);
            if(y==204 && man == person[0])
            {
                JOptionPane.showMessageDialog(null, "游戏成功完成,共计"+sum+"步", "成功", JOptionPane.PLAIN_MESSAGE);
                restart.setText("成功!点击继续");
            }
        }
    }
    public void actionPerformed(ActionEvent e)
    {
        dispose();//销毁一个原来的
        new HuaRongDao();
    }
    public void focusGained(FocusEvent e)
    {
        Person person = (Person)e.getSource();
        person.setBackground(Color.red);
        focusPerson = person;
    }
    public void focusLost(FocusEvent e)
    {
        Person person = (Person)e.getSource();
        person.setBackground(c);
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Person extends JButton //implements  FocusListener
{
    int number;//编号
    Color c = new Color(255,245,170);
    Font font = new Font("宋体",Font.BOLD,12);
    Person(int number,String s)
    {
        super(s);
        setBackground(c);
        setFont(font);
        this.number = number;
        c = getBackground();
    }
}
public class MainClass
{
    public static void main(String[] args)
    {
        new HuaRongDao();
    }
}

 

你可能感兴趣的:(Java学习)