package 華容道;
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供高州網(wǎng)站建設(shè)、高州做網(wǎng)站、高州網(wǎng)站設(shè)計(jì)、高州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、高州企業(yè)網(wǎng)站模板建站服務(wù),十余年高州做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
import java.awt.*;
import java.awt.event.*;
//主函數(shù)
public class Main {
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);//好像是焦點(diǎn)監(jiān)聽器
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);//只要單擊該按鈕則按鈕變顏色
}
public void focusLost(FocusEvent e) {
setBackground(c);//上一個(gè)按鈕回復(fù)原先的顏色
}
}
//華容道總類
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener{
Person person[] = new Person[10];
Button left,right,above,below;
Button restart = new Button("Start");//重新開始按鈕
public Hua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);//設(shè)置Frame為可見,默認(rèn)為不可見
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[]={"我","陸遜","姜維","陳宮","許攸","鄧艾","周瑜","龐統(tǒng)","諸葛亮","賈詡"};
for(int k=0;kname.length;k++)
{
person[k]=new Person(k,name[k]);
person[k].addMouseListener(this);
person[k].addKeyListener(this);
add(person[k]);
}//為所有的按鈕注冊(cè)所需的東西
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[0].requestFocus();
left=new Button();
right=new Button();
above=new Button();
below=new Button();
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(yh/2)
{
go(man,below);
}
if(yh/2)
{
go(man,above);
}
if(xw/2)
{
go(man,left);
}
if(xw/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;k10;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();
}
}
我給你個(gè)華容道的游戲參照下吧??梢宰约赫罩鱿隆6嗫蠢酉胂刖托辛?。
找個(gè)游戲做的不好,不要笑話啊。
import java.awt.*;
import javax.swing.JApplet.*;
import java.awt.event.*;
import javax.swing.*;
class People extends JButton implements FocusListener
{
Rectangle rect=null;
int left_x,left_y;//按鈕左上角坐標(biāo).
int width,height; //按鈕的寬和高.
String name;
int number;
public People(int number,String s,int x,int y,int w,int h,HuaRongRoad road)
{
super(s);
name=s;
this.number=number;
left_x=x;
left_y=y;
width=w;
height=h;
setBackground(Color.GREEN);
road.add(this);
addKeyListener(road);
setBounds(x,y,w,h);
addFocusListener(this);
rect=new Rectangle(x,y,w,h);
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
setBackground(Color.GREEN);
}
}
public class HuaRongRoad extends JApplet implements KeyListener,ActionListener
{
People people[]=new People[10];
Rectangle left,right,above,below;//華容道的邊界
JButton restart=new JButton("restart");
public void init()
{
getContentPane().setLayout(null);
getContentPane().add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
getContentPane().setBackground(Color.white);
people[0]=new People(0,"曹操",154,54,200,200,this);
people[1]=new People(1,"關(guān)羽",154,254,200,100,this);
people[2]=new People(2,"張飛",54,254,100,200,this);
people[3]=new People(3,"劉備",354,254,100,200,this);
people[4]=new People(4,"張遼",54,54,100,200,this);
people[5]=new People(5,"曹仁",354,54,100,200,this);
people[6]=new People(6,"兵 ",54,454,100,100,this);
people[7]=new People(7,"兵 ",354,454,100,100,this);
people[8]=new People(8,"兵 ",154,354,100,100,this);
people[9]=new People(9,"兵 ",254,354,100,100,this);
people[9].requestFocus();
people[0].setForeground(Color.white);
left=new Rectangle(49,49,5,510);
right=new Rectangle(454,49,5,510);
above=new Rectangle(49,49,410,5);
below=new Rectangle(49,554,410,5);
}
public void paint(Graphics g)
{ //華容道的邊界
super.paint(g);
g.setColor(Color.cyan);
g.fillRect(49,49,5,510);
g.fillRect(454,49,5,510);
g.fillRect(49,49,410,5);
g.fillRect(49,554,410,5);
//
g.drawString("單擊,按方向箭頭移動(dòng)",100,20);
g.setColor(Color.red);
g.drawString("曹操到達(dá)該位置",110,300);
}
public void keyPressed(KeyEvent e)
{
People man=(People)e.getSource();
man.rect.setLocation(man.getBounds().x,man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
man.left_y=man.left_y+100; //向下前進(jìn)50個(gè)單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現(xiàn)就退回50個(gè)單位
for(int i=0;i10;i++)
{
if((man.rect.intersects(people[i].rect))(man.number!=i))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
man.left_y=man.left_y-100; //向上前進(jìn)50個(gè)單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現(xiàn)就退回50個(gè)單位
for(int i=0;i10;i++)
{
if((man.rect.intersects(people[i].rect))(man.number!=i))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
man.left_x=man.left_x-100; //向左前進(jìn)50個(gè)單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現(xiàn)就退回50個(gè)單位
for(int i=0;i10;i++)
{
if((man.rect.intersects(people[i].rect))(man.number!=i))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
man.left_x=man.left_x+100; //向右進(jìn)50個(gè)單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現(xiàn)就退回50個(gè)單位
for(int i=0;i10;i++)
{
if((man.rect.intersects(people[i].rect))(man.number!=i))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{
getContentPane().removeAll();
this.init();
}
}
這個(gè)我試了的沒有任務(wù)問題,稀望對(duì)你有點(diǎn)幫助,記得類名要改為Hua_Rong_Road ,因?yàn)橹挥蠬ua_Rong_Road 這個(gè)類是公開的.另外包名也改下package xxxx(你自己建的包名),玩游戲時(shí)移動(dòng)人物,用鍵盤(上下左右 ,--,--,上,下)操作,鼠標(biāo)是不能移動(dòng) 人物的,照著我說的做,應(yīng)該是沒什么問題的:
package baidu.testfive;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
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;
class People extends Button implements FocusListener // 代表華容道人物的類。
{
Rectangle rect = null;
int left_x, left_y;// 按扭的左上角坐標(biāo).
int width, height; // 按扭的寬和高.
String name;
int number;
People(int number, String s, int x, int y, int w, int h, Hua_Rong_Road road)// 構(gòu)造函數(shù)
{
super(s);
name = s;
this.number = number;
left_x = x;
left_y = y;
width = w;
height = h;
setBackground(Color.orange);
road.add(this);
addKeyListener(road);
setBounds(x, y, w, h);
addFocusListener(this);
rect = new Rectangle(x, y, w, h);
}
public void focusGained(FocusEvent e) {
setBackground(Color.red);
}
public void focusLost(FocusEvent e) {
setBackground(Color.orange);
}
}
public class Hua_Rong_Road extends Applet implements KeyListener,
ActionListener {
People people[] = new People[10];
Rectangle left, right, above, below;// 華容道的邊界 .
Button restart = new Button("重新開始");
public void init() {
setLayout(null);
add(restart);
restart.setBounds(5, 5, 80, 25);
restart.addActionListener(this);
people[0] = new People(0, "曹操", 104, 54, 100, 100, this);// 構(gòu)造曹操
people[1] = new People(1, "關(guān)羽", 104, 154, 100, 50, this);// 構(gòu)造關(guān)羽
people[2] = new People(2, "張飛", 54, 154, 50, 100, this);
people[3] = new People(3, "劉備", 204, 154, 50, 100, this);
people[4] = new People(4, "張遼", 54, 54, 50, 100, this);
people[5] = new People(5, "曹仁", 204, 54, 50, 100, this);
people[6] = new People(6, "兵 ", 54, 254, 50, 50, this);
people[7] = new People(7, "兵 ", 204, 254, 50, 50, this);
people[8] = new People(8, "兵 ", 104, 204, 50, 50, this);
people[9] = new People(9, "兵 ", 154, 204, 50, 50, this);
people[9].requestFocus();
left = new Rectangle(49, 49, 5, 260);
people[0].setForeground(Color.white);
right = new Rectangle(254, 49, 5, 260);
above = new Rectangle(49, 49, 210, 5);
below = new Rectangle(49, 304, 210, 5);
}
public void paint(Graphics g) {// 畫出華容道的邊界:
g.setColor(Color.cyan);
g.fillRect(49, 49, 5, 260);// left.
g.fillRect(254, 49, 5, 260);// right.
g.fillRect(49, 49, 210, 5); // above.
g.fillRect(49, 304, 210, 5);// below.
// 提示曹操逃出位置和按鍵規(guī)則:
g.drawString("點(diǎn)擊相應(yīng)的人物,然后按鍵盤上的上下左右箭頭移動(dòng)", 100, 20);
g.setColor(Color.red);
g.drawString("曹操到達(dá)該位置", 110, 300);
}
public void keyPressed(KeyEvent e) {
People man = (People) e.getSource();// 獲取事件源.
man.rect.setLocation(man.getBounds().x, man.getBounds().y);
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
man.left_y = man.left_y + 50; // 向下前進(jìn)50個(gè)單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或下邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個(gè)單位距離。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_y = man.left_y - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(below)) {
man.left_y = man.left_y - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
man.left_y = man.left_y - 50; // 向上前進(jìn)50個(gè)單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或上邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個(gè)單位距離。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_y = man.left_y + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(above)) {
man.left_y = man.left_y + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
man.left_x = man.left_x - 50; // 向左前進(jìn)50個(gè)單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或左邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個(gè)單位距離。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_x = man.left_x + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(left)) {
man.left_x = man.left_x + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
man.left_x = man.left_x + 50; // 向右前進(jìn)50個(gè)單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或右邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個(gè)單位距離。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_x = man.left_x - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(right)) {
man.left_x = man.left_x - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void actionPerformed(ActionEvent e) {
this.removeAll();
this.init();
}
}
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[] = {"曹操","關(guān)羽","張飛","劉備","趙云","黃忠","兵","兵","兵","兵"};
for(int k = 0;kname.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(yh/2)
{
go(man,below);
}
if(yh/2)
{
go(man,above);
}
if(xw/2)
{
go(man,left);
}
if(xw/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;k10;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組件寫的,你應(yīng)該是要用swing寫的吧,照這個(gè)改改吧...
import java.awt.*;
import java.awt.event.*;
public class MoveExample //主類
{
public static void main(String args[]) //定義主方法
{
new Hua_Rong_Road(); //創(chuàng)建對(duì)象
}
}
class Person extends Button implements FocusListener
{
int number;
Color c = new Color(128,128,128);
Person(int number,String s)//構(gòu)造方法
{
super(s);//調(diào)用父類s的構(gòu)造方法
setBackground(c);//設(shè)置組件的背景色
this.number = number;//調(diào)用當(dāng)前的number
c = getBackground();
addFocusListener(this);//添加焦點(diǎn)事件監(jiān)聽器
}
public void focusGained(FocusEvent e)//焦點(diǎn)事件觸發(fā)
{
setBackground(Color.blue);
}
public void focusLost(FocusEvent e)//焦點(diǎn)事件失去
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];//person類的數(shù)組
Button left,right,above,below;
Button restart = new Button("重新開始");
public Hua_Rong_Road() //華容道的構(gòu)造方法
{
init(); //初始化
setBounds(100,100,320,360);//設(shè)置窗口在屏幕上出現(xiàn)位置,和窗口大小
setVisible(true);//設(shè)置窗口可見
setResizable(true);//設(shè)置窗口可調(diào)節(jié)
validate();//刷新
addWindowListener( new WindowAdapter()//獲得窗口事件監(jiān)視器
{
public void windowClosing(WindowEvent e)//窗口正在被關(guān)閉時(shí),窗口監(jiān)視器調(diào)用該方法
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);//設(shè)置默認(rèn)布局
add(restart);//添加重新開始
restart.setBounds(100,320,120,25);//重新開始按鈕大小
restart.addActionListener(this);//事件源獲得監(jiān)視器
String name[] = {"曹操","關(guān)羽","張飛","劉備","趙云","黃忠","兵","兵","兵","兵"};
for(int k = 0;kname.length;k++)
{
person[k] = new Person(k,name[k]);//給按鈕添加名字
person[k].addMouseListener(this);//每個(gè)按鈕都注冊(cè)鼠標(biāo)事件
person[k].addKeyListener(this);//每個(gè)按鈕都注冊(cè)鍵盤事件
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);//為每個(gè)人物按鈕設(shè)置位置和大小
person[9].requestFocus();//把焦點(diǎn)先設(shè)置在這個(gè)按鈕上
left = new Button();//畫出游戲界面邊框,并用定義的left,right,above,below控制大小
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)//響應(yīng)鍵盤事件,按鍵,釋放鍵,按下和釋放組合
{
Person man = (Person)e.getSource();//獲得事件源
if(e.getKeyCode()==KeyEvent.VK_DOWN)//響應(yīng)用戶按下方向光標(biāo)的操作;用KeyEvent類中的getkeycode()判斷哪個(gè)鍵被按下
{
go(man,below); //go方法控制移動(dòng)
}
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(yh/2)
{
go(man,below);
}
if(yh/2)
{
go(man,above);
}
if(xw/2)
{
go(man,left);
}
if(xw/2)
{
go(man,right);
}
}
public void mouseReleased(MouseEvent e){}//鼠標(biāo)事件
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)//向各個(gè)方向移動(dòng)
{
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;k10;k++)
{
Rectangle personRect = person[k].getBounds();
if((manRect.intersects(personRect))(man.number!=k))//如果覆蓋就不移動(dòng)
{
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();
}
}