Example类

public class Example9_25 {

public static void main(String[] args) {

new Hua_Rong_Road();

}

}

Hua_Rond_Road类

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Hua_Rong_Road extends JFrame implements MouseListener,KeyListener,ActionListener{

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

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();

}

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

go(man,above);

if(x

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 mouseClick(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 k=0;k<10;k++){

Rectangle personRect=person[k].getBounds();

if((manRect.intersects(personRect))&&(man.number!=k))

move=false;

}

if(manRect.intersects(directionRect))

move=false;

if(move==true)

man.setLocation(x,y);

}

public void actionPersonformed(ActionEvent e){

dispose();

new Hua_Rong_Road();

}

@Override

public void mouseClicked(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

}

}

Person类

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

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);

}

public void focusGained(FocusEvent e){

setBackground(Color.red);

}

public void focusLost(FocusEvent e){

setBackground(c);

}

}

华容道 java_Java-华容道相关推荐

  1. 华容道 java_华容道 ( java)

    java老师布置这华容道的题,大概有两个多礼拜了,上机的时候,一班的同学做出来了,心里感觉很...  所以清明回家,就研究了研究.因为以前上过c++的选修课,所以对类的什么有那么一点点了解,对java ...

  2. python第六周项目华容道_华容道游戏(中)

    此为<算法的乐趣>读书笔记,我用javascript(ES6)重新实现算法. 华容道游戏看似简单,但求解需要设计的数据结构比较复杂,还牵涉到棋类游戏的棋局判断,所以整个过程还是挺费劲的.我 ...

  3. 看最强大脑的数字华容道,尝试理解与总结

    目录 前言 华容道起源 简介 发展 华容道解法 横刀立马解(最少步数) 数字华容道 界面 解法前言 定理 定理解释 针对定理的证明 正解解法细分3种(非官方定义,都是按照自己摸索和理解定义的) 第一种 ...

  4. java华容道代码_Java 华容道完整源码

    时间:2018-12-19 概述:华容道 Java 华容道完整源码,本代码实现华容道游戏的整体功能.程序执行后,点击相应的人物,然后按上下左右键可以移动.点击重新开始按钮,可以将各个人物的位置重置.如 ...

  5. 15数字华容道解法 图解_数字华容道攻略(数字华容道最快解法图)

    数字华容道有何解法? 4*4基本在一分钟左右,最快一次32秒.5*5.6*6还没测.4*4太简单,我就简单说一下了,后面5*5和6*6比较详细.4*4: 首先还原前两行.然后把9到12按下图...下面 ...

  6. golang 实现华容道

    游戏介绍 华容道游戏共有10个棋子, 在5*4的棋盘上. 其中曹操占2*2格, 张飞,关羽, 赵云, 黄忠, 马超 占2*1格, 这些棋子有横向和竖向的区别. 4个卒各占1格. 目标: 将曹操这枚棋子 ...

  7. 《智能设备艺术、科技、文化作品实例开发与设计》android开发系列介绍---2.1棋类作品:华容道

    第四章 棋类:  华容道 三子棋AI算法 手机棋牌开发的优点和缺点 与其他手机端游戏相比较,手机棋牌开发演化的手游竞技,与休闲性合二为一,游艺兼备.手游玩家随着公共WIFI的普及,参与门槛越来越低.加 ...

  8. C语言easyx制作华容道游戏,huarongdao 用EasyX编写的华容道程序 - 下载 - 搜珍网

    huarongdao/Debug/main.obj huarongdao/Debug/vc120.idb huarongdao/Debug/vc120.pdb huarongdao/Debug/华容道 ...

  9. 使用VB求解“华容道”问题

    由于年代久远,此文本地已经无存,网上临时找到一篇备份,录在下面. 收录的网站本身已经没有图片了,真是遗憾,以后有空再补充下. 概述:本文描述了利用广度优先算法求解"华容道"问题的基 ...

  10. IQ使命 Luxor 埃及卢克索(华容道) 攻略

    IQ使命 目录: IQ使命 Rapa Nui 复活岛(智力大逃亡)攻略 IQ使命 London 伦敦(一笔画)攻略 IQ使命 Luxor 埃及卢克索(华容道) 攻略 IQ使命 Antwerp 安特卫普 ...

最新文章

  1. Spring Boot 配置加载顺序详解
  2. 解决 TortoiseGit 诡异的 Bad file number 问题(转)
  3. java匿名对象赋初值_不想进BAT的Java程序员不是好程序员,BAT后端Java岗面试真题分享
  4. 计算机二级c语言考点分析,计算机二级C语言考点分析.doc
  5. 安卓逆向_11 --- methodprofiling(方法分析)【在 smali 代码中打印信息 --- 协议分析常用】
  6. iBATIS date MySQL_LocalDateTime与mysql日期类型的交互(基于mybatis)
  7. php闭包原理,闭包原理及实例
  8. 欢迎访问我的Github
  9. C# Task 循环任务_聊聊 JavaScript 的并发、异步和事件循环
  10. 开源可视化网页抓取工具Portia 爬虫
  11. vant swipe点击切换
  12. 在Unity2018如何使用代码一键设置Icon
  13. java实现beamsearch_Beam Search(集束搜索/束搜索)
  14. 行车路线(CCF201712-4)
  15. 02_JavaScript数据结构与算法(二)数组
  16. 如何快速搞一个导播系统?
  17. mysql 1205错误
  18. 选修课:唐宋词鉴赏课堂笔记05
  19. 2022DASCTF MAY 出题人挑战赛个人Writeup
  20. php怎么设置文字环绕图片,css文字环绕图片的方法

热门文章

  1. Problem:跳房子
  2. php+怎么加边框,ps给图片加上漂亮的边框-PS教程
  3. 流式计算利器-Storm
  4. 【基金学习】小白基金学习记录-从入门到实践(二)
  5. BPI-M1P(全志A20)刷Android启动卡之后启动的过程
  6. ffmpeg+nginx+dash调用摄像头直播
  7. 一道十分经典的intern面试题(String字符串)让你彻底搞懂intern方法
  8. ae-概述、流程、菜单01
  9. linux系统四个组成部分,Linux系统由哪几部分组成?系统详解(干货)
  10. mysql配置kodi16.1_kodi中文包|kodi 16.1中文语言包下载 附插件使用教程 - 121下载站...