Java 课本实验 华容道(简易版)

您所在的位置:网站首页 华容道电子版和木质版哪个好 Java 课本实验 华容道(简易版)

Java 课本实验 华容道(简易版)

2024-06-28 08:59:48| 来源: 网络整理| 查看: 265

实验要求:

华容道是大家很熟悉的一个传统智力游戏。编写GUI程序,用户通过键盘和鼠标事件来实现曹操,关羽等人物的移动。

代码:

MainClass.java

public class MainClass { public static void main(String[] args) { new Hua_Rong_Road(); } } Hua_Rong_Road.java

import java.awt.Color; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JFrame; // ,, public class Hua_Rong_Road extends JFrame implements ActionListener, MouseListener, KeyListener { Person person[] = new Person[10]; JButton left, right, above, below; 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.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(); 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(); } @Override 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 keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } 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 mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseReleased(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 dirctionRect = direction.getBounds(); for (int k = 0; k < 10; k++) { Rectangle personRect = person[k].getBounds(); if ((manRect.intersects(personRect)) && (man.number != k)) move = false; } if (manRect.intersects(dirctionRect)) move = false; if (move == true) man.setLocation(x, y); } public void actionPerformed(ActionEvent e) { dispose(); new Hua_Rong_Road(); } } Person.java

import java.awt.Color; import java.awt.Font; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.JButton; 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(); addFocusListener(this); } @Override public void focusGained(FocusEvent arg0) { // TODO Auto-generated method stub setBackground(Color.red); } @Override public void focusLost(FocusEvent arg0) { // TODO Auto-generated method stub setBackground(c); } } 运行结果:

                 



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭