华容道代码解释

package huarongdao;

import java.awt.*;  //包含用于创建用户界面和绘制图形和图像的所有类
import javax.swing.*;  //提供了一组丰富的库来用平台独立的方式创建图形用户界面
import java.awt.event.*;  //提供处理由 AWT 组件所激发的各类事件的接口和类

public class Hua_Rong_Road extends JFrame implements MouseListener, KeyListener, ActionListener {  //继承JFrame类和Mouse、Key、Action监视器的接口
    Person person[] = new Person[10];    //创建10个Person类对象
    JButton left, right, above, below;   //创建4个按钮以设置边框
    JButton restart = new JButton("重新开始");

    public Hua_Rong_Road() {
        init();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);   //设置程序关闭时的行为
        setBounds(100, 100, 320, 500);    //设置程序的位置和大小
        setVisible(true);     //设置为可视窗口
        validate();     //确保组件具有有效的布局
    }

    public void init() {
        setLayout(null);   //设置布局为空布局
        add(restart);     //将restart按钮增加到容器
        restart.setBounds(100, 320, 120, 35);
        restart.addActionListener(this);
        String name[] = {"曹操", "关羽", "张", "刘", "周", "黄", "兵", "兵", "兵", "兵"};
        for (int k = 0; k < name.length; k++) {
            person[k] = new Person(k, name[k]);
            person[k].addMouseListener(this);   //为每个对象添加鼠标和键盘监视器
            person[k].addKeyListener(this);
            add(person[k]);
        }
        person[0].setBounds(104, 54, 100, 100);   //设置每个棋子的位置和大小
        person[1].setBounds(104, 154, 100, 50);
        person[2].setBounds(54, 154, 50, 100);
        person[3].setBounds(204, 154, 50, 100);
        person[4].setBounds(54, 54, 50, 100);
        person[5].setBounds(204, 54, 50, 100);
        person[6].setBounds(54, 254, 50, 50);
        person[7].setBounds(204, 254, 50, 50);
        person[8].setBounds(104, 204, 50, 50);
        person[9].setBounds(154, 204, 50, 50);
        person[9].requestFocus();   //person[9]获取焦点
        left = new JButton();
        right = new JButton();
        above = new JButton();
        below = new JButton();
        add(left);
        add(right);
        add(above);
        add(below);
        left.setBounds(49, 49, 5, 260);
        right.setBounds(254, 49, 5, 260);
        above.setBounds(49, 49, 210, 5);
        below.setBounds(49, 304, 210, 5);
        validate();
    }

    public void keyTyped(KeyEvent e) {   //重写所有抽象类的方法
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {   //当用户按下键盘的方向键时将调用相应的go函数
        Person man = (Person) e.getSource(); //getSource()函数返回的是事件源的Object对象,故用强制转换
        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) {  //当用户点击按钮的时候会进行判断并调用满足条件的go函数。
        Person man = (Person) e.getSource();
        int x = -1, y = -1;
        x = e.getX();   //获取事件源的坐标及大小
        y = e.getY();
        int w = man.getBounds().width;
        int h = man.getBounds().height;
        if (y > h / 2)
            go(man, below);
        if (y < h / 2)
            go(man, above);
        if (x < w / 2)
            go(man, left);
        if (x > w / 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)  //判断direction的值并对坐标进行调整
            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 k = 0; k < 10; k++) {     //循环判断是否有发生碰撞
            Rectangle personRect = person[k].getBounds();
            if ((manRect.intersects(personRect)) && (man.number != k))   //如果被选中按钮与其他妻子发生碰撞,则move被设置为false
                move = false;
        }
        if (manRect.intersects(directionRect))    //如果被选中按钮与边框发生碰撞,则move被设置为false
            move = false;
        if (move == true)    //如果move为true,则将被选中窗口设置为新坐标
            man.setLocation(x, y);
    }

    public void actionPerformed(ActionEvent e) {
        dispose();    //当按钮组件被选中时将原来的窗口释放并创建新的窗口
        new Hua_Rong_Road();
    }
}
package huarongdao;

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

public class Person extends JButton implements FocusListener {  //继承JButton类和Focus监视器接口
    int number;
    Color c = new Color(255, 245, 170);
    Font font = new Font("宋体", Font.BOLD, 12);

    Person(int number, String s) {
        super(s);   //调用JButton的构造函数并将内容设置为s
        setBackground(c);
        setFont(font);
        this.number = number;
        c = getBackground();
        addFocusListener(this);   //为对象本身调用一个焦点监视器
    }

    public void focusGained(FocusEvent e) {
        setBackground(Color.red);
    }   //当按钮被选中时背景颜色变红

    public void focusLost(FocusEvent e) {
        setBackground(c);
    }   //当按钮失去焦点时背景颜色变回c
}

猜你喜欢

转载自blog.csdn.net/rumple49/article/details/84334090