❶ 基于java的华容道游戏设计的论文

JAVA的华容道游戏论文设计去威客网上面试试!网址是
www.wktx.net

是河北卓创网络科技有限公司创建的威客网!
你可以去上面发个任务
就可以征集到好的作品的

❷ java华容道查错

其实没有必要引用你说的两个包

//华容道

import java.awt.Button;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class MoveExample {

/**
* @param args
*/
public static void main(String[] args) {
new Hua_Rong_Road();
}
}

class Person extends Button implements FocusListener {
int number;

Color c = new Color(255, 245, 170);

Person(int number, String s) {
super(s);
setBackground(c);
this.number = number;
c = getBackground();
addFocusListener(this);
}

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

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

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

Button left, right, above, below;

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

public Hua_Rong_Road() {
init();
setBounds(100, 200, 320, 360);
setVisible(true);
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}

);
}

public void init() {
setLayout(null);
add(restart);
restart.setBounds(100, 320, 120, 25);
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 Button();
right = new Button();
above = new Button();
below = new Button();
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, Button 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 actionPerformed(ActionEvent e) {
dispose();
new Hua_Rong_Road();
}

public void mouseExited(MouseEvent e) {
// TODO 自动生成方法存根

}
}

❸ Java华容道问题

1:road.add(this);
road是Hua_Rong_Road的对象,通过People 的构造方法传过来。
this指当前对象,这里指的是People 的一个对象。
road.add(this)即是把当前对象添加到road中。
其实就是把一个Button添加到Applet 中。

2:people[0]=new People(0,"曹操",104,54,100,100,this);
this:当前对象,Hua_Rong_Road的一个对象。
把当前对象作为参数传到People的构造方法中,在那边用road保存此对象的引用,用road可以拿到这个this指的对象。

3:People man=(People)e.getSource();
e.getSource()返回的是Object对象,前面加(People)强制转成People对象。

❹ 用java编程 华容道游戏

import java.awt.*;
import java.awt.event.*;
public class MoveExample
{
public static void main(String args[])
{
new Hua_Rong_Road();
}
}
class Person extends Button implements FocusListener
{
int number;
Color c = new Color(255,245,170);
Person(int number,String s)
{
super(s);
setBackground(c);
this.number = number;
c = getBackground();
addFocusListener(this);
}

public void focusGained(FocusEvent e)
{
setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];
Button left,right,above,below;
Button restart = new Button("重新开始");
public Hua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);
validate();
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);
add(restart);
restart.setBounds(100,320,120,25);
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 Button();
right = new Button();
above = new Button();
below = new Button();
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 mouseClicked(MouseEvent e){}
public void go(Person man,Button 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 actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road();
}
}

这是我们课本上的,颜色不一样,其他的都差不多,不过是用awt组件写的,你应该是要用swing写的吧,照这个改改吧...

❺ java华容道,点击菜单怎么跳一个框出来

菜单还得添加菜单项,给菜单项添加ActionListener
JMenuItem introItem = new JMenuItem("介绍");
introItem.addActionListener(this);
introMenu.add(introItem);
在事件处理方法actionPerformed中调用 JOptionPane.showMessageDialog(this, "弹出的对对话框中显示的信息");//this是你的窗口对象
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, "弹出的对对话框中显示的信息");
}

❻ 怎样在Java华容道里加图片

JButton有一个setIcon(Icon icon);方法可以在JButton上加图片。例如:
public class ButtonImage
{
public ButtonImage()
{
JFrame frame = new JFrame();
JButton button = new JButton("confirm");
ImageIcon icon = new ImageIcon("image.jpg");
button.setIcon(icon);
frame.add(button);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args)
{
new ImageButton();
}
}

❼ JAVA华容道程序扩展,想用图片替换例如曹操那些文字,得怎么修改代码,急

Person需继承JButton
然后使用setIcon方法设置图片
把Person的构造方法写成如下,图片位置和java文件同一个文件夹下,如果不行,可看一下path的值是什么,根据其值摆放图片
Person(int number, String s) {
super(s);
this.number = number;
setFont(new Font("宋体", Font.CENTER_BASELINE, 14));
setBackground(Color.pink);
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
Icon defaultIcon = new ImageIcon(path+"/a.jpg") ;
this.setIcon(defaultIcon );
}

若将Person(int number, String s) 写成
Person(int number, String s,String pic) {
super(s);
this.number = number;
setFont(new Font("宋体", Font.CENTER_BASELINE, 14));
setBackground(Color.pink);
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
Icon defaultIcon = new ImageIcon(path+"/"+pic) ;
this.setIcon(defaultIcon );
}
创建时传入图片名称即可

❽ 华容道(JAVA)

程序主要思路:1.定义了一个棋子类person(文件person.java中)。两个过滤类MyFilter(文件huangrong.java中),MyFilter2(文件huangrong.java中)分别过滤存盘文件和棋局文件。一个走棋声音类SoundMove(文件huangrong.java中)2.主窗体类(文件huangrong.java中)中有如下几个关键成员变量:
person person[] = new person[10];(10个棋子,依次为曹操,关羽,张飞,赵云,黄忠,马超,张云,卒1,卒2,卒3,卒4)
boolean IsStart = false;//是否开始下棋信息,默认为false还没开始下棋
Sequencer ser;//背景音乐
int space1X;//空闲棋盘1的X坐标(1-4)
int space1Y;//空闲棋盘1的Y坐标(1-5)
int space2X;//空闲棋盘2的X坐标(1-4)
int space2Y;//空闲棋盘2的Y坐标(1-5)
int activenum;//激活棋子对应编号(0-9)
int movenum;//记录移动的步子
String huiqi = new String();//保存以前走动的步子,格式为ID0X0Y0ID1X1Y1ID2X2Y2...(ID0,X0,Y0都为一位数)3.好多功能用函数单独实现,便于以后代码的改动,主要功能函数有:
public void centerWindow(Component component)//让窗口或控件(Component)处于屏幕中央
public void initgame()//用于重新开始游戏的初始化界面和变量
Move(int n)//n:第n个棋子。移动第n个棋子到指定处(也即移到(person[n].getP_positionX(),person[n].getP_positionY())处)
public void initMove(String a,int b,String c,String d)//设置棋局时用户每次设置好一个棋子准备设置下一个棋子前的准备工作
public void dragDo(String a,String b,String c)//设置棋局时每次产生拖动棋子事件时调用(将鼠标图标设为该棋子图片)
public void releaseDo(String a,int b,String c,int d,int e)//设置棋局时每次拖玩棋子,松开鼠标时调用,将棋子放在鼠标位置。
public boolean IsDrag(int n,int x,int y)//判断第n个棋子是否可以移到与移动前位置相差x,y的新位置处。4.程序主要算法(1)棋子的走动
先判断棋子是否可走,这里分两个大情况如果没有按开始按钮则不能走,如果按了开始按钮又分两个大情况,如果没有选中要走的棋子(activenum)则不可走,这
些情况都排除了,接下来就是判断是否走得动的情况了。由于是把棋子的左上端那块的位置定位棋子位置,因此当如下几种情况,表明棋子可走:((tempx-
person[activenum].getP_positionX())==person[activenum].getP_width()||(tempx-person[activenum].getP_positionX())==-1)&&
(tempy-person[activenum].getP_positionY()) && (tempy-person[activenum].getP_positionY())>-1则左右可走,当然这里还要判断一下棋子高度为2时两空闲块是否在一起。
((tempy-person[activenum].getP_positionY())==person[activenum].getP_height()||(tempy-person[activenum].getP_positionY())==-1) &&
(tempx-person[activenum].getP_positionX()) && (tempx-person[activenum].getP_positionX())>-1则上下可走,也要判断宽度为2时两空闲块是否在一起。
判断棋子为可走后接下来就是移动棋子,也即Move(N)函数的功能,由于在前面已经设置好了棋子新的位置,所以算法很简单,就是将图片按钮移到新位置,当然
此步要判断曹操是否移动下方正中处。还要将移到步子数movenum加1。 (2)设置棋局算法:先移曹操这其中要判断移动过程中是否移出棋盘外(后面棋子还要判断是否移入的位置已被棋子占用),移子基本算法是判断鼠标处位置在棋子中所
占位置,然后设置棋子的x,y为此处,再用Move函数即可实现。(3)存盘文件保存格式:依次保存person[0]-persaon[9]的X,Y信息再保存两空闲地方的X,Y然后保存机会棋子ID号(0-9),再保存已走步子数,最后另起一行保存
悔棋信息(悔棋信息格式上面已介绍)。读取存盘文件过程就是提取这些信息用于初始化界面和变量。(保存的信息有些可以省略如有棋子信息,就可推出空闲块信
息,有悔棋信息就可推出所走步数信息,此处保存只是方便打开存盘文件时信息的提取)(4)棋局文件只保存棋局棋子信息和两空闲块信息。功能简介:程序花了四天的时间完成(2005-6-4至2005-6-7),费了不少心血。本程序功能强大,拥有任意步悔棋(也即可以悔棋到游戏开始处)、开关启背景音乐(默认关闭)、开关启棋子走步声(默认开启)、设置,保存棋局(扩展名为.huyis)、打开棋局、打开存盘(扩展名为.huyih)接着上次玩,可以悔棋信息也在、游戏存盘等功能。注意点:1.每次开始游戏前请先点击开始游戏按钮,否则移动不了棋子。2.棋子移动方法是先点下要移动的棋子,再点击移动处。倘若可以到达,棋子将到达那处。每个棋子只能一步一步移动,倘若要移两步,只能一步一步移。3.每次点击重新开始程序的起始棋局为游戏内的默认棋局,你可以打开棋局用你设置好的棋局玩。4.设置棋局时,每移完一个棋子后点击左边第三个按钮(那个显示”**移完“的按钮),你可以移动下一个棋子。移完全部后点击移完卒4按钮,程序会问你是否保存棋局,若不保存,只能玩此盘,点击重新开始或程序退出下次再打开时不再拥有此棋局。5.请不要任意修改存盘文件和棋局文件,否则可能出现一些不可预知的错误。6.程序运行时图片、背景音乐以及棋子走步声文件和可执行文件应放在同一目录。不足点:1.对于打开存盘文件和棋局文件时没有检查格式是否正确,所以倘若被修改过,打开可能出现不可预知的错误。2.由于Java没有指针,对Java数据结构知识没有C++熟,所有原本打算编写的求解任何棋局是否有解以及所需最小步数和搜索深度没有写。这部分功能还有待扩充。3.对图片是否存在,以及棋子走步声文件是否存在没有做检查。4.由于时间关系,没有用常量表示棋子数,每类棋子的宽和高,棋盘宽和高等一些固定量,不便于以后代码的修改。由于期末临近,面临考试,所以只是把我实验报告的一部分摘过来,希望大家谅解。