⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huarongdao.java

📁 华容道的简单程序,自己做的,比较简单,适合新手
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HuaRongDao {

	public static void main(String[] args) {
		new Hua_Rong_Dao();
	}
}

class Hua_Rong_Dao extends JFrame implements MouseListener, KeyListener,
		ActionListener {
	Person person[] = new Person[10];

	JButton left, right, above, below;

	JButton restart = new JButton("重新开始");

	public Hua_Rong_Dao() {
		init();
		setBounds(450, 300, 300, 350);
		setVisible(true);
		validate();
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		}

		);
	}

	public void init() {
		setLayout(null);
		add(restart);
		restart.setBounds(100, 10, 120, 25);
		restart.addActionListener(this);
		String name[] = { "曹操", "关羽", "张", "刘", "马", "许", "兵", "兵", "兵", "兵" };
		for (int i = 0; i < name.length; i++) {
			person[i] = new Person(i, name[i]);
			person[i].addMouseListener(this);
			person[i].addKeyListener(this);
			add(person[i]);
		}
		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();
		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)

	{
		Person man = (Person) e.getSource();
		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) {
		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 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.num != i)) {
				move = false;
			}
		}
		if (manRect.intersects(directionRect)) {
			move = false;
		}
		if (move == true) {
			man.setLocation(x, y);
		}
	}

	public void actionPerformed(ActionEvent e) {
		dispose();
		new Hua_Rong_Dao();
	}

	public void mouseExited(MouseEvent e) {

	}
}

class Person extends JButton implements FocusListener {
	int num;

	Person(int num, String s) {
		super(s);
		this.num = num;
		addFocusListener(this);
	}

	public void focusGained(FocusEvent e) {
		setBackground(Color.blue);
	}

	public void focusLost(FocusEvent e) {
		setBackground(null);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -