package hhuarongdao;

 public class example {
public static void main(String args[])
{
new Hua_Rong_Road();
}
}
 package hhuarongdao;
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]; //person 为自定义的一个变量类型
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(); //使用 validate 方法会使容器再次布置其子组件。确保布局有效。
}
public void init()
{
setLayout(null);
add(restart);
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,54,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 mouseExited(MouseEvent e){}
public void go(Person man, JButton direction) {
// TODO Auto-generated method stub
boolean move=true;
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
if(direction==below) y+=50;
else if(direction==above)y-=50;
else if(direction==left)x-=50;
else if(direction==right)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);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub }
public void actionPerformed(ActionEvent e)
{
dispose(); //注销
new Hua_Rong_Road();
} }
 package hhuarongdao;
import javax.swing.*;
import java.awt.*;
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初学之华容道游戏的更多相关文章

  1. java初学的分析

    java初学的分析第一阶段:入门阶段学习目标:简单项目开发学习内容:1.Java入门书籍,Java基础知识.关于Java入门级的书,给大家推荐过<Java编程思想>.<Java核心技 ...

  2. 疯狂java实战演义 弹球游戏代码

    package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; ...

  3. Java -- 乒乓球 乒乓弹球游戏

    <疯狂Java讲义> 练习游戏 import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; impo ...

  4. 基于Java的开源3D游戏引擎jMonkeyEngine

    jMonkeyEngine简介 jMonkeyEngine是一款纯Java语言编写的游戏引擎,继承了Java应用跨平台的特性,而且是开放源代码的,遵循BSD开源协议,BSD开源协议用一句简单的话概括就 ...

  5. 【全网最优方法】JAVA初学:错误: 找不到或无法加载主类HelloWorld

    JAVA初学:错误: 找不到或无法加载主类 HelloWorld 我这是看的黑马2019网课(B站)出现的问题. 放一下别人的图,我也是大概的问题:就是javac没问题,java却无论怎么弄都报错. ...

  6. Java写的斗地主游戏源码

    源码下载在最后 我们的前年的课设要求做一个斗地主程序,当时正在愁如何做界面,当时刚好在学习C#,于是就用C#完成了这个程序.一方面,当时我C#功底还很差(其实现在也不怎么样),很多地方用了“笨办法”, ...

  7. java实现贪吃蛇游戏

    最简单的4个java类就可以实现贪吃蛇: main函数: package tcs; public class GreedSnake { public static void main(String[] ...

  8. Java自制人机小游戏——————————剪刀、石头、布

    package com.hello.test; import java.util.Scanner; public class TestGame { public static void main(St ...

  9. java猜数字小游戏

    /* * * 猜数字小游戏 * * 先由系统生成一个2-100之间的随机数字, * * 然后捕获用户从控制台中输入的数字是否与系统生成的随机数字相同, * * 如果相同则统计用户所猜的次数,并给出相应 ...

随机推荐

  1. HashCheck

    https://github.com/gurnec/HashCheck

  2. Populating Display Item Value On Query In Oracle Forms

    Write Post-Query trigger for the block you want to fetch the field value for display item.ExampleBeg ...

  3. [51NOD1024] 矩阵中不重复的元素(数学,精度)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1024 因为n和m都到100了,所以直接快速幂硬算一定会爆炸,考 ...

  4. jquery初涉,First Blood

    jquery可以帮助干的事情有: 遍历HTML文档 操作DOM 处理事件 执行动画 开发Ajax操作 优点就不在这儿扯蛋了~ 1.jquery环境配置 jquery不需要安装,只需要将下载的jquer ...

  5. Shell概述

    一,Shell原理

  6. Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence 二分

    E. Restoring Increasing Sequence time limit per test 1 second memory limit per test 256 megabytes in ...

  7. (一)linux常见命令

    一.chmod 修改文件权限 每一文件或目录的访问权限都有三组,每组用三位表示,分别为文件属主的读.写和执行权限:与属主同组的用户的读.写和执行权限:系统中其他用户的读.写和执行权限.横线代表空许可. ...

  8. Delphi 使用之函数

    函数由一句或多句代码组成,可以实现某个特定的功能.使用函数可以使代码更加易读.易懂,加快编程速度及减少重复代码.过程与函数类似,过程与函数最重要的区别在于,过程没有返回值,而函数能有返回值.     ...

  9. Objective-C与C++的区别

    1.两者的最大相同:都是从C演化而来的面相对象语言,两者都兼容标准C语言 2.两者的最大不同:Objective-C提供了运行期动态绑定机制,而C++是编译静态绑定,并且通过嵌入类(多重继承)和虚函数 ...

  10. dbca建库时找不到ASM磁盘

    现象 dbca创建数据库时,找不到ASM磁盘组:而grid用户使用asmca却又能看到ASM磁盘组. 解决方法 1. 检查设备的权限,正确的权限为grid:asmadmin 2. 检查GRID_HOM ...