❶ 基於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.由於時間關系,沒有用常量表示棋子數,每類棋子的寬和高,棋盤寬和高等一些固定量,不便於以後代碼的修改。由於期末臨近,面臨考試,所以只是把我實驗報告的一部分摘過來,希望大家諒解。