100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 华容道java代码解释_华容道代码解释

华容道java代码解释_华容道代码解释

时间:2024-06-21 01:01:35

相关推荐

华容道java代码解释_华容道代码解释

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

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。