1. 程式人生 > >華容道程式碼解釋

華容道程式碼解釋

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
}